打开CV Haarcascades无法正常工作

时间:2014-05-15 05:06:57

标签: c++ opencv face-detection haar-classifier

我已经编写了面部和眼部检测的代码,之前工作正常但现在由于原因,程序出现问题,并显示一条消息,表明您的项目已停止工作......请查看在下面的代码和注释的行号有问题,程序停在那里

#include <opencv2/core/core.hpp>
#include "opencv2/objdetect/objdetect.hpp"
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;
int main()
{

    /*Mat image=imread("im.jpg");
    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.
    //imwrite("im2.jpg",image);// write the image stored in object image as im2.jpg
    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;*/

    /*VideoCapture capture;  
    Mat frame;
    capture.open(0);
    if(capture.isOpened())
    {
        cout<<"success"<<endl;
        while(1)
        {
            bool flag=capture.read(frame); 
            namedWindow( "Display window", WINDOW_AUTOSIZE );
            if(flag)
                imshow( "Display window", frame );
            else
                cout<<"failed";
            waitKey(300);
        }

    }*/
    //face detection


    cout<<"i'm here";
    CascadeClassifier face_cascade("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
    CascadeClassifier eye_cascade("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml");
    Mat image=imread("subject3.jpg");
    cout<<"i'm here";
    Mat res;
    resize(image, res, Size(800, 600), 0, 0, INTER_LINEAR);
    Mat gray;
    cvtColor(res,gray,COLOR_BGR2GRAY);
    equalizeHist(gray, gray);
    std::vector<Rect> faces;
    std::vector<Rect> eyes;
    face_cascade.detectMultiScale(gray,faces, 1.1, 2,0 | CASCADE_SCALE_IMAGE, Size(30, 30));//problem
    Rect roi;
    cout<<faces.size();
    Mat crop;
    Mat grayEye;
    for(int i=0;i<faces.size();i++)
    {
        cout<<"flag2";
        roi.x=faces[i].x;
        roi.y=faces[i].y;
        roi.width=faces[i].width;
        roi.height=faces[i].height;
        Point pt1(faces[i].x, faces[i].y); 
        Point pt2((faces[i].x + faces[i].height), (faces[i].y + faces[i].width));
        crop=res(roi);
        resize(crop, crop, Size(300, 300), 0, 0, INTER_LINEAR);
        cvtColor(crop, grayEye, CV_BGR2GRAY);
        equalizeHist(grayEye, grayEye);
        eye_cascade.detectMultiScale(grayEye,eyes, 1.1, 2,0 | CASCADE_SCALE_IMAGE, Size(30, 30));
        cout<<endl<<eyes.size();
        for(int j=0;j<eyes.size();j++)
        {
            Point pnt1(eyes[j].x, eyes[j].y); 
            Point pnt2((eyes[j].x + eyes[j].height), (eyes[j].y + eyes[j].width));
            rectangle(crop, pnt1, pnt2, Scalar(0, 255, 0), 2);
        }
        rectangle(res, pt1, pt2, Scalar(0, 255, 0), 2);
    }
    namedWindow( "detected", WINDOW_AUTOSIZE );
    imshow("detected",res);
    waitKey(300);
    imshow("detected",crop);
    waitKey(0);


}

1 个答案:

答案 0 :(得分:0)

以下修改使程序正常工作(奇怪) 上一个代码:

CascadeClassifier face_cascade("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");


替换为:

CascadeClassifier face_cascade;
face_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");

我之前在构造函数中加载了xml文件(仍然不知道它为什么会发生),但它现在正在运行。