OpenCV findContours destroys the original image

I am writing code that draws a circle, line and rectangle in a single channel blank image. After that, I just detect the outline in the image, and I get the entire outline correctly. But after finding the outline, my original image is distorted. Why is this happening? Anyone can help me solve this problem. And my code looks below.

using namespace cv;
using namespace std;
int main()
{

    Mat dst = Mat::zeros(480, 480, CV_8UC1);
    Mat draw= Mat::zeros(480, 480, CV_8UC1);

    line(draw, Point(100,100), Point(150,150), Scalar(255,0,0),1,8,0);
    rectangle(draw, Rect(200,300,10,15),  Scalar(255,0,0),1, 8,0); 
    circle(draw, Point(80,80),20, Scalar(255,0,0),1,8,0);

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    findContours( draw, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

     for( int i = 0; i< contours.size(); i++ )
    {
        Scalar color( 255,255,255);
        drawContours( dst, contours, i, color, 1, 8, hierarchy );
    }

    imshow( "Components", dst );
    imshow( "draw", draw );

    waitKey(0);
}

Source image

enter image description here

Distorted source after finding the contour

+3
source share
4 answers

The documentation clearly states that the original image changes when using findContours.

http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours

See the first note.

, findContours .

+11

findContours( draw.clone(), contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

+2

, , findContours, .

FindContours . drawContours .

: http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours

, /. , , . .

, . . , : " ".

I did not work much with findContours, but I never had a clear idea of ​​what I wanted. I should always use drawContours to get a good plot.

Otherwise, you can use the Canny function, which will give you edges instead of outlines.

+1
source

For me, the second image looks like what I expect as a result of the edge detection algorithm. I assume that the findContours function overwrites the original image with the result found.

Take a look here .

+1
source

Source: https://habr.com/ru/post/1536132/


All Articles