c++opencvcontourcanny-operator

Opencv findcontours CV_RETR_EXTERNAL not working


I've this image:

EDIT Sorry but I had to remove the images!

I need to extract the contour of the non-black picture, so I used findcontour with the CV_RETR_EXTERNAL parameter, but I obtain this:

Here's the code:

static Mat canny_output, grey,draw;
        vector<vector<Point>> contours;
        cvtColor(final_img, grey, CV_BGR2GRAY);
        Canny(grey, canny_output, 100, 200);
        findContours(canny_output, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

        draw = Mat::zeros(canny_output.size(), CV_8UC3);


        for (size_t i = 0; i < contours.size(); i++)
        {
            drawContours(draw, contours, i, Scalar(255, 0, 0));

        }

how can I resolve?


Solution

  • Simply add a binarization with minimal threshold, and remove Canny:

    cvtColor(final_img, grey, CV_BGR2GRAY);
    //Threshold=1: very low value, anyway the rest of the image is pure black
    threshold(grey, binary, 1, 255, CV_THRESH_BINARY);
    findContours(binary, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);