2014-05-15 68 views
0

我已經寫了一個面部和眼睛檢測代碼,以前工作正常,但現在由於unknwon原因,該程序有問題,並顯示一條消息,您的項目已停止工作...請看看下面的代碼和註釋行號碼的問題,並在程序停止有Open CV Haarcascades不能正常工作

#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); 


} 
+0

如果它「以前工作得很好」,我懷疑代碼決定去打破自己。你有沒有正確安裝和配置所有的依賴關係?你確定'haarcascade_frontalface_alt.xml'格式正確嗎?自上次使用您的代碼以來,庫可能已更新;什麼時候它以前工作正常? – Sveltely

+0

haarcascade_frontalface_alt.xml由opencv本身提供,我仍然擁有相同版本的opencv – Ahmed

+0

那麼,您使用的是什麼版本的OpenCV?如果你試圖捕獲一個opencv異常或什麼,會發生什麼? – Sveltely

回答

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文件以前(仍然不知道爲什麼會發生),但現在運轉。

+0

這是一個古老的線程,但這是愚蠢的。添加雙斜槓爲我工作。 – luckyging3r