OpenCV findContours破坏源图像

Har*_*ris 3 opencv

我编写了一个在单个通道空白图像中绘制圆,线和矩形的代码.之后,我只是找出图像中的轮廓,我正确地得到了所有的轮廓.但在找到轮廓后,我的源图像变得扭曲.为什么会这样?任何人都可以帮我解决.我的代码如下所示.

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);
}
Run Code Online (Sandbox Code Playgroud)

来源图片

在此输入图像描述

找到轮廓后扭曲的光源

jno*_*cho 11

文档清楚地指出使用findContours时源图像会被更改.

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

见第一个注释.

如果需要源图像,则必须在副本上运行findContours.