我是opencv的新手,我在开始时使用颜色,阈值并前进到面部跟踪。这是我用我的网络摄像头输入实现面部跟踪矩形的代码。问题是由“face_cascade.detectMultiScale”填充的向量的大小变为一个巨大的数字,导致一个未处理的异常。
我已经包含了所有库和级联xml文件,我没有看到我做过的任何错误。为了便于调试,我在多方式方法调用之后添加了一个cout。
#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
//creating the classifier object used for the face detection
CascadeClassifier face_cascade;
//use the haarcascade_frontface_alt.xml library
face_cascade.load("haarcascade_frontalcatface.xml");
//setup camera
VideoCapture captureDevice;
captureDevice.open(0);
//setup image files used in the capture process
Mat capFrame;
Mat grayscaleFrame;
//create a window to present the results
namedWindow("output", 1);
//create a loop to capture and find the faces
while (true)
{
//capture a new image frame
captureDevice >> capFrame;
//convert captured Image to grayscale and equalize
cvtColor(capFrame, grayscaleFrame, CV_BGR2GRAY);
equalizeHist(grayscaleFrame, grayscaleFrame);
//create a vector array to store the faces found
std::vector<Rect> faces;
//find faces and store them in the vector array
face_cascade.detectMultiScale(grayscaleFrame, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30));
cout << faces.size();
//draw a rectangle on the face
for (int i = 0; i < faces.size(); i++)
{
Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
Point pt2(faces[i].x, faces[i].y);
rectangle(capFrame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
}
//print the output
imshow("output", capFrame);
//pause for 33 ms
waitKey(33);
}
return 0;
}
所以控制台上的输出(由cout)是:
4292486918
我已经关注了很多面部跟踪教程,一切都与此类似。这是最简单的,甚至不起作用。 p.s:不要担心xml文件。我已经尝试了所有的xml文件,这不是问题。
提前谢谢