我刚开始使用OpenCV,我的设置是:
OpenCV 3.0
视觉工作室2013
我的问题是我试图检测图像中的人脸,但是功能调用detectMultiScale可以检测到很多人脸。
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, const char** argv)
{
//create the cascade classifier object used for the face detection
CascadeClassifier face_cascade;
//use the haarcascade_frontalface_alt.xml library
if (!face_cascade.load("haarcascade_frontalface_alt.xml"))
{
printf("Unable to load classifier XML");
return 0;
}
//setup video capture device and link it to the first capture device
//VideoCapture captureDevice;
//captureDevice.open(0);
//setup image files used in the capture process
Mat captureFrame;
Mat grayscaleFrame;
captureFrame = imread("Test.png", IMREAD_COLOR);
if (captureFrame.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return 0;
}
//create a window to present the results
namedWindow("outputCapture", 1);
//create a loop to capture and find faces
while (true)
{
//capture a new image frame
//captureDevice >> captureFrame;
//convert captured image to gray scale and equalize
cvtColor(captureFrame, grayscaleFrame, CV_BGR2GRAY);
equalizeHist(grayscaleFrame, grayscaleFrame);
//create a vector array to store the face found
std::vector<Rect> faces;
//find faces and store them in the vector array
face_cascade.detectMultiScale(grayscaleFrame, faces, 1.1, 3, 0 , Size(30, 30));
////draw a rectangle for all found faces in the vector array on the original image
//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(captureFrame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
//}
//print the output
imshow("outputCapture", captureFrame);
//pause for 33ms
waitKey(33);
}
return 0;
}
我想知道它的设置是否正确,或者CascadeClassifier是否只是不停止检测。当我查看数据时,其中一些数据在正确的位置,但是有268158156条目会崩溃。
任何的建议都受欢迎
最佳答案
您可能需要在调用face_cascade.detectMultiScale之后检查“面孔”的大小。我注意到有时,“面孔”的大小非常大,但实际上只有前几条有效。其余部分的宽度或高度通常为0,在这种情况下,您将需要遍历“面孔”的每个条目,并在最后一个有效条目之后停止。