Ask Your Question

Why findContour detects same edge twice?

asked 2019-02-14 16:11:15 -0500

Koralgoll gravatar image

I am trying to detect all contours from binary image but for some reason the findContour gives me contours that overlap themselves: image description

The source image looks like this: image description

I am using simple code for this, based on the OpenCV documentation:

// Find contours
Canny(src, canny_output, thresh, thresh * 2, 3);
findContours(canny_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_L1, Point(0, 0));

// Simplify contour and draw
Mat drawing(canny_output.size(), CV_8UC3, Scalar(255, 255, 255));
for (size_t i = 0; i < contours.size(); i++)
    approxPolyDP(contours[i], contours[i], arcLength(Mat(contours[i]), true) * simplicityRate, true);

    Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
    drawContours(drawing, contours, (int)i, color, 1, 8, hierarchy, 0, Point());
    for (rsize_t j = 0; j < contours[i].size(); ++j)
        circle(drawing, contours[i][j], 2, color, 3);
    putText(drawing, std::to_string(i), GetContourCenter(contours[i]), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(), 2);

Is it normal that the contour goes around the edge of the shape twice? Is that how the Canny and findContours work? Can I configure this through some parameters?

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2019-02-14 18:00:44 -0500

sjhalayka gravatar image

updated 2019-02-14 19:03:52 -0500

It’s because you’re running Canny. Did you stop to visualize the Canny result, to see why it finds two contours per shape?

edit flag offensive delete link more

answered 2019-02-15 02:59:37 -0500

Koralgoll gravatar image

You are right - it was because of the Canny function. When I removed it there are no more double contours per shape. For binary images the Canny is not needed I guess.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-02-14 16:11:15 -0500

Seen: 999 times

Last updated: Feb 14 '19