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

时间:2014-02-19 16:51:42

标签: c++ image opencv contour

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

背景:我想将每个循环的代码更改为a。旧代码:

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

是否有更简洁的方法来绘制单个轮廓(矢量&lt; Point&gt;对象)?

2 个答案:

答案 0 :(得分:8)

我有同样的问题,直到现在我找到的更好的方法是:

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

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

我的第一个想法是使用Mat(contour),但它不起作用。

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

答案 1 :(得分:1)

使用绘制轮廓,它不是很漂亮,但你不需要循环。

std::vector<cv::Point> contour;
std::vector<std::vector<cv::Point> > contourVec;
contourVec.push_back(contour);

cv::drawContours(img,contourVec,0,color,1,8,hierarchy); //Replace i with 0 for index. 
相关问题