【问题标题】:Draw the biggest element conncted using areaContours (OpenCV )使用区域轮廓(OpenCV)绘制连接的最大元素
【发布时间】:2014-02-25 01:54:31
【问题描述】:

我是这个主题的新手。我正在尝试使用一些 openCV 库来制作一个项目,但我在 findContour、drawContours 方面遇到了一些问题。在我读取输入图像并进行阈值处理后,我在代码中使用 findContours

cv::Mat cont;    // i create a matrix


    result2.copyTo(cont);  // this is the copy of the input image tresholded    
    std::vector<std::vector<cv::Point> > contours;          
    std::vector<cv::Vec4i> hierarchy;
            cv::findContours(cont,contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE,  cv::Point(0,0 ));

    eiid::imshow("Im_Find", cont);   // I watch the points
            for( int s = 0; s< contours.size(); s++ )
    {

     printf(" * Contour[%d]=  Area OpenCV: %.2f \n", s,cv::contourArea(contours[s])) ;   
     drawContours( cont, contours, s, cv::Scalar(255,255), 10, 1, hierarchy, 0, cv::Point() );          
 }
   eiid::imshow ( "FINE" , cont);

我有 7 个元素连接,(所以 7 areaContours) 这是我的目标:知道我所有元素的区域,我想画,画,只有最大的区域(所以排除其他 6 个区域) 我做不到,有人可以帮助我吗? (我试图将我的 7 个区域存储在双数组中,但我无法继续:()

谢谢你回答我,但我还是有问题。您的代码有效,但我不知道为什么图像中仍有一些像素...当我观看最终图像时,我看到了我最大的轮廓,但也看到了一些白色像素:( ..问题是我必须使用这个算法更多图片,所以我的代码必须是所有图片的标准..也许我使用不正确的功能??


我的图像是黑白的,但我认为 Color(255,255) 对我来说很好。但是我理解我的问题是 findContours 将像素放入我的图像中,所以我复制到其他图像中,现在我没有问题。但是我仍然有问题:(现在我有最大的轮廓,但我需要将它应用到我的原始图像上以从中排除一些文本。问题是最大轮廓的内部是黑色的,但在我原来的图片,里面是白色的……所以我不能得到像“less-”(interseption)这样的操作……:(我该怎么做?

【问题讨论】:

  • 图片是彩色还是灰度?我认为这不是问题,但您应该将 cv::Scalar(255,255) 更改为 cv::Scalar(255) 是灰度或 cv::Scalar(255,255,255) 如果是颜色。

标签: c++ opencv


【解决方案1】:

先找到最大的,然后画出来。

int idx = -1;
double maxArea = 0.0;

for( int s = 0; s< contours.size(); s++ )
 {
  double area = cv::contourArea(contours[s]);
  if( area > maxArea){
         maxArea = area;
         idx = s;
    }
 }  

 cv::drawContours( cont, contours, idx , cv::Scalar(255,255), 10, 1, hierarchy, 0, cv::Point() );          

【讨论】:

    【解决方案2】:

    我们去吧。 (仅供参考:尽量不要偷懒,弄清楚下面我的函数会发生什么。

    cv::Mat findBiggestBlob(cv::Mat & matImage){
        int largest_area=0;
        int largest_contour_index=0;
    
        vector< vector<Point> > contours; // Vector for storing contour
        vector<Vec4i> hierarchy;
    
        findContours( matImage, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
    
        for( int i = 0; i< contours.size(); i++ ) {// iterate through each contour. 
            double a=contourArea( contours[i],false);  //  Find the area of contour
            if(a>largest_area){
                largest_area=a;
                largest_contour_index=i;                //Store the index of largest contour
                //bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
            }
        }
    
        drawContours( matImage, contours, largest_contour_index, Scalar(255), CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
        return matImage;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多