2

I have the following code:

    findContours( src, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );

    Mat drawing = Mat::zeros( src.size(), CV_8UC3 );

    double largest_area = 0;

    for( int i = 0; i < contours.size(); i++) {  // get the largest contour
        area = fabs( contourArea( contours[i] ) );
        if( area >= largest_area ){
            largest_area = area;
            largest_contours.clear(); 
            largest_contours.push_back( contours[i] );
        }
    }

    if( largest_area >= 3000 ){   // draw the largest contour if exceeded minimum largest area 
        drawContours( drawing, largest_contours, -1, Scalar(0,0,255), 2 );
    }

... which produces the following output image:

enter image description here

I want to get coordinates of four points (marked with green), is that possible?

kav
  • 5,363
  • 6
  • 28
  • 53
  • 2
    It's hard to tell for a computer if a contour is a rectangle. Try [`cv::approxPolyDP()`](http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=approxpoly#approxpolydp) on your largest contour with a reasonable epsilon parameter, it should produce a 4-sided, 4-cornered polygon for approximately-quadrangular shapes. – Iwillnotexist Idonotexist May 18 '15 at 18:41
  • try HoughLinesP function – Micka May 18 '15 at 18:42
  • There's [some stuff](http://stackoverflow.com/a/13532779/176769) here that's [ready for grabs](http://stackoverflow.com/a/26242885/176769). – karlphillip May 18 '15 at 19:00
  • 1
    Or you can just iterate on the vector, and write a little logic to find the `cv::Point` of the corners. – karlphillip May 18 '15 at 19:03

1 Answers1

1

Do you trying to find corners of rectangle in perspective?

You may want to try several solutions:

  1. Use HoughLines for line detection and find their intersection.
  2. Use Generalized Hough Transform
  3. Use Harris corner detector. But you need to filter extra corners.

For similar task I used following procedure (it works fine in my case): do cv::approxPolyDP for input contour with increasing epsilon parameter until it returns 4 or less polylines. If it returns 4 polylines you may get 4 corner points exact what you need. If it returns less than 4 polylines most probably something is wrong.

akarsakov
  • 1,956
  • 4
  • 20
  • 25