-2

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!

Cro
  • 338
  • 4
  • 18
  • Start by not using global variables. Repeat 471 times: "This problem was caused by my use of global variables. I shall not do that again." – Cheers and hth. - Alf Jan 11 '17 at 07:37
  • Did you miss calling `cvCreateMemStorage` for `storage` first ? – P0W Jan 11 '17 at 07:39
  • Sorry, POW's suggestion made another error, bad flag(parameter or structure field)(unrecognized or unsupported array type) – Cro Jan 11 '17 at 07:46
  • 1
    Why are you learning a **dead** api? – Miki Jan 11 '17 at 08:11
  • If you are just getting started with OpenCV, you should be learning the C++ API. The C API has been deprecated for the better part of a decade and is no longer supported. – beaker Jan 11 '17 at 18:38
  • On windows, programs using C++ API always says undefined reference. – Cro Jan 12 '17 at 08:35
  • @Cro You should probably fix that, then. – beaker Jan 14 '17 at 15:51
  • @beaker I've being trying to do that for three months and I get nothing, I use the new API in Python or CentOS but my science teacher wants a Windows 8 executable one. – Cro Jan 14 '17 at 15:55

1 Answers1

1

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

Vedranh13
  • 76
  • 4