我有以下图像蒙版:

c++ - opencv 欧几里得聚类与 findContours-LMLPHP

我想应用类似于 cv::findContours 的东西,但该算法只连接相同组中的连接点。我想用一些容差来做到这一点,即我想在给定的半径容差内添加彼此靠近的像素:这类似于欧几里德距离层次聚类。

这是在 OpenCV 中实现的吗?或者有什么快速的方法来实现这个?

我想要的是与此类似的东西,

http://www.pointclouds.org/documentation/tutorials/cluster_extraction.php

应用于此蒙版的白色像素。

谢谢你。

最佳答案

您可以为此使用 partition:
partition 将元素集拆分为等价类。您可以将等价类定义为给定欧几里得距离(半径容差)内的所有点

如果你有 C++11,你可以简单地使用一个 lambda 函数:

int th_distance = 18; // radius tolerance

int th2 = th_distance * th_distance; // squared radius tolerance
vector<int> labels;

int n_labels = partition(pts, labels, [th2](const Point& lhs, const Point& rhs) {
    return ((lhs.x - rhs.x)*(lhs.x - rhs.x) + (lhs.y - rhs.y)*(lhs.y - rhs.y)) < th2;
});

否则,您可以只构建一个仿函数(请参阅下面代码中的详细信息)。

使用适当的半径距离(我发现 18 在这张图片上效果很好),我得到:

c&#43;&#43; - opencv 欧几里得聚类与 findContours-LMLPHP

完整代码:
#include <opencv2\opencv.hpp>
#include <vector>
#include <algorithm>

using namespace std;
using namespace cv;

struct EuclideanDistanceFunctor
{
    int _dist2;
    EuclideanDistanceFunctor(int dist) : _dist2(dist*dist) {}

    bool operator()(const Point& lhs, const Point& rhs) const
    {
        return ((lhs.x - rhs.x)*(lhs.x - rhs.x) + (lhs.y - rhs.y)*(lhs.y - rhs.y)) < _dist2;
    }
};

int main()
{
    // Load the image (grayscale)
    Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);

    // Get all non black points
    vector<Point> pts;
    findNonZero(img, pts);

    // Define the radius tolerance
    int th_distance = 18; // radius tolerance

    // Apply partition
    // All pixels within the radius tolerance distance will belong to the same class (same label)
    vector<int> labels;

    // With functor
    //int n_labels = partition(pts, labels, EuclideanDistanceFunctor(th_distance));

    // With lambda function (require C++11)
    int th2 = th_distance * th_distance;
    int n_labels = partition(pts, labels, [th2](const Point& lhs, const Point& rhs) {
        return ((lhs.x - rhs.x)*(lhs.x - rhs.x) + (lhs.y - rhs.y)*(lhs.y - rhs.y)) < th2;
    });

    // You can save all points in the same class in a vector (one for each class), just like findContours
    vector<vector<Point>> contours(n_labels);
    for (int i = 0; i < pts.size(); ++i)
    {
        contours[labels[i]].push_back(pts[i]);
    }

    // Draw results

    // Build a vector of random color, one for each class (label)
    vector<Vec3b> colors;
    for (int i = 0; i < n_labels; ++i)
    {
        colors.push_back(Vec3b(rand() & 255, rand() & 255, rand() & 255));
    }

    // Draw the labels
    Mat3b lbl(img.rows, img.cols, Vec3b(0, 0, 0));
    for (int i = 0; i < pts.size(); ++i)
    {
        lbl(pts[i]) = colors[labels[i]];
    }

    imshow("Labels", lbl);
    waitKey();

    return 0;
}

关于c++ - opencv 欧几里得聚类与 findContours,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33825249/

10-12 02:07