opencv 欧氏聚类与 findContours
opencv euclidean clustering vs findContours
我有以下图像遮罩:
我想应用类似于 cv::findContours
的东西,但该算法只连接同一组中的连接点。我想以一定的公差来做到这一点,即我想在给定的半径公差范围内添加彼此靠近的像素:这类似于欧几里德距离层次聚类。
这是在 OpenCV 中实现的吗?或者有什么快速的实现方法吗?
我想要的是类似这样的,
http://www.pointclouds.org/documentation/tutorials/cluster_extraction.php
应用于此遮罩的白色像素。
谢谢。
我建议使用DBSCAN算法。这正是您要寻找的。使用简单的欧氏距离甚至曼哈顿距离可能效果更好。
输入全是白点(阈值)。输出是一组点(你的连接组件)
编辑:
我自己尝试了 DBSCAN,结果如下:
如你所见,只有真正连接的点被认为是一个集群。
这个结果是使用standerad DBSCAN算法得到的EPS=3(静态不需要调整)MinPoints=1(也是静态的)和 曼哈顿距离
您可以为此使用 partition:
partition
将元素集拆分为等价 classes。 您可以将等价 class 定义为 all给定欧式距离(半径公差)内的点
如果你有 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 个在这张图片上效果很好),我得到:
完整代码:
#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;
}
我有以下图像遮罩:
我想应用类似于 cv::findContours
的东西,但该算法只连接同一组中的连接点。我想以一定的公差来做到这一点,即我想在给定的半径公差范围内添加彼此靠近的像素:这类似于欧几里德距离层次聚类。
这是在 OpenCV 中实现的吗?或者有什么快速的实现方法吗?
我想要的是类似这样的,
http://www.pointclouds.org/documentation/tutorials/cluster_extraction.php
应用于此遮罩的白色像素。
谢谢。
我建议使用DBSCAN算法。这正是您要寻找的。使用简单的欧氏距离甚至曼哈顿距离可能效果更好。 输入全是白点(阈值)。输出是一组点(你的连接组件)
编辑:
我自己尝试了 DBSCAN,结果如下:
如你所见,只有真正连接的点被认为是一个集群。
这个结果是使用standerad DBSCAN算法得到的EPS=3(静态不需要调整)MinPoints=1(也是静态的)和 曼哈顿距离
您可以为此使用 partition:
partition
将元素集拆分为等价 classes。 您可以将等价 class 定义为 all给定欧式距离(半径公差)内的点
如果你有 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 个在这张图片上效果很好),我得到:
完整代码:
#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;
}