c++ - 在图像上的 OpenCV 中绘制单个轮廓

标签 c++ image opencv contour

在 OpenCV 中绘制单个轮廓的最佳方法是什么?据我所知drawContours只能处理多个轮廓。

背景:我想将我的代码更改为 for each 循环。旧代码:

//vector<vector<Point> > contours = result of findContours(...)
for (int i = 0; i < contour.size; i++){
    if(iscorrect(contours[i])){
        drawContours(img, contours, i, color, 1, 8, hierarchy);
    }
 }

呈现方式in this mailing list非常丑陋:

for (vector<Point> contour : contours){
     if(iscorrect(contour)){
          vector<vector<Point> > con = vector<vector<Point> >(1, contour);
          drawContours(img, con, -1, color, 1, 8);
     }
}

有没有更简洁的方法来绘制单个轮廓(vector对象)?

最佳答案

我有同样的问题,到目前为止我发现的更好的方法是:

for (vector<Point> contour : contours){
  if(iscorrect(contour)){
    drawContours(img, vector<vector<Point> >(1,contour), -1, color, 1, 8);
  }
}

这与您的几乎相同,但少了一行。

我的第一个想法是使用 Mat(contour) 但它没有用。

如果您找到更好的方法,请在此处发布并分享智慧。

关于c++ - 在图像上的 OpenCV 中绘制单个轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21887000/

相关文章:

c++ - MFC - 仅最小化主 MDI 窗口

c++ - C++运算符中的隐式类型转换规则

java - java 中的 opencv 使用 MAT 而不是 vector 作为标签

c# - C# 在图像上绘制字符串时如何根据图像大小调整文本大小

image - 从 MPEG-2 传输流 (H.264 - Annex B) 字节流中提取原始 I 帧图像数据

c++ - 如何将 OpenCV cv::Mat 转换为 QImage

c# - 从 Emgu 2 升级到 Emgu 3.1.0.1 后随机出现 Emgu AccessViolationException

android - QAbstractButton 和 QPushButton* 之间的比较缺少强制转换

c++ - 我需要释放推力返回的 device_ptr 吗?

python - 在 Keras 的 ImageDataGenerator.flow_from_directory 中处理无效/损坏的图像文件