Reputation: 4032
I keep getting the same error, this line of code seems to be the problem i also add those line you told me
CvSeq *hand = cvHaarDetectObjects(img, cascade, hstorage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(100, 100));
Any great ideas?
// 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);
}
Upvotes: 0
Views: 2807
Reputation: 93468
The source of the problem is that you people don't code safely, forgetting to check the return of the calls every single time!
For instance, cvHaarDetectObjects()
may not find anything. Yes, it can detect... nothing! When you execute your application and the camera is turned on, when there is no HAND in front of the camera it is completely understandable that cvHaarDetectObjects()
doesn't detect anything, wouldn't you agree?
So the next call that operates on hand
might crash or continue to return nothing, as a way to say I FAILED, which is what I think cvGetSeqElem()
is doing, and then the first call that tries to dereference r
crashes your program.
The right way to do this is:
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 );
For a more complete example, check the FaceDetection demo.
Upvotes: 1
Reputation: 6204
I'm guessing that cvGetSeqElem()
returns NULL and you crash when you dereference r
. The solution is to check r for being NULL before you use it (same with any other function that can fail like cvHaarDetectObjects).
Upvotes: 1