1. 程式人生 > >【opencv人臉識別1】從圖片中檢測人臉

【opencv人臉識別1】從圖片中檢測人臉

【opencv人臉識別一】從圖片中檢測人臉

本系列主要講述利用opencv實現人臉識別的相關知識,並給出實際程式碼。且循序漸進,由基礎到複雜,從最基本的圖片檢測人臉到視訊檢測、識別人臉,再到較大型人臉資料模型訓練、識別。下邊是本系列的主要目錄:

       注意:本系列的實驗平臺是VS2017+opencv3.4

1. 從圖片中檢測人臉

        利用opencv3.4庫中\opencv3_4\opencv\sources\data\haarcascades\資料夾下的haarcascade_frontalface_alt.xml檔案,可以實現人臉檢測。

        haarcascade_frontalface_alt.xml是已經訓練好的haar+adaboost人臉檢測模型。其中,opencv提供了4種haar的人臉檢測模型,具體可戳:

https://blog.csdn.net/u012679707/article/details/80377387

    從圖片中檢測人臉的程式碼:

// face_recog_from_picture.cpp: 定義控制檯應用程式的入口點。

#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main(int argc,char *argv[])
{
	Mat img = imread("she.jpg");
	namedWindow("display");
	imshow("display", img);
	
	/*********************************** 1.載入人臉檢測器  ******************************/
	// 建立級聯分類器
	CascadeClassifier cascade;
	// 載入訓練好的 人臉檢測器(.xml)
	const string path = "./xml/haarcascade_frontalface_alt.xml";
	if ( ! cascade.load(path))
	{
		cout << "cascade load failed!\n";
	}
	
	//計時
	double t = 0;
	t = (double)getTickCount();
	/*********************************** 2.人臉檢測 ******************************/
	vector<Rect> faces(0);
	cascade.detectMultiScale(img, faces, 1.1, 2, 0 ,Size(30,30));

	cout << "detect face number is :" << faces.size() << endl;
	/********************************  3.顯示人臉矩形框 ******************************/
	
	if (faces.size() > 0)
	{
		for (size_t i = 0;i < faces.size();i++)
		{
			rectangle(img, faces[i], Scalar(150, 0, 0), 3, 8, 0);

		}
	}
	else cout << "未檢測到人臉" << endl;

	t = (double)getTickCount() - t;  //getTickCount():  Returns the number of ticks per second.
	cout << "檢測人臉用時:" << t * 1000 / getTickFrequency() << "ms (不計算載入模型的時間)" << endl;

	namedWindow("face_detect");
	imshow("face_detect", img);
	while(waitKey(0)!='k') ;
	destroyWindow("display");
	destroyWindow("face_detect");
    return 0;
}

    執行結果:


    會發現,Selina的臉沒有被識別出來。這可能是由於Selina的臉傾斜過大。下邊我們想辦法實現檢測有一定傾斜度的臉。

附:

detectMultiScale原始碼解析
  */
    CV_WRAP void detectMultiScale( InputArray image,
                          CV_OUT std::vector<Rect>& objects,
                          double scaleFactor = 1.1,
                          int minNeighbors = 3, int flags = 0,
                          Size minSize = Size(),
                          Size maxSize = Size() );

    /** @overload
    @param image Matrix of the type CV_8U containing an image where objects are detected. 輸入影象
    @param objects Vector of rectangles where each rectangle contains the detected object, the
    rectangles may be partially outside the original image.   輸出檢測到的人臉矩形
    @param numDetections Vector of detection numbers for the corresponding objects. An object's number
    of detections is the number of neighboring positively classified rectangles that were joined
    together to form the object.  
    @param scaleFactor Parameter specifying how much the image size is reduced at each image scale.縮放因子,此處為每次縮小10%
    @param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have
    to retain it.  最小檢測鄰域
    @param flags Parameter with the same meaning for an old cascade as in the function
    cvHaarDetectObjects. It is not used for a new cascade.此引數用於老的版本中的cascade
    @param minSize Minimum possible object size. Objects smaller than that are ignored. 最小檢測目標的尺寸,小於這個尺寸的不檢測
    @param maxSize Maximum possible object size. Objects larger than that are ignored. If `maxSize == minSize` model is evaluated on single scale. 最大檢測目標的尺寸*/   
-------------------------------------------         END      -------------------------------------