我正在使用 opencv 3.3.0 在 Visual Studio 2017 中编写。我从工作示例中获取了代码,但是有以前版本的 opencv
#include <iostream>
#include <stdio.h>
#include <opencv2\opencv.hpp>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat image;
image = imread("lena.jpg", CV_LOAD_IMAGE_COLOR);
imshow("cam", image);
// Load Face cascade (.xml file)
CascadeClassifier face_cascade;
face_cascade.load("С:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml");
// Detect faces
std::vector<Rect> faces;
/*face_cascade.detectMultiScale(image, faces,
1.1, 2, 0
//|CASCADE_FIND_BIGGEST_OBJECT
//|CASCADE_DO_ROUGH_SEARCH
//|CASCADE_DO_CANNY_PRUNING
| CASCADE_SCALE_IMAGE,
Size(30, 30));*/
face_cascade.detectMultiScale(image, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
/*// Draw circles on the detected faces
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(image, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
}*/
//namedWindow("Win");
waitKey(0);
return 0;
}
在 detectMultiscale 之前,使用 cvtColor 将图像转换为 GRAYSCALE,或者立即将其加载为 CV_LOAD_IMAGE_GRAY,还建议在检测器之前对图像应用 equalizeHist 过滤器