Opencv findcontours CV_RETR_EXTERNAL无效

时间:2015-04-01 16:14:57

标签: c++ opencv canny-operator opencv-contour

我有这张图片:

修改 对不起,但我不得不删除图片!

我需要提取非黑色图片的轮廓,所以我使用了findcontour和CV_RETR_EXTERNAL参数,但我得到了这个:

以下是代码:

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));

        }

我该如何解决?

1 个答案:

答案 0 :(得分:1)

只需添加具有最小阈值的二值化,然后删除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);