我試圖運行這個簡單的人臉檢測程序,該程序在Visual Studio 2010中使用OpenCV 2.4.3中的haar cascades。現在每件事情都編譯得很好,但它似乎是haar級聯的xml文件沒有加載。我在Google上找到了很多的發現,但是找不到任何可行的解決方案。我嘗試了所有可能的方法使它們加載。我給出了絕對路徑,我複製了它們進入項目工作目錄,我使用雙反斜槓作爲絕對路徑中的分隔符,但它們不只是加載。我甚至從其他來源下載它們認爲可能與圖書館包含的內容可能無效或腐敗,但沒有成功。我知道我我在這裏犯了一些非常基本的錯誤。這裏是我使用的代碼。請幫我。謝謝。Haar Cascades的XML文件無法在Visual Studio 2010中使用OpenCV進行加載2.4.3
<pre><code>
#include "stdio.h"
#include "cv.h"
#include "highgui.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
void detectAndDisplay(Mat frame);
/** Global variables */
String face_cascade_name = "E:\Downloads\IDM Downloads\opencv\data\haarcascades\haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "E:\Downloads\IDM Downloads\opencv\data\haarcascades\haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
CascadeClassifier face_cascade;
/** @function main */
int main(int argc, const char** argv)
{
// printf();
CvCapture* capture;
Mat frame;
// face_cascade
//-- 1. Load the cascades
if(!face_cascade.load(face_cascade_name)){
printf("--(!)Error loading\n");
return -1;
}
if(!eyes_cascade.load(eyes_cascade_name)){ printf("--(!)Error loading\n"); return -1; }
//-- 2. Read the video stream
capture = cvCaptureFromCAM(0);
if(capture)
{
while(true)
{
frame = cvQueryFrame(capture);
//-- 3. Apply the classifier to the frame
if(!frame.empty())
{ detectAndDisplay(frame); }
else
{ printf(" --(!) No captured frame -- Break!"); break; }
int c = waitKey(10);
if((char)c == 'c') { break; }
}
}
return 0;
}
/** @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
//-- Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30));
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);
ellipse(frame, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
Mat faceROI = frame_gray(faces[i]);
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30));
for(int j = 0; j < eyes.size(); j++)
{
Point center(faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5);
int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);
}
}
//-- Show what you got
imshow(window_name, frame);
}
</code></pre>
The ouput command line window just prints
<pre><code>
--(!)Error loading
Press any key to continue . . .
這意味着xml文件沒有加載。 這裏是我的項目目錄列表。我已經把xml文件放在每個文件夾中,但仍然沒有成功。
OpenCVTest_2
|___Debug
|___ipch
|___opencvtest_2-365b6930
|___OpenCVTest_2
|___Debug
|___Release
|___Release
你怎麼會在年底解決這一問題? – twerdster