本文介绍了OpenCV findContours破坏源图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I writing a code that draw circle, line and rectangle in a single channel blank image. After that I just find out the contour in the image and I am getting all the contour correctly. But after finding the contour my source image is getting distorted. Why this happening ? Any one can help me to solve it out. And my code look like 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);
}

源图像

找到轮廓后,源变形了

推荐答案

文档明确指出使用findContours时源图像已更改.

Documentation clearly states that source image is altered when using findContours.

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

请参阅第一个音符.

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

If you need the source image, you have to run findContours on copy.

这篇关于OpenCV findContours破坏源图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 12:27