0

I am trying to detect faces using opencv2 in eclipse. I am using the following program for the same...

#include <cv.h>
#include"opencv2/highgui/highgui.hpp"
#include"opencv2/core/core.hpp"
#include"opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;

CvRect detectFaceInImage(IplImage *inputImg, CvHaarClassifierCascade* cascade);
String face_cascade_name = "src/haarcascade_frontalface_alt.xml";
CvHaarClassifierCascade* cascade;

int main(int argc, const char* argv[])
{
     CvCapture* capture =0;

     capture = cvCaptureFromCAM(0);
     IplImage* inputImg=0;

    if( !cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return   -1; };

while(true)
{
    inputImg = cvQueryFrame(capture);
    if(!inputImg) break;

    namedWindow("My_capture",CV_WINDOW_AUTOSIZE);

    CvRect outputImg;

    outputImg = detectFaceInImage(inputImg, cascade);
}

 return 0;
}
// Perform face detection on the input image, using the given Haar Cascade.
// Returns a rectangle for the detected region in the given image.
CvRect detectFaceInImage(IplImage *inputImg, CvHaarClassifierCascade* cascade)
{
    // Smallest face size.
    CvSize minFeatureSize = cvSize(20, 20);
    // Only search for 1 face.
    int flags = CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_DO_ROUGH_SEARCH;
    // How detailed should the search be.
    float search_scale_factor = 1.1f;
    IplImage *detectImg;
    IplImage *greyImg = 0;
    CvMemStorage* storage;
    CvRect rc;
    double t;
    CvSeq* rects;
    CvSize size;
    int ms, nFaces;
    storage = cvCreateMemStorage(0);
    cvClearMemStorage( storage );
    // If the image is color, use a greyscale copy of the image.
    detectImg = (IplImage*)inputImg;
    if (inputImg->nChannels > 1)
    {
        size = cvSize(inputImg->width, inputImg->height);
        greyImg = cvCreateImage(size, IPL_DEPTH_8U, 1 );
        cvCvtColor( inputImg, greyImg, CV_BGR2GRAY );
        detectImg = greyImg;    // Use the greyscale image.
    }

    // Detect all the faces in the greyscale image.
    t = (double)cvGetTickCount();
    rects = cvHaarDetectObjects( detectImg, cascade, storage,search_scale_factor, 3, flags, minFeatureSize);
    t = (double)cvGetTickCount() - t;
    ms = cvRound( t / ((double)cvGetTickFrequency() * 1000.0) );
    nFaces = rects->total;
    printf("Face Detection took %d ms and found %d objects\n", ms, nFaces);
        // Get the first detected face (the biggest).
    if (nFaces > 0)
        rc = *(CvRect*)cvGetSeqElem( rects, 0 );
    else
        rc = cvRect(-1,-1,-1,-1);   // Couldn't find the face.
        if (greyImg)
        cvReleaseImage( &greyImg );
        cvReleaseMemStorage( &storage );
        //cvReleaseHaarClassifierCascade( &cascade );
return rc;  // Return the biggest face found, or (-1,-1,-1,-1).
}

But I am getting this error...

../src/test3.cpp:28:15: error: request for member ‘load’ in ‘cascade’, which is of non-class type ‘CvHaarClassifierCascade*’

I have already added the "haarcascade_frontalface_alt.xml" in the source folder. When i am defining cascade as CascadeClassifier, it show some compatibility error.

I am actually new to opencv... so plz help

1 Answer 1

1

please don't use the outdated c-api, it won't be supported in near future.

look here for a more up-to-date version

btw, if you got a pointer to something, you need to access it as : something-> , not something.

Sign up to request clarification or add additional context in comments.

2 Comments

hey, i am able to do the face detection with the help of the program mentioned in the link provided by you already. But i really want to figure out the problem with the above problem...can u please elaborate whatever you want to convey...I'll be highly grateful to you.
CvHaarClassifierCascade* cascade; if( !cascade->load( face_cascade_name ) ) you have a pointer to the CvHaarClassifierCascade, so you have to use -> instead of . . also, the pointer is never instanciated, that should have been CvHaarClassifierCascade* cascade = createHaarClassifierCascade(); or similar, i forgot. again, stick to the new, c++ api and have fun!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.