opencv_helloworld.exe中0x778715de处的未处理异常:0xC0000005:访问冲突读取位置0x00000004

时间:2012-03-29 21:49:19

标签: c++ opencv

我一直得到同样的错误,这行代码似乎是问题我也添加了你告诉我的那些行

CvSeq * hand = cvHaarDetectObjects(img,cascade,hstorage,1.2,2,CV_HAAR_DO_CANNY_PRUNING,cvSize(100,100));

有什么好主意吗?

// OpenCV_Helloworld.cpp : Defines the entry point for the console application.
// Created for build/install tutorial, Microsoft Visual C++ 2010 Express and OpenCV 2.1.0

#include "stdafx.h"

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include "math.h"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <sstream>

using namespace std;

IplImage* img = 0;

static CvHaarClassifierCascade* cascade = 0;
CvMemStorage *cstorage;
CvMemStorage *hstorage;

void detectObjects( IplImage *img );
int key;

int main( int argc, char** argv )
{
    CvCapture *capture;
    IplImage *frame;

    // Create a string that contains the cascade name
    const char* cascade_name ="C:/Users/Mario/Documents/Visual Studio 2010/Projects/opencv_helloworld/1256617233-2-haarcascade_hand.xml";
    cascade = ( CvHaarClassifierCascade* )cvLoad( cascade_name, 0, 0, 0 );

    if (cascade == 0)
    {
        printf("No se encontro el archivo xml\n");
        system("pause");
        return 0;
    }

    hstorage = cvCreateMemStorage( 0 );
    cstorage = cvCreateMemStorage( 0 );

    capture = cvCaptureFromCAM( 0 );

    cvNamedWindow( "camerawin", 1 );

    while(key!='q') {
        frame = cvQueryFrame( capture );
        if( !frame ) break;

        detectObjects (frame );

        key = cvWaitKey( 10 );
}

    cvReleaseCapture( &capture );
    cvDestroyAllWindows();
    cvReleaseHaarClassifierCascade( &cascade );
    cvReleaseMemStorage( &cstorage );
    cvReleaseMemStorage( &hstorage );

    return 0;
}

void detectObjects( IplImage *img )
{
    //int px;
    //int py;
    int edge_thresh = 1;
    IplImage *gray = cvCreateImage( cvSize(img->width,img->height), 8, 1);
    IplImage *edge = cvCreateImage( cvSize(img->width,img->height), 8, 1);

    cvCvtColor(img,gray,CV_BGR2GRAY);                       

    gray->origin=1;                         

    cvThreshold(gray,gray,100,255,CV_THRESH_BINARY);    

    cvSmooth(gray, gray, CV_GAUSSIAN, 11, 11);

    cvCanny(gray, edge, (float)edge_thresh, (float)edge_thresh*3, 5); 

    CvSeq *hand = cvHaarDetectObjects(img, cascade, hstorage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(100, 100));

    if (!hand)
    {
        printf("cvHaarDetectObjects error\n");
        system("pause");
    }

    if (hand->total <= 0)
    {
        printf("hand->total menor a cero\n");
        system("pause");
    }

    CvRect *r = ( CvRect* )cvGetSeqElem( hand, 0 );
    cvRectangle( img,cvPoint( r->x, r->y ),
        cvPoint( r->x + r->width, 
        r->y + r->height ),
        CV_RGB( 255, 0, 0 ), 
        1, 8, 0 );

    cvShowImage("camerawin",img);
}

2 个答案:

答案 0 :(得分:1)

我猜测cvGetSeqElem()返回NULL并且在取消引用r时崩溃。解决方案是在使用之前检查r是否为NULL(与cvHaarDetectObjects之类的任何其他可能失败的函数相同)。

答案 1 :(得分:1)

问题的根源在于你们人们没有安全编码,忘记每次都检查回电话!

例如,cvHaarDetectObjects()可能找不到任何内容。 是的,它可以检测到......没有!当您执行应用程序并打开相机时,如果相机前面没有HAND,cvHaarDetectObjects()无法完全理解什么都找不到,你不同意吗?

因此,对hand进行操作的下一个调用可能会崩溃或继续返回任何内容,作为一种说法我失败的方式,这就是我认为cvGetSeqElem()正在做的事情,然后尝试解除引用r的第一个调用崩溃了您的程序。

正确的方法是:

CvSeq *hand = cvHaarDetectObjects(img, cascade, hstorage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(100, 100));
if (!hand)
{
  // print error, cry or exit
}

if (hand->total <= 0)
{
  // print error, cry or exit
}

CvRect *r = ( CvRect* )cvGetSeqElem( hand, 0 );

有关更完整的示例,请查看FaceDetection演示。

相关问题