天天看点

基于Gabor+PCA+SVM的性别识别(1)

本文实现了基于人脸的性别识别。

人脸是重要的生物特征之一,人脸图像上蕴含了大量的信息,例如性别、年龄、人种、身份等。人脸的性别识别就是试图赋予计算机根据输入的人脸图像判断其性别的能力。本文讲的性别识别按如下四个步骤进行。

  1. 首先进行人脸识别,即在图像中找出人脸,把人脸区域提取出来作为训练对象。
  2. 第二步,运用Gabor小波对人脸提取特征。
  3. 第三步,运用PCA对Gabor提取的特征进行降维处理,除去冗余的数据,使特征更加简洁。
  4. 最后,用SVM对降维后的数据进行训练,得到一个性别分类器。

由于本文的侧重点在于性别识别,本文使用OpenCV自带人脸识别程序,来实现人脸的识别和提取人脸区域。从网上下载的BroID人脸数据库,但没找到完整的BroID数据库,只用了其中一部分,其中,man样本437,woman样本281。把提取的人脸统一相同的尺寸,本文人脸的尺寸为18X21。接下来用Gabor提取特征…………

请看:基于Gabor+PCA+SVM的性别识别(2):  http://www.cnblogs.com/xiaoming123abc/p/5078792.html

         基于Gabor+PCA+SVM的性别识别(3):  http://www.cnblogs.com/xiaoming123abc/p/5079116.html 

                                                                                                   原图像库

基于Gabor+PCA+SVM的性别识别(1)

                                                                                                        提取人脸

基于Gabor+PCA+SVM的性别识别(1)
#include "opencv2/core/core.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <fstream>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int CropImageCount = 0; //裁剪出来的样本图片个数

string face_cascade_name ="D:\\Program Files\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
string window_name = "人脸识别";

Mat detectAndDisplay( Mat);

int main()
{
    Mat image,ROI;
    //image = imread("pangzi.jpg");
	
	if( !face_cascade.load( face_cascade_name ) )
	  { 
        printf("[error] 无法加载级联分类器文件!\n");
        return -1; 
      }
	string ImgName;
	char saveName[256];  //裁剪出来的负样本图片文件名
	ifstream fin("woman.txt");//打开原始样本图片文件列表
	//ifstream fin("woman.txt");//打开原始样本图片文件列表
	//一行一行读取文件列表
	while(getline(fin,ImgName))
	{  
		cout<<"处理:"<<ImgName<<endl;
		//ImgName = "D:\\Mycode\\man\\" + ImgName+".png";
		ImgName = "D:\\Mycode\\woman\\" + ImgName+".png";
		//cout<<ImgName<<endl;
		image= imread(ImgName);//读取图片

        if(image.data ==0)
	   {
          printf("[error] 没有图片\n");
          return -5;
       }
      ROI= detectAndDisplay(image);
	  // cout<<ROI<<endl;
		//sprintf(saveName,"man%d.png",++CropImageCount);//生成裁剪出的样本图片的文件名
		sprintf(saveName,"woman%d.png",++CropImageCount);//生成裁剪出的样本图片的文件名
		imwrite(saveName, ROI);//保存文件
 }
    waitKey(0); 
	return 4;
}

Mat detectAndDisplay( Mat frame)
{
    std::vector<Rect> faces;
    Mat frame_gray,ROI;
	frame_gray= frame;
    cvtColor( frame, frame_gray, CV_BGR2GRAY );
    equalizeHist( frame_gray, frame_gray );

    face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    for( int i = 0; i < faces.size(); i++ )
	{
        Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
       // ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
     rectangle(frame,                //图像.
			     faces[i].tl(),             //矩形的一个顶点。
				 faces[i].br(),             //矩形对角线上的另一个顶点
				 Scalar(0, 255, 0),  //线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)
				  3);                 //组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形
     ROI=frame_gray(Rect(faces[i].tl().x,faces[i].tl().y,faces[i].width,faces[i].height));
	 resize(ROI,ROI,Size(21,18),0,0,CV_INTER_LINEAR);
	}
	//namedWindow("qq",2);
	//imshow( "qq", ROI );
   
 // imshow( window_name, frame );
	return ROI;
}
           

  

 程序下载:http://download.csdn.net/detail/u012507022/9378302

               (VS2010+opencv2.4.11)

继续阅读