Pregunta

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

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top