简体   繁体   中英

FindContour: Measure distance between contours

I would be very thankful to know further on post: Finding Minimum Distance between Contours

I am using FindContours and as required getting multiple contours. The problem is that I want to find min distance between each adjacent pair, eg between yellow contour and dark green, between dark green and cyan, between cyan and purple.

I understood the general method from above post. But can anyone please tell me how can i select them one after another(automaticaly)?

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM