性别识别(上)

				版权声明:本文为博主原创文章,未经博主允许不得转载。					https://blog.csdn.net/u012507022/article/details/50979001				</div>
							            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
					<div class="htmledit_views" id="content_views">

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

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

  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 

                                                                                                   原图像库

 

                                                                                                        提取人脸


  
  
  1. #include "opencv2/core/core.hpp"
  2. #include "opencv2/objdetect/objdetect.hpp"
  3. #include "opencv2/highgui/highgui.hpp"
  4. #include "opencv2/imgproc/imgproc.hpp"
  5. #include <fstream>
  6. #include <iostream>
  7. #include <stdio.h>
  8. using namespace std;
  9. using namespace cv;
  10. int CropImageCount = 0; //裁剪出来的样本图片个数
  11. string face_cascade_name = "D:\\Program Files\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
  12. CascadeClassifier face_cascade;
  13. string window_name = "人脸识别";
  14. Mat detectAndDisplay( Mat);
  15. int main()
  16. {
  17. Mat image,ROI;
  18. //image = imread("pangzi.jpg");
  19. if( !face_cascade.load( face_cascade_name ) )
  20. {
  21. printf( "[error] 无法加载级联分类器文件!\n");
  22. return -1;
  23. }
  24. string ImgName;
  25. char saveName[ 256]; //裁剪出来的负样本图片文件名
  26. ifstream fin("woman.txt"); //打开原始样本图片文件列表
  27. //ifstream fin("woman.txt");//打开原始样本图片文件列表
  28. //一行一行读取文件列表
  29. while(getline(fin,ImgName))
  30. {
  31. cout<< "处理:"<<ImgName<< endl;
  32. //ImgName = "D:\\Mycode\\man\\" + ImgName+".png";
  33. ImgName = "D:\\Mycode\\woman\\" + ImgName+ ".png";
  34. //cout<<ImgName<<endl;
  35. image= imread(ImgName); //读取图片
  36. if(image.data == 0)
  37. {
  38. printf( "[error] 没有图片\n");
  39. return -5;
  40. }
  41. ROI= detectAndDisplay(image);
  42. // cout<<ROI<<endl;
  43. //sprintf(saveName,"man%d.png",++CropImageCount);//生成裁剪出的样本图片的文件名
  44. sprintf(saveName, "woman%d.png",++CropImageCount); //生成裁剪出的样本图片的文件名
  45. imwrite(saveName, ROI); //保存文件
  46. }
  47. waitKey( 0);
  48. return 4;
  49. }
  50. Mat detectAndDisplay( Mat frame)
  51. {
  52. std:: vector<Rect> faces;
  53. Mat frame_gray,ROI;
  54. frame_gray= frame;
  55. cvtColor( frame, frame_gray, CV_BGR2GRAY );
  56. equalizeHist( frame_gray, frame_gray );
  57. face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size( 30, 30) );
  58. for( int i = 0; i < faces.size(); i++ )
  59. {
  60. Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
  61. // ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
  62. rectangle(frame, //图像.
  63. faces[i].tl(), //矩形的一个顶点。
  64. faces[i].br(), //矩形对角线上的另一个顶点
  65. Scalar( 0, 255, 0), //线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)
  66. 3); //组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形
  67. ROI=frame_gray(Rect(faces[i].tl().x,faces[i].tl().y,faces[i].width,faces[i].height));
  68. resize(ROI,ROI,Size( 21, 18), 0, 0,CV_INTER_LINEAR);
  69. }
  70. //namedWindow("qq",2);
  71. //imshow( "qq", ROI );
  72. // imshow( window_name, frame );
  73. return ROI;
  74. }

  

转自:https://blog.csdn.net/u012507022/article/details/50979001
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值