【问题标题】:OpenCV findContours of points vectorOpenCV findContours of points 向量
【发布时间】:2017-08-30 09:50:42
【问题描述】:

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

那么问题是有什么解决方法来获取点的轮廓吗?也许可以使用这些点形成一个二进制图像,然后在cv::findContours 函数中使用这个图像? 请在这里指教。

【问题讨论】:

标签: c++ opencv


【解决方案1】:

如果你知道图像的大小,你可以创建一个零的二进制图像,并用值 255 填充所有 2D 点。然后使用 cv::findContours 查找二进制图像中的所有轮廓.

以下代码 sn-p 可能对您有所帮助:

// 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);

【讨论】:

  • 我知道图片大小。您能否详细描述您的解决方法?我有浮点坐标,所以不知道如何填充所有 2D 浮点数
  • 我已经编辑了我的答案并添加了代码 sn-p。它可能会帮助你。
  • 反转x和y...或者直接使用p[i]访问图片
  • 是的,使用 image.at(p[i].y, p[i].x) = 255;或 image.at(p[i]) = 255;
  • 看起来像解决方案。我无法获得正确的图像尺寸,但我会在今晚尝试这样做并更新结果。谢谢。
猜你喜欢
  • 2015-11-21
  • 2012-12-14
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 2017-01-12
  • 1970-01-01
  • 2018-03-04
相关资源
最近更新 更多