Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Opencv Findcontours导致堆错误_C++_Opencv_Image Processing - Fatal编程技术网

C++ Opencv Findcontours导致堆错误

C++ Opencv Findcontours导致堆错误,c++,opencv,image-processing,C++,Opencv,Image Processing,我使用的是opencv 2.49 但我被“查找轮廓”功能卡住了好几个小时 当我在调试模式下运行程序时,错误框返回 调试断言失败 节目:。。。 文件f:\dd\vctools\crt\u bld\self\u x86\crt\src\dbgheap.c 电话号码:1322 例外情况:\ CrtIsValidHeapPoionter(pUserData) 这是我的函数 HRESULT OpenCVHelper::DrawHand(Mat* pImg) { vector<

我使用的是opencv 2.49

但我被“查找轮廓”功能卡住了好几个小时

当我在调试模式下运行程序时,错误框返回

调试断言失败

节目:。。。 文件f:\dd\vctools\crt\u bld\self\u x86\crt\src\dbgheap.c

电话号码:1322

例外情况:\ CrtIsValidHeapPoionter(pUserData)

这是我的函数

HRESULT OpenCVHelper::DrawHand(Mat* pImg)        
{
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    cvtColor(*pImg, *pImg, CV_RGBA2GRAY);
    //Canny(*pImg, *pImg, 30,50);
    threshold( *pImg, *pImg, 50, 255,THRESH_BINARY);  
    if(pImg->type() == CV_8UC1)
    {
        findContours( *pImg, contours, hierarchy, CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
    }
        for( int i = 0; i< contours.size(); i++ )
       {
        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255),  rng.uniform(0,255) );
        Scalar color( rand()&255, rand()&255, rand()&255 );
        drawContours( *pImg, contours, i, color, 2, 8, hierarchy, 0, Point() );
        }

//contours.clear();
//hierarchy.clear();
cvtColor(*pImg, *pImg, CV_GRAY2RGBA);
return S_OK;
HRESULT OpenCVHelper::DrawHand(Mat*pImg)
{
矢量等值线;
向量层次;
CVT颜色(*pImg,*pImg,CV_RGBA2GRAY);
//Canny(*pImg,*pImg,30,50);
阈值(*pImg,*pImg,50255,阈值二进制);
如果(pImg->type()==CV_8UC1)
{
findContours(*pImg、轮廓、层次、CV_RETR_列表、CV_链_近似_简单、点(0,0));
}
对于(int i=0;i
}

当我删除findcontour函数时,没有错误

当我使用findcontour时,会弹出上面显示的错误框

当我添加“cours.clear();hierarchy.clear();”这两行时,没有错误消息,但程序仍然崩溃

有人能帮忙吗


编辑1。我找到了导致堆损坏的分配器,即向量>轮廓;但我仍然不知道如何修复它

您拥有的代码应该可以工作。最有可能的失败点是当你找到一个空的图像。我说的不是未初始化的图像,而是用大小(0,0)初始化的图像。将您的“如果声明”更改为:

if ((pImg->type() == CV_8UC1) && (pImg->rows>0))
如果这样做无济于事,那么下一步就是验证压碎的函数确实是findContours而不是drawContours。崩溃可能在drawContours中,因为在删除findContours时未使用它

另外,不建议使用指向Mat的指针。Mat本身就是一个智能指针,它有参考计数器。这会产生各种讨厌的bug。

最后我发现了问题。。。 我的开发环境 window10 x64,英特尔i5(x64), 微软VisualStudio2010SP1

OpenCV 2.4.9(测试2.4.13得到相同的错误) 有些人解决了这个问题:直到opencv版本。2.4.9. ,这是我的密码

Mat Img = imread( src_image );
if (Img.rows == 0 || Img.cols == 0)
    return -1;

Mat ImgGray;
cvtColor( Img, ImgGray, CV_BGR2GRAY );
Mat threshold_output;
vector<vector<Point>>  contours; // << 
vector<Vec4i> hierarchy;

int blkSize = 5;
int nKernelSz = 3;
double dFactor = 2.0f;

adaptiveThreshold(ImgGray,threshold_output, 
100,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY_INV,blkSize, 5);

findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, 
CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
Mat Img=imread(src_图像);
if(Img.rows==0 | | Img.cols==0)
返回-1;
Mat ImgGray;
CVT颜色(Img、ImgGray、CV_bgr2灰色);
Mat阈值输出;

矢量等高线;//这就是你使用的代码吗?
颜色的重新定义是怎么回事?@alrikai啊,对不起,第一行被评论了。是的,我正在使用这段代码。它通过了条件(pImg->type()==CV_8UC1)和(&&(pImg->rows>0),但也会导致崩溃。调试器显示了“访问冲突读取位置”,我最好的猜测是它来自某种内存损坏。而腐败的最佳原因是使用指向Mat的指针。试着不用它们。