Ema*_*jar 2 opencv face-detection haar-classifier
有没有办法优化Haar-Cascade分类器进行人脸检测?
我已经创建了这个功能,它运行良好,但我仍然有一些照片有些问题:
void ImageManager::detectAndDisplay(Mat frame, CascadeClassifier face_cascade){
string window_name = "Capture - Face detection";
string filename;
std::vector<Rect> faces;
std::vector<Rect> eyes;
Mat frame_gray;
Mat crop;
Mat res;
Mat gray;
string text;
stringstream sstm;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
// Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
// Set Region of Interest
cv::Rect roi_b;
cv::Rect roi_c;
size_t ic = 0; // ic is index of current element
for (ic = 0; ic < faces.size(); ic++) // Iterate through all current elements (detected faces)
{
roi_c.x = faces[ic].x;
roi_c.y = faces[ic].y;
roi_c.width = (faces[ic].width);
roi_c.height = (faces[ic].height);
crop = frame_gray(roi_c);
faces_img.push_back(crop);
rectangle(frame, Point(roi_c.x, roi_c.y), Point(roi_c.x + roi_c.width, roi_c.y + roi_c.height), Scalar(0,0,255), 2);
}
imshow("test", frame);
waitKey(0);
cout << faces_img.size();
}
Run Code Online (Sandbox Code Playgroud)
框架:我必须分析的照片.
face_cascade:是从haar_cascade.xml创建的级联分类器
这是我用作算法测试的照片,结果如下:
结果非常好,面部都被正确检测到,但正如你所看到的,有三个误报我想删除.
提前致谢
在内部,CascadeClassifier会执行多次检测,并对这些检测进行分组.
minNeighbours (在detectMultiScale调用中)是大约相同位置的检测数量,无法计入有效检测,因此从当前的2增加到大约5个左右,直到您开始错过正数.