gpt4 book ai didi

c++ - OpenCV findContours 点 vector

转载 作者:行者123 更新时间:2023-11-28 05:01:05 25 4
gpt4 key购买 nike

我有一个二维点 vector ,我需要找到由这些点形成的所有轮廓。不幸的是,cv::findContours 无法处理点数组,它需要二进制图像。

所以问题是有没有任何解决方法来获取点的轮廓?也许可以使用这些点形成二值图像,然后在 cv::findContours 函数中使用该图像?请在此指教。

最佳答案

如果您知道图像的大小,则可以创建零的二值图像并用值 255 填充所有二维点。然后使用 cv::findContours 找到二值图像中的所有轮廓.

以下代码片段可能对您有所帮助:

// If pts is your array of float points and r,c are number of rows and columns of image
//calculate total number of points in array
int n = sizeof(pts) / sizeof(*pts);
//if points are stored in vector, then use n = pts.size()
//create binary image
cv::Mat image = cv::Mat::zeros(Size(c, r), CV_8UC1);
//fill all the points with value 255
for (int i = 0; i < n; i++) {
image.at<uchar>(p[i]) = 255;
}
//find all contours in binary image and save in contours variale
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(image, contours, hierarchy, RETR_LIST, CHAIN_APPROX_NONE);

关于c++ - OpenCV findContours 点 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45957172/

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