C++ 中未处理的异常使用 OpenCV 库

发布于 2024-12-10 02:25:57 字数 3302 浏览 0 评论 0原文

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

https://i.sstatic.net/WQ8KH.png

感谢您的所有回复。

I'm new to OpenCV and trying some stuff. I want to detect a hand using a webcam and here is a simple code. But it gives me something like that:
Unhandled exception at 0x000000013f5b140b in HaarCascade.exe: 0xC0000005: Access violation reading location 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);
}

Image:
https://i.sstatic.net/WQ8KH.png

Thank you for all your responses.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拒绝两难 2024-12-17 02:25:57

cvLoad() 有可能因为找不到该文件而失败。这是一个问题,因为您稍后会使用它,如果它是 NULL 指针,它可能会使您的应用程序崩溃:

但是除非您测试函数的返回,否则您永远不会知道这一点:

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

There's a chance that cvLoad() failed because it didn't found the file. That's a problem because you use it later on, and if it's a NULL pointer it can crash your application:

But you'll never know this unless you test the return of the function:

cascade = ( CvHaarClassifierCascade* )cvLoad( "haarcascade_hand.xml", 0, 0, 0 );
if (!cascade)
    // Print something to say it failed!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文