<span style="font-size:18px;">// Face_ASM.cpp : 定义控制台应用程序的入口点。 // //#include "stdafx.h" #include <iostream> #include <vector> //#include "stdafx.h" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/objdetect/objdetect.hpp" #pragma comment(lib,"asmlibraryD.lib") #include "asmfitting.h" using namespace std; using namespace cv; int main() { Point2D32f pt; int circle_x, circle_y, i, j; //IplImage* img = cvLoadImage("D:6.jpg"); Mat img = imread("D:6.jpg"); IplImage* img1; img1 = &IplImage(img); //*****************人脸检测**************************************************************************** CascadeClassifier face_cascade; //识别人脸的功能 if (!face_cascade.load("haarcascade_frontalface_alt.xml")) { cout << "Cascadeclassifier load false!" << endl; return -1; } vector<Rect> face_vec; //保存人脸的个数,vector保存到容器中 Mat pic_RGB, pic_GRAY; pic_RGB = img; cvtColor(pic_RGB, pic_GRAY, CV_RGB2GRAY);//进行灰度变换 equalizeHist(pic_GRAY, pic_GRAY);//进行直方图均衡化 face_cascade.detectMultiScale(pic_GRAY, face_vec, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(10, 10)); //检测数人脸 if (!face_vec.size()) cout << "检测不到人脸!" << endl; else cout << "检测到 " << face_vec.size() << "张人脸!" << endl; for (i = 0; i<(int)face_vec.size(); i++) { Point p1((int)face_vec[i].x, (int)face_vec[i].y); Point p2((int)(face_vec[i].x + face_vec[i].width), (int)(face_vec[i].y + face_vec[i].height*1.1)); rectangle(img, p1, p2, Scalar(255, 0, 0), 1, 8, 0); } //矩形框显示出人脸 //imshow("facedetect_test",pic_RGB); //***************ASM特征点匹配************************************************************************* if (!face_vec.size()) { cout << "没有检测到人脸,无法ASM特征点匹配" << endl; return -1; } //下面上是ASM模型对人脸中特征点的检测 asm_shape shape, detshape; asmfitting fit_asm; if (fit_asm.Read("my68-1d.amf") == false) { cout << "ASM特征点模版载入失败,退出!" << endl; return -1; } //模版位置初始化 detshape.Resize(2); for (i = 0; i<(int)face_vec.size(); i++) { detshape[0].x = (float)face_vec[i].x;//face_rect是cvRect型结构体的实体,cvRect结构体的成员为x,y,width,height detshape[0].y = (float)face_vec[i].y; detshape[1].x = (float)(face_vec[i].x + face_vec[i].width); detshape[1].y = (float)(face_vec[i].y + face_vec[i].height); InitShapeFromDetBox(shape, detshape, fit_asm.GetMappingDetShape(), fit_asm.GetMeanFaceWidth());//给人脸图像贴上ASM模版 bool b = fit_asm.ASMSeqSearch(shape, img1, 0, true, 30);//对准人脸特征点 //提取特征点坐标位置 // for(j=0;j<68;j++) // { //pt = shape[j]; // circle_x=cvRound(pt.x); // circle_y=cvRound(pt.y); // cvCircle( img,cvPoint(circle_x,circle_y), 2, cvScalar(255,0,0),1, 8, 0 ); // } pt = shape[27]; circle_x = cvRound(pt.x); circle_y = cvRound(pt.y); circle(img, cvPoint(circle_x, circle_y), 2, cvScalar(0, 255, 0), 1, 8, 0); pt = shape[29]; circle_x = cvRound(pt.x); circle_y = cvRound(pt.y); circle(img, cvPoint(circle_x, circle_y), 2, cvScalar(0, 255, 0), 1, 8, 0); pt = shape[34]; circle_x = cvRound(pt.x); circle_y = cvRound(pt.y); circle(img, cvPoint(circle_x, circle_y), 2, cvScalar(0, 255, 0), 1, 8, 0); pt = shape[32]; circle_x = cvRound(pt.x); circle_y = cvRound(pt.y); circle(img, cvPoint(circle_x, circle_y), 2, cvScalar(0, 255, 0), 1, 8, 0); } imshow("facedetect_test", img); if (cvWaitKey(0) >= 0) return 0; return 0; } </span>
<span style="font-size:18px;"> </span>
<span style="font-size:18px;"> </span>
代码下载地址:
http://download.csdn.net/detail/qq_18343569/9085929
点击打开链接