我刚开始使用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条目,它崩溃了。
欢迎任何建议
答案 0 :(得分:-1)
你可能想检查一下&#34; faces&#34;的大小。在调用face_cascade.detectMultiScale之后。我有时会注意到,&#34;面孔的大小&#34;非常大,但只有前几个条目实际上是有效的。其余的通常具有非常宽度或高度= 0,在这种情况下,您将需要遍历&#34; faces&#34;的每个条目,并在最后一次有效输入后停止。