gpt4 book ai didi

c++ - Opencv轮廓错误

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

我想找到我的图像的轮廓,然后我想使用 openCV 绘制轮廓。我正在使用 VS 2012 和 OpenCV 2.4.5我编写了有关查找轮廓和绘制轮廓的示例代码。但是我堆积了那个可怕的错误 :) 对于任何帮助,我将不胜感激

void MyClass::findContoursAndDraw(cv::Mat image,int b,int g,int r)
{
findContours(image,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
for(int i=0;i<contours.size();i++)
{
int size=cv::contourArea(contours[i]);
if(size>500)
{
printf("%i \n",size);
drawContours(originalTemp,contours,i,cv::Scalar(b,g,r),2,8);
}

}

void MyClass::findContoursAndDrawFilled(cv::Mat image,int b,int g,int r)
{
findContours(image,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
for(int i=0;i<contours.size();i++)
{
int size=cv::contourArea(contours[i]);
if(size>3000)
{
printf("%i \n",size);
drawContours(originalImg,contours,i,cv::Scalar(b,g,r));
}

}
}

我的阈值和其他必要功能运行良好。但是我的程序堆积在寻找轮廓和绘制轮廓功能上。说:

 Unhandled exception at 0x00B3A52A (opencv_imgproc245d.dll) in OpencvTest.exe: 
0xC0000005: Access violation reading location 0xCDCDCDCD

最佳答案

我遇到了类似的问题。但是有两种隐含的情况。

第一个是Drawing issue,我照着官方文档中的方法解决了:

    findContours( src, contours, hierarchy, CV_RETR_CCOMP, 
CV_CHAIN_APPROX_SIMPLE );
// iterate through all the top-level contours,
// draw each connected component with its own random color
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
Scalar color( rand()&255, rand()&255, rand()&255 );
drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
}

这对我为每个轮廓绘制不同的颜色很有用。


编辑:此函数“drawContours”可以为轮廓和所有子项绘制颜色。要更好地理解这一点,请阅读 this .


第二个是轮廓上的迭代导航。由于某些未知原因,“findContours(...)”函数的输出“等高线”带来了大小为 0 或非常高的等高线(这就像内存抖动,一个非常大的数字)。我解决了使用条件使用轮廓的方式:

    for(int i=0;i<contours.size();i++)
{
if(contours[i].size() < 10000 && contours[i].size() > 0)
{
int size=cv::contourArea(contours[i]);
if(size>3000)
{
printf("%i \n",size);
drawContours(originalImg,contours,i,cv::Scalar(b,g,r));
}
}
}

我使用了条件“if(contours[i].size() < 10000 && contours[i].size() > 0)”,因为在任何情况下,当我们操纵“contours[i]”时,其中“contours[i].size()”为 0 或那个大数字,程序崩溃。 (“10000”是任意的,在我的情况下有效)。

关于c++ - Opencv轮廓错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17341812/

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