簡體   English   中英

使用OpenCV庫的C ++中未處理的異常

[英]Unhandled exception in C++ using OpenCV libraries

我是OpenCV的新手並嘗試了一些東西。 我想用網絡攝像頭檢測一只手,這是一個簡單的代碼。 但它給了我類似的東西: HaarCascade.exe中0x000000013f5b140b處的未處理異常:0xC0000005:訪問沖突讀取位置0x0000000000000004。

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

IplImage* img = 0;

CvHaarClassifierCascade *cascade;
CvMemStorage *cstorage;
CvMemStorage *hstorage;

void detectObjects( IplImage *img );
int key;

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

    // loads classifier for hand haar cascade
    char *filename = "haarcascade_hand.xml";
    cascade = ( CvHaarClassifierCascade* )cvLoad( "haarcascade_hand.xml", 0, 0, 0 );

    // setup memory buffer
    hstorage = cvCreateMemStorage( 0 );
    cstorage = cvCreateMemStorage( 0 );

    // initialize camera
    capture = cvCaptureFromCAM( 0 );

    // always check
    //assert( cascade && storage && capture );

    // create a window
    cvNamedWindow( "Camera", 1 );

    while(key!='q') {
        // captures frame and check every frame
        frame = cvQueryFrame( capture );
        if( !frame ) break;

        // detect objects and display video
        detectObjects (frame );

        // quit if user press 'q'
        key = cvWaitKey( 10 );
    }

    // free memory
    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(640,480), 8, 1 );
    IplImage *edge = cvCreateImage( cvSize(640,480), 8, 1 );

    // convert video image color
    cvCvtColor(img,gray,CV_BGR2GRAY);                       

    // set the converted image's origin
    gray->origin=1;                         

    // color threshold
    cvThreshold(gray,gray,100,255,CV_THRESH_BINARY);    

    // smooths out image
    cvSmooth(gray, gray, CV_GAUSSIAN, 11, 11);

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

    // detects circle
    CvSeq* circle =  cvHoughCircles(gray, cstorage, CV_HOUGH_GRADIENT, 1, gray->height/50, 5, 35);

    // draws circle and its centerpoint
    float* p = (float*)cvGetSeqElem( circle, 0 );
    cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(255,0,0), -1, 8, 0 );
    cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(200,0,0), 1, 8, 0 );
    px=cvRound(p[0]); 
    py=cvRound(p[1]);

    // displays coordinates of circle's center
    cout <<"(x,y) -> ("<<px<<","<<py<<")"<<endl;

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

    // draws red box around hand when detected
    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("Camera",img);
}

圖片: http//i.imgur.com/Dneiw.png

感謝您的所有回復。

cvLoad()有可能失敗,因為它沒有找到該文件。 這是一個問題,因為您稍后使用它,如果它是一個NULL指針,它可能會崩潰您的應用程序:

但除非你測試函數的返回,否則你永遠不會知道這個:

cascade = ( CvHaarClassifierCascade* )cvLoad( "haarcascade_hand.xml", 0, 0, 0 );
if (!cascade)
    // Print something to say it failed!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM