I'm getting started with OpenCV and it's C API, I've written this face detection program but it cannot be executed, I'm using OpenCV 2.4.9, here's my code:
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace cv;
using namespace std;
String faceCascade_name = "haarcascade_frontalface_default.xml";
char* name = "res";
CvHaarClassifierCascade* faceCascade;
CvMemStorage* storage = 0;
int detectAndDisplay( IplImage* frame );
int main(int argc, char** argv){
CvCapture* capture;
capture = cvCaptureFromCAM(CV_CAP_ANY);
faceCascade = (CvHaarClassifierCascade*)cvLoad( faceCascade_name.c_str() );
IplImage* frame;
cvNamedWindow(name, 1);
if( !faceCascade){
printf("--(!)Error loading face cascade\n");
return -1;
}
if ( !capture ) {
printf("--(!)Error opening video capture\n");
return -1;
}
while( true ){
frame = cvQueryFrame(capture);
detectAndDisplay(frame);
if(cvWaitKey(30) == 27){
break;
}
}
return 0;
}
int detectAndDisplay( IplImage* frame ){
int scale = 1;
int i;
CvPoint pt1, pt2;
cvClearMemStorage(storage);
CvSeq* faces = cvHaarDetectObjects( &frame, faceCascade, storage, 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/, cvSize(24, 24) );
for( i = 0; i < (faces ? faces->total : 0); i++ ){
// Create a new rectangle for drawing the face
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
// Draw the rectangle in the input image
cvRectangle( &frame, pt1, pt2, CV_RGB(255,0,0), 3 );
}
cvShowImage(name, &frame);
return 0;
}
But, the following error occurred when I executed the compiled program:
OpenCV Error: Null pointer () in cvClearMemStorage, file C:\builds\2_4_PackSlave-win32-vc12-shared\opencv\modules\core\src\datastructs.cpp
Plz tell me how to fix it, if you have more time, plz finish the program for me!
Use the new API. From the OpenCV 2.4 Docs on CvMemStorage:
The section describes OpenCV 1.x API for creating growable sequences and other dynamic data structures allocated in CvMemStorage. If you use the new C++, Python, Java etc interface, you will unlikely need this functionality. Use std::vector or other high-level data structures
That said, if you want to continue using CvMemStorage, you need to first allocate memory to the structure. According to the docs:
A new memory buffer that may be allocated explicitly by MemStorageAlloc() function or implicitly by higher-level functions, such as SeqPush(), GraphAddEdge() etc
For more information, see http://docs.opencv.org/2.4/modules/core/doc/dynamic_structures.html
Also, when creating the structure, you need to pass in an integer to cvCreateMemStorage. Again, docs:
CvMemStorage* cvCreateMemStorage(int block_size=0 ) Parameters: block_size – Size of the storage blocks in bytes. If it is 0, the block size is set to a default value - currently it is about 64K.
Not doing so could be the cause of your bad parameter error