【问题标题】:Draw single Contour in OpenCV on image在图像上的 OpenCV 中绘制单个轮廓
【发布时间】:2014-03-20 04:01:48
【问题描述】:

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

有没有更简洁的方法来绘制单个轮廓(向量 Object)?

【问题讨论】:

    标签: c++ image opencv contour


    【解决方案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. 
    

    【讨论】:

    • 我只想绘制满足某些属性的轮廓。这就是迭代的原因。
    【解决方案2】:

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

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

    这几乎和你的一样,但是少了一行。

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

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

    【讨论】:

    • 开启优化后编译后的机器码应该是一样的。我没有更好的办法,也许通过 openCV 更新会有一些实现。
    【解决方案3】:

    使用 OpenCV 3.0 polylines() 更加灵活,我们可以这样做,例如:

    vector<Point> approx;
    polylines(frame, approx, true, color, 1, 8);
    

    在您的循环中,这将是:

    for (vector<Point> contour : contours) {
        if (iscorrect(contour)) {
            polylines(frame, approx, true, color, 1, 8);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-12
      • 2012-11-06
      • 1970-01-01
      • 2017-11-06
      • 2021-09-07
      • 2020-11-24
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多