gpt4 book ai didi

c++ - opencv 欧氏聚类与 findContours

转载 作者:可可西里 更新时间:2023-11-01 18:19:53 31 4
gpt4 key购买 nike

我有以下图像 mask :

mask

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

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

我想要的是类似这样的东西,

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

应用于此 mask 的白色像素。

谢谢。

最佳答案

您可以使用 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 个在这张图片上效果很好),我得到:

enter image description here

完整代码:

#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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com