Ask Your Question

Revision history [back]

Why findContour detects same edge twice?

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?