說到人臉檢測,首先要了解haar特征分類器。haar特征分類器說白了就是一個個的xml文件,不同的xml里面描述人體各個部位的特征值,比如人臉、眼睛等等。opencv3.2.0中提供了如下特征文件:
haarcascade_eye.xml
haarcascade_eye_tree_eyeglasses.xml
haarcascade_frontalcatface.xml
haarcascade_frontalcatface_extended.xml
haarcascade_frontalface_alt.xml
haarcascade_frontalface_alt_tree.xml
haarcascade_frontalface_alt2.xml
haarcascade_frontalface_default.xml
haarcascade_fullbody.xml
haarcascade_lefteye_2splits.xml
haarcascade_licence_plate_rus_16stages.xml
haarcascade_lowerbody.xml
haarcascade_profileface.xml
haarcascade_righteye_2splits.xml
haarcascade_russian_plate_number.xml
haarcascade_smile.xml
haarcascade_upperbody.xml
通過加載不同的特征文件,就能達到相應的檢測效果。
opencv3.2.0中detectmultiscale函數參數說明:
detectmultiscale(mat image, matofrect objects, double scalefactor, int minneighbors, int flags, size minsize, size maxsize)
image:待檢測圖片,一般為灰度圖(提高效率)
objects:被檢測物體的矩形框向量組
scalefactor:前后兩次相繼的掃描中,搜索窗口的比例系數。默認為1.1即每次搜索窗口依次擴大10%
minneighbors:構成檢測目標的相鄰矩形的最小個數(默認為3個)
flags:要么使用默認值,要么使用cv_haar_do_canny_pruning,如果設置為cv_haar_do_canny_pruning,那么函數將會使用canny邊緣檢測來排除邊緣過多或過少的區域,因此這些區域通常不會是人臉所在區域
minsize:得到的目標區域的最小范圍
maxsize:得到的目標區域的最大范圍
人臉檢測示例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import org.opencv.core.core; import org.opencv.core.mat; import org.opencv.core.matofrect; import org.opencv.core.point; import org.opencv.core.rect; import org.opencv.core.scalar; import org.opencv.imgcodecs.imgcodecs; import org.opencv.imgproc.imgproc; import org.opencv.objdetect.cascadeclassifier; public class facedetect { public static void main(string[] args) { // todo auto-generated method stub system.loadlibrary(core.native_library_name); system.out.println( "\nrunning facedetector" ); cascadeclassifier facedetector = new cascadeclassifier(); facedetector.load( "c:\\program files\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml" ); mat image = imgcodecs.imread( "f:\\1114.jpg" ); matofrect facedetections = new matofrect(); facedetector.detectmultiscale(image, facedetections); system.out.println(string.format( "detected %s faces" , facedetections.toarray().length)); for (rect rect : facedetections.toarray()) { imgproc.rectangle(image, new point(rect.x, rect.y), new point(rect.x + rect.width, rect.y + rect.height), new scalar( 0 , 255 , 0 )); } string filename = "f:\\ouput.jpg" ; imgcodecs.imwrite(filename, image); } } |
源圖像與結果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/m1109048058/article/details/78192269