1

I am trying to find a coordinates of a shape(s) in a white-black image. I am using findContours method for contour finding and approxPolyDP for optimizing them to a polygon. The shape in the input image (see below) is an processed text, I need to find a 4 corner polygon for each field, which would fit around this shape using less outside space. ApproxPolyDP function rarely gives me a 4 corners (despite changing parameters), which I need to use to apply perspective transform on an original image and skip the deskewing algorythm and to crop out the text. How can i find the best fitting 4 corner polygons for each field (not rectangles)? I could not find any proper tutorial on how to do that, is it really hard? Below I present my current code in java; desired result; input; current output. NOTE: I would highly appreciate if you could give me a method where HoughLines are not involved, this method is slow (for mobile phones; that's why I am asking this question), but if it is the only one possibility you know to get the result I need, please, post it, it would be appreciated.

Code for finding current shape(s):

Mat mask = new Mat(src.size(), CvType.CV_8UC3, new Scalar(0,0,0));
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(src, contours, new Mat(), Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);

for (int i = 0 ; i < contours.size() ; i++)
{
    int contourSize = (int)contours.get(i).total();

    MatOfPoint2f curContour2f = new MatOfPoint2f(contours.get(i).toArray());
    Imgproc.approxPolyDP(curContour2f, curContour2f, 0.04 * Imgproc.arcLength(curContour2f, true), true);
    contours.set(i, new MatOfPoint(curContour2f.toArray()));

    Imgproc.drawContours(mask, contours, i, new Scalar(0, 255, 0), 3);
}

Average input:

enter image description here

Desired result (it's not a rectangle, corners do not have to be 90 degrees, but here must be 4 of them):

enter image description here

Average current output:

enter image description here

Other output example: the input picture here was more detailed (with some gaps), so the output is much worse depending on what I want it to be. Polygons in other polygons is not a problem, but the main shape of a whole block has to much corners:

enter image description here

Thank you in advance.

Dainius Šaltenis
  • 1,644
  • 16
  • 29
  • I'm unclear on what the problem is with your average current output. It looks like it has 4 corners. – medloh Jan 22 '16 at 20:56
  • Look at the second picture below, it's the other output, which shows other, bad result, which occurs more frequently. I thought I'll show how it looks when everything is right and bad, but I thought at the same that it could confuse some people. I might take that one out... – Dainius Šaltenis Jan 22 '16 at 21:22
  • 1
    I had a similar problem a few years ago, and the best solution I found was hough lines, then finding the intersection points of the hough lines to find the corners. Once you have found all the intersections, you can test the 4 quadrants around the intersection to find the intersections that are actually corners. If that's too slow, then you might want to look into corner detection techniques, maybe there's some new better ones in the new version of opencv? – medloh Jan 25 '16 at 16:43

0 Answers0