libfacedetection
在GitHub上下载libfacedetection的最新库文件(https://github.com/ShiqiYu/libfacedetection)
#include"facedetect-dll.h" #include<iostream> #include<opencv2\opencv.hpp> #define DETECT_BUFFER_SIZE 0x20000 using namespace std; using namespace cv; void faceDetection(const Mat&image) { Mat gray; cvtColor(image, gray, CV_BGR2GRAY); int *pResults = NULL; unsigned char *pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE); int doLandmark = 1; pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*) (gray.ptr(0)), gray.cols, gray.rows, (int) gray.step, 1.2f, 2, 48, 0, doLandmark); printf("%d faces detected.\n", (pResults ? * pResults :0)); Mat result_multiview = image.clone(); ; for (int i = 0; i < (pResults ? * pResults :0); i++){ short *p = (( short*)(pResults + 1))+142 * i; int x = p[0]; int y = p[1]; int w = p[2]; int h = p[3]; int neighbors = p[4]; int angle = p[5]; printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x, y, w, h, neighbors, angle); rectangle(result_multiview, Rect(x, y, w, h), Scalar(0, 255, 0), 2); } imshow("face", result_multiview); waitKey(0); free(pResults); } int main() { Mat src = imread("test.png", IMREAD_COLOR); faceDetection(src); CascadeClassifier ccf; vector<Rect> faceBox; ccf.load("haarcascade_frontalface_default.xml"); ccf.detectMultiScale(src, faceBox, 1.1, 3, 0, Size(20, 20), Size(100, 100)); for (vector<Rect>::const_iterator i = faceBox.begin(); i != faceBox.end(); i++){ rectangle(src, ( * i), Scalar(0, 0, 255), 2); } imshow("cascade", src); waitKey(0); return 0; }