gpt4 book ai didi

OpenCV findContours 破坏源图像

转载 作者:太空宇宙 更新时间:2023-11-03 21:01:22 26 4
gpt4 key购买 nike

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

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

来源图片

enter image description here

找到轮廓后失真的源

最佳答案

文档明确指出使用 findContours 时源图像被更改。

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

见第一个注释。

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

关于OpenCV findContours 破坏源图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17162559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com