- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有以下图像 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 个在这张图片上效果很好),我得到:
完整代码:
#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/
我刚开始学习 Emgu 和 OpenCV。所以这可能是一个愚蠢的问题。当我查看 Emgu 文档(参见链接 http://www.emgu.com/wiki/files/3.1.0/document/h
我在以下代码查找轮廓的运行时遇到段错误。我提到了this在此表格上发帖,但对我帮助不大。我知道 findContours 有一些问题这是 findContours 的另一期.请检查这两个链接并帮助我解
我正在使用这样的函数; Mat large = imread(path+name); Mat rgb; if (large.rows > 2500 || large.cols > 1250) {
我正在尝试检测一个球并为其划定边界。显示错误 segmentation fault (core dumped) 错误就在这里, vector > contours; vector hierar
我想使用 haar cascade 来计算汽车数量。 #import libraries of python opencv import numpy as np import cv2 import g
我正在使用 Android OpenCV。我有一个方形标记图像。我必须找出实时提要中标记的四个角的位置及其索引的位置。我能够在帧大小为 640*480 的实时馈送中获得这四个点的位置。但是索引位置的问
我写了一段代码,在单 channel 空白图像中绘制圆、线和矩形。在那之后,我只是找出图像中的轮廓,并且我正确地得到了所有轮廓。但是在找到轮廓后,我的源图像变得扭曲了。为什么会这样?任何人都可以帮助我
我们正在使用 opencv for android 实现 OCR,一切都很顺利,直到使用 Imgproc.findcontours() 查找轮廓的部分它返回的轮廓与输入图像中的顺序不同IE :输入图像
这个问题在这里已经有了答案: What is the algorithm that opencv uses for finding contours? (2 个答案) 关闭 7 年前。 OpenCV
我有下面的图片,并尝试用这些线找到最大的 OpenCV 矩形 std::vector > contours; cv::findContours(result,contours,CV_RETR_LIST
我是 C++ 和 opencv 的新手。我写了一个简单的程序,您可以在下面找到它,但是当我运行它时,我总是会抛出异常 findContours(img, ctr, CV_RETR_LIST, CV_C
我尝试在二进制图像中找到轮廓,但是当尝试执行cvFindContours时,它给了我该错误消息 Traceback (most recent call last): File "convert.py"
我正在尝试实现一个在二进制图像中找到轮廓并过滤掉小轮廓的函数。 这是我的代码和示例图像。这是一个 super 简单的功能,可以去除小面积的 Blob 。但我不断得到“边缘轮廓”而不是区域轮廓。 :S
我遇到了一个奇怪的行为,虽然我可以解决它,但我想知道这是为什么。 当我使用 cv2.findContours 时,它会修改原始图像,即使我没有将它传递给函数。这是一个可以找到图片的最小示例here .
我想问如何在 blob(findcontours)上检测人类或行人?我尝试学习如何使用 findcontours() 来检测框架上的任何对象,如下所示: #include"stdafx.h" #inc
提供一些上下文: 我正在尝试获取这张图片中的盒子数量, 我上面的图像存储在ndarray blank_img中。 如果我运行以下命令: v = np.median(blank_img) sigma =
Cow browncow; vector Cows; Mat temp; threshold.copyTo(temp); vector > contours; vector hierarchy; f
我试过 findContours 有 2 张图片。实际上,它们是一体的。一个是彩色图像(jpg),另一个是由 MS Paint 从颜色创建的(导出为单色图像 - bmp): #include "cv.
我是opencv和Java的初学者。我想学习Imgproc.findContours()方法的用法和功能。我没有任何资料可以学习。谁能详细解释一下它的工作原理。或有人可以建议我作为学习它的合适资源。
当我们想根据某个阈值查找给定图像的轮廓时,我们使用cv2.findContours()函数,该函数除其他外返回轮廓列表(表示图片轮廓的数组的Pythonic列表)。 这是use of the func
我是一名优秀的程序员,十分优秀!