Opencv c ++人脸检测代码无法正常工作

时间:2015-06-15 17:48:27

标签: c++ opencv face-detection

我正在尝试运行以下代码进行面部检测,但

中存在问题
circle( image, center,Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360,Scalar( 255, 0, 255 ), 4, 8, 0 );

完整代码:

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include<highgui.h>

using namespace std;
using namespace cv;


int main(int argc, char** argv)
{
    IplImage* img = cvLoadImage( "1.jpg" );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Example1", img);

    Mat image=cvarrToMat(img);  
    CascadeClassifier face_cascade;
    face_cascade.load( "haarcascade_frontalface_alt2.xml" );

    std::vector<Rect> faces;
    face_cascade.detectMultiScale( image, faces, 1.1, 2,         0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    // Draw circles on the detected faces
    for( int i = 0; i < faces.size(); i++ )
    {
        Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
        circle( image, center,Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360,Scalar( 255, 0, 255 ), 4, 8, 0 );
    }


    imshow("Detected Face",image);
    waitKey(0);                   
    return 0;
}

2 个答案:

答案 0 :(得分:2)

请检查face_cascade.load( "haarcascade_frontalface_alt2.xml")的返回值。否则,如果找不到文件haarcascade_frontalface_alt2.xml,则不会发生任何事情。

我可能没有与您相同版本的opencv,但这里有一个基于您的代码,可以产生可接受的输出。可能需要更改包含文件和haarcascade_frontalface_alt2.xml的路径。

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include<opencv/highgui.h>

using namespace std;
using namespace cv;


int main(int argc, char** argv)
{
    IplImage* img = cvLoadImage( "2.jpg" );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Example1", img);

    Mat image=cvarrToMat(img);  
    CascadeClassifier face_cascade;
    // face_cascade.load( "haarcascade_frontalface_alt2.xml" );
    String face_cascade_name = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml";
    if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
    std::vector<Rect> faces;
    face_cascade.detectMultiScale( image, faces, 1.1, 2,         0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    // Draw circles on the detected faces
    for( int i = 0; i < faces.size(); i++ )
    {
        Point center=Point( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
        circle( image, center,faces[i].width/2,Scalar( 255, 0, 255 ), 4, 8, 0 );
    }


    imshow("Detected Face",image);

    try {
        imwrite("alpha.jpg", image);
    }
    catch (runtime_error& ex) {
        fprintf(stderr, "Exception converting image to jpg format: %s\n", ex.what());
        return 1;
    }

    waitKey(0);                   
    return 0;
}

上面的代码编译:

gcc -fPIC main.cpp -o main3 -lopencv_highgui -lopencv_imgproc -lopencv_core -lopencv_objdetect -I /usr/local/include

基于this image in the public domain,我得到了这个: enter image description here

答案 1 :(得分:1)

那是因为你没有正确地调用circle(),它不接受10个输入参数:

//! draws the circle outline or a solid circle in the image
CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius,
                       const Scalar& color, int thickness=1,
                       int lineType=8, int shift=0);

对于您的情况,您应该按如下方式调用它:

circle(image, center, faces[i].width*0.5, Scalar(255, 0, 255), 4, 8, 0);