opencv欧几里得聚类vs findContours [英] opencv euclidean clustering vs findContours
问题描述
我有以下图像遮罩:
我想应用类似于cv::findContours
的方法,但是该算法仅将相同组中的连接点连接在一起.我想以一定的公差来做到这一点,即我想在给定的半径公差内将彼此靠近的像素相加:这类似于欧几里得距离层次聚类.
I want to apply something similar to cv::findContours
, but that algorithm only joins connected points in the same groups. I want to do this with some tolerance, i.e., I want to add the pixels near each other within a given radius tolerance: this is similar to Euclidean distance hierarchical clustering.
这是在OpenCV中实现的吗?还是有任何快速的方法来实现这一点?
Is this implemented in OpenCV? Or is there any fast approach for implementing this?
我想要的东西与此类似,
What I want is something similar to this,
http://www.pointclouds.org/documentation/tutorials/cluster_extraction.php
应用于此蒙版的白色像素.
applied to the white pixels of this mask.
谢谢.
推荐答案
您可以使用分区:
partition
将元素集划分为等效类.您可以将等效类定义为给定欧氏距离(半径公差)内的所有点
partition
splits an element set into equivalency classes. You can define your equivalence class as all points within a given euclidean distance (radius tolerance)
如果您具有C ++ 11,则可以简单地使用lambda函数:
If you have C++11, you can simply use a lambda function:
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;
});
否则,您可以只构建一个仿函数(请参见下面的代码中的详细信息).
otherwise, you can just build a functor (see details in the code below).
以适当的半径距离(我发现这张图片有18个效果很好),我得到了:
With appropriate radius distance (I found 18 works good on this image), I got:
完整代码:
#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;
}
这篇关于opencv欧几里得聚类vs findContours的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!