【问题标题】:Unhandled exception at 0x778715de in opencv_helloworld.exe: 0xC0000005: Access violation reading location 0x00000004opencv_helloworld.exe 中 0x778715de 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000004
【发布时间】:2012-03-30 08:45:38
【问题描述】:

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

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

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    问题的根源在于你们编码不安全,每次都忘记检查调用的返回!

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

    因此,在 hand 上运行的下一个调用可能会崩溃或继续不返回任何内容,以表示 I FAILED,这就是我认为 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 演示。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2011-03-22
        • 2011-09-28
        • 1970-01-01
        • 2013-08-22
        • 2018-12-05
        • 1970-01-01
        • 2014-10-26
        • 2013-04-05
        相关资源
        最近更新 更多