【问题标题】:FindContour: Measure distance between contoursFindContour:测量轮廓之间的距离
【发布时间】:2015-07-17 11:51:59
【问题描述】:

非常感谢您在帖子中进一步了解:Finding Minimum Distance between Contours

我正在使用 FindContours 并根据需要获取多个轮廓。 问题是我想找到每对相邻对之间的最小距离, 例如在黄色轮廓和深绿色之间,在深绿色和青色之间,在青色和紫色之间。

我从上面的帖子中理解了一般方法。 但是谁能告诉我如何一个接一个地选择它们(自动)?

void thresh_function(int, void*)
{

        Mat canny_output;
        vector<vector<Point> > contours;
        vector<Vec4i> hierarchy;


      /// Detect edges using canny
        Canny( roiImg, canny_output, threshold_value, threshold_value*2, 3 );
      /// Find contours
        findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

      /// Draw contours
        Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
        for( int i = 0; i< contours.size(); i++ )
         {
           Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
           drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );//Scalar(255,255,255)
         }

        erode(drawing,drawing,erodeElement2);
        erode(drawing,drawing,erodeElement1);
        dilate(drawing,drawing,dilateElement);

      /// Show in a window
     //namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
     resize(drawing, enlargeD0, Size(), 2, 2, CV_INTER_CUBIC);
     done = 1;
     imshow("Contours", enlargeD0);
}

【问题讨论】:

    标签: c++ opencv image-processing edge-detection opencv-contour


    【解决方案1】:
    vector<vector<Point> > all_contours;
    ... 
    findContours(... all_contours ... CV_CHAIN_APPROX_NONE ...);
    ...
    
    // Remove small contours
    int minSize = 20;
    vector<vector<Point> > contours;
    contours.reserve(all_contours.size());
    for(int i=0; i<all_contours.size(); ++i)
    {
        if(all_contours[i].size() > minSize) 
        {
             contours.push_back(all_contours[i]);
        }
    }  
    
    Mat1f dist(contours.size(), contours.size(), 0.f);
    for(int i=0; i<contours.size()-1; ++i)
    {
        const vector<Point>& firstContour = contours[i];
        for(int j=i+1; j<contours.size(); ++j)
        {
            const vector<Point>& secondContour = contours[j];
            float d = compute_pairwise_distance(firstContour, secondContour); // You should implement this
            dist(i,j) = d;
            dist(j,i) = d;  // distance from first to second is equal
                            // to distance from second to first 
        }
    }
    
    // Now dist contains the pairwise distances between i-th and j-th contours
    

    【讨论】:

    • Mat1f 在 OpenCV 2.4.9 中可用吗?
    • 是的,只是 Mat_&lt;float&gt; 的 typedef。如果您愿意,可以使用泛型 Mat 和类型 CV_32FC1,但是您需要使用 distances.at&lt;float&gt;(...) 访问它
    • 好的,非常感谢。我仍然有一点疑问,请注意图像中有一个轮廓(在黄色之后是天蓝色),我在过滤阶段无法摆脱。 :( 我不希望该轮廓的距离介入其中。
    • @Mandar 最简单的做法是根据它们的大小删除小轮廓。您可能想使用CV_CHAIN_APPROX_NONE 来获取轮廓中的所有像素,因此轮廓的大小实际上就是像素数。查看更新的答案。
    • 愚蠢地问,但是,你为什么继续使用 ++i ?
    猜你喜欢
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 2013-10-03
    • 2012-01-27
    相关资源
    最近更新 更多