opencv euclidean clustering vs findContours

man*_*tta 7 c++ opencv cluster-analysis

我有以下图像掩码:

面具

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

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

我想要的是类似的东西,

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

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

谢谢.

Mik*_*iki 11

你可以使用分区:

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; 
});
Run Code Online (Sandbox Code Playgroud)

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

通过适当的半径距离(我发现这个图像上的18个效果很好),我得到了:

在此输入图像描述

完整代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)