Opencv findcontours CV_RETR_EXTERNAL not working

Opencv findcontours CV_RETR_EXTERNAL not working

本文关键字:not working EXTERNAL RETR CV Opencv findcontours      更新时间:2023-10-16

我有这个图像:

编辑抱歉,我不得不删除这些图片!

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

这是代码:

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

我该如何解决?

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