1. 程式人生 > >OpenCV學習筆記(11):libfacedetection人臉檢測的配置與使用

OpenCV學習筆記(11):libfacedetection人臉檢測的配置與使用

1. 前言

libfacedetection庫是深圳大學的於仕琪老師釋出的開源人臉檢測庫,相比於OpenCV自帶的CascadeClassifier人臉檢測,無論在速度上還是精度上,都有巨大的優勢,是目前已知開源庫中最好用的一款。

本文通過學習libfacedetection庫中的example,進行人臉檢測程式的簡單實現。

2. 開發環境

  • OpenCV3.3
  • Windows 10 64位
  • Visual Studio 2013 Ultimate

3. 配置過程

3.1 libfacedetection的下載

這裡寫圖片描述

3.2 新建VS工程

這裡寫圖片描述

將下載好的libfacedetection資料夾中的幾個檔案拖拽到faceDetection工程的根目錄

,包括——

  • libfacedetect-x64.dll
  • libfacedetect-x64.lib
  • facedetect-dll.h

3.3 配置VS屬性

先配置opencv

這裡寫圖片描述

然後在連結器中配置libfacedetection的庫檔案,注意,64位的要配置成libfacedetect-x64.lib
這裡寫圖片描述

注意,libfacedetection庫中使用了比較底層的fopen函式,VS2013對fopen報編譯錯誤,需要設定如下——

這裡寫圖片描述

在C++命令列中新增 /D _CRT_SECURE_NO_WARNINGS

4. 原始碼示例

這是對libfacedetection檔案中example進行的修改使用,example提供了4個人臉檢測函式,分別是facedetect_frontal、facedetect_frontal_surveillance、facedetect_multiview、facedetect_multiview_reinforce,四個函式應該是對應不同的使用場景,效能有所不同,但引數型別完全一致,可以根據需要進行調整。

該程式碼使用了 facedetect_multiview 和 OpenCV自帶的CascadeClassifier進行比較,可以看出於仕琪老師的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; }

5. 結果對比

libfacedetection識別Tara女團結果

這裡寫圖片描述

OpenCV CascadeClassifier 識別Tara
這裡寫圖片描述