#include "cv.h"
#include "highgui.h"
#include <list>
#include <vector>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <math.h>
#include <string.h>
using namespace cv;
using namespace std;
CvHaarClassifierCascade* load_object_detector( const char* cascade_path )
{
return (CvHaarClassifierCascade*)cvLoad( cascade_path );
}
/* takes image filename and cascade path from the command line */
int main()
{
char *file = new char;
cout << "请输入图像路径:"<<endl;
cin >> file;
IplImage* image;
image = cvLoadImage(file, 3 );
CvHaarClassifierCascade* cascade_eye = load_object_detector("haarcascade_eye_tree_eyeglasses.xml");
CvHaarClassifierCascade* cascade_face = load_object_detector("haarcascade_frontalface_alt2.xml");
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* faces = cvHaarDetectObjects(image, cascade_face, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING );
int i;
for( i = 0; i < faces->total; i++ )
{
CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i);
cvSetImageROI(image,face_rect);
int eye_left=0;
int eye_right=0;
int eye_hei=0;
///////////////////////////////////////////////////////////////////////////////////////////////////////
CvMemStorage* storage1 = cvCreateMemStorage(0); //创建动态内存
CvSeq* eyes;
int j;
eyes = cvHaarDetectObjects(image, cascade_eye, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING );
/* draw all the rectangles */
for( j = 0; j < eyes->total; j++ )
{
/* extract the rectanlges only */
CvRect eye_rect = *(CvRect*)cvGetSeqElem( eyes, j);
cvSetImageROI(image,cvRect(face_rect.x+eye_rect.x,face_rect.y+eye_rect.y,eye_rect.width,eye_rect.height));
//记录眼球中点
if(j==0)
{
eye_left = face_rect.x + eye_rect.x + eye_rect.width/2;
eye_hei = face_rect.y + eye_rect.y + eye_rect.height;
}
else if(j==1)
{
eye_right = face_rect.x + eye_rect.x + eye_rect.width/2;
int temp = face_rect.y + eye_rect.y + eye_rect.height;
if(temp>eye_hei)
eye_hei = temp;
}
//检测眼睛区域内的瞳孔
IplImage* img = cvCreateImage(cvSize(eye_rect.width,eye_rect.height), IPL_DEPTH_8U, 3);
IplImage* hue = cvCreateImage(cvSize(eye_rect.width,eye_rect.height), IPL_DEPTH_8U, 1);
cvCopy(image,img,0);
//计算色相平面
int p,q;
for(p=0;p<img->height;p++)
{
for(q=0;q<img->widthStep-1;q=q+3)
{
int r,g,b,max=0,min=300;
b = ((uchar*)(img->imageData+p*img->widthStep))[q];
g = ((uchar*)(img->imageData+p*img->widthStep))[q+1];
r = ((uchar*)(img->imageData+p*img->widthStep))[q+2];
if(r<g)
{
min=r;
max=g;
}
else
{
min=g;
max=r;
}
if(b>max)
max=b;
if(b<min)
min=b;
double h;
if(max==min)
h = 0;
else if(max==r && g>=b)
h = 60*double(g-b)/(max-min);
else if(max==r && g<b)
h = 60*double(g-b)/(max-min)+360;
else if(max==g)
h = 60*double(b-r)/(max-min)+120;
else if(max==b)
h = 60*double(r-g)/(max-min)+240;
int h_i = floor(h/360*255);
int thre = 50;
if(h_i < 0 || h_i>255)
h_i=0;//有误
if(h_i < thre)
h_i = 255;
else h_i = 0;
(hue->imageData+p*hue->widthStep)[q/3] = char(h_i);
}
}
cvErode(hue,hue,NULL,3);
CvMemStorage* eye_storage;
eye_storage = cvCreateMemStorage(0);
CvSeq* eye_contours = 0;
cvFindContours( hue, eye_storage, &eye_contours );
CvRect e_rect = cvBoundingRect(eye_contours,0);
//cvRectangle(image,cvPoint(e_rect.x,e_rect.y),cvPoint(e_rect.x+e_rect.width,e_rect.y+e_rect.height),CV_RGB(255,255,0),3);
CvPoint pupil;
pupil = cvPoint(e_rect.x+e_rect.width/2,e_rect.y+e_rect.height/2);
//检测眼睛区域中的角点
IplImage* img1 = cvCreateImage(cvSize(eye_rect.width,eye_rect.height), IPL_DEPTH_8U, 1);
IplImage* img2 = cvCreateImage(cvSize(eye_rect.width,eye_rect.height), IPL_DEPTH_32F, 1);
IplImage* img3 = cvCreateImage(cvSize(eye_rect.width,eye_rect.height), IPL_DEPTH_32F, 1);
cvCvtColor(image,img1,CV_BGR2GRAY);//将目标区域转为灰度
const int MAX_CORNERS = 140;//estimate a corner number
CvPoint2D32f corners[MAX_CORNERS] = {0};// coordinates of corners
int corner_count = MAX_CORNERS;
double quality_level = 0.1;//threshold for the eigenvalues
double min_distance = 5;//minimum distance between two corners
int eig_block_size = 3;//window size
int use_harris = false;//use 'harris method' or not
cvGoodFeaturesToTrack(img1,img2,img3,corners,&corner_count,quality_level,min_distance,NULL,eig_block_size,use_harris);
int zuo=eye_rect.width,you=0;
int zuo_k=0,you_k=0;
float yu = 0.25;
for(int k=0;k<corner_count;k++)
{
if(zuo>corners[k].x && corners[k].y > yu*eye_rect.height && corners[k].y < (1-yu)*eye_rect.height)
{
zuo=corners[k].x;
zuo_k=k;
}
if(you<corners[k].x && corners[k].y > yu*eye_rect.height && corners[k].y < (1-yu)*eye_rect.height)
{
you=corners[k].x;
you_k=k;
}
}
int x = zuo;
int y = corners[zuo_k].y;
cvCircle(image,cvPoint(x,y),3,CV_RGB(0,0,255),-1,CV_AA,0);
x = you;
y = corners[you_k].y;
cvCircle(image,cvPoint(x,y),3,CV_RGB(0,0,255),-1,CV_AA,0);
cvCircle(image,pupil,3,CV_RGB(255,0,0),-1,CV_AA,0);
cvRectangle( image, cvPoint(0,0),
cvPoint(eye_rect.width,eye_rect.height),
CV_RGB(0,255,0), 3 );
cvSetImageROI(image,face_rect);
cvReleaseImage( &img1 );cvReleaseImage( &img2 );cvReleaseImage( &img3 );
}
cvReleaseMemStorage( &storage1 );
cvRectangle( image, cvPoint(0,0),
cvPoint(face_rect.width,face_rect.height),
CV_RGB(255,0,0), 3 );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//估计鼻子区域
if(eye_left > eye_right)
{
int temp;
temp = eye_left;
eye_left = eye_right;
eye_right = temp;
}
Point p1,p2;
int height = face_rect.height/3;
int width = eye_right - eye_left;
if(width!=0)//width=0为检测不正常
{
cvSetImageROI(image,cvRect(eye_left,eye_hei,width,height));
IplImage* img11 = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 1);
IplImage* img12 = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 1);
cvCvtColor(image,img11,CV_BGR2GRAY);//将目标区域转为灰度
//腐蚀
cvErode(img11,img11,NULL,3);
int p,q;
int thre = 50;
for(p=0;p<img11->height;p++)
{
for(q=0;q<img11->widthStep;q++)
{
if(((uchar*)(img11->imageData+p*img11->widthStep))[q]<thre)
(img11->imageData+p*img11->widthStep)[q] = 0;
else
(img11->imageData+p*img11->widthStep)[q] = 255;
}
}
//cvShowImage( "test", img11 );
//cvWaitKey(0);
cvCopy(img11,img12,0);
//定位左右鼻孔
CvRect lef = cvRect(4,4,img11->width/2,img11->height-6);
CvRect righ = cvRect(img11->width/2-3,4,img11->width/2,img11->height-6);
cvSetImageROI(img11,lef);
CvMemStorage* g_storage;
g_storage = cvCreateMemStorage(0);
CvSeq* contours = 0;
cvFindContours( img11, g_storage, &contours );
CvRect rect1 = cvBoundingRect(contours,0);
cvSetImageROI(img12,righ);
g_storage = cvCreateMemStorage(0);
contours = 0;
cvFindContours( img12, g_storage, &contours );
CvRect rect2 = cvBoundingRect(contours,0);
没有合适的资源?快使用搜索试试~ 我知道了~
人脸特征点检测(瞳孔、眼角、鼻子、嘴角等)
共49个文件
dll:28个
jpg:4个
manifest:2个
5星 · 超过95%的资源 需积分: 30 399 下载量 188 浏览量
2012-10-27
08:35:22
上传
评论 19
收藏 13.93MB RAR 举报
温馨提示
OpenCV下人脸特征点检测,C++源代码,装了OpenCV可直接运行
资源推荐
资源详情
资源评论
收起资源包目录
人脸特征点检测.rar (49个子文件)
人脸特征点检测
Detecting
RayTracing.vcproj.sony-PC.sony.user 1KB
test.cpp 11KB
2.jpg 28KB
1.jpg 31KB
res_1.jpg 29KB
haarcascade_frontalface_alt2.xml 818KB
haarcascade_eye_tree_eyeglasses.xml 1.04MB
RayTracing.vcproj 4KB
Debug
test.obj 809KB
RayTracing.exe.embed.manifest 663B
RayTracing.exe.intermediate.manifest 621B
BuildLog.htm 7KB
RayTracing.exe.embed.manifest.res 728B
mt.dep 65B
res_2.jpg 27KB
Detecting.sln 895B
Detecting.ncb 18.94MB
Debug
opencv_objdetect231d.dll 1000KB
opencv_contrib231d.dll 1.02MB
opencv_imgproc231.dll 1.57MB
opencv_calib3d231.dll 728KB
opencv_ts231d.dll 864KB
opencv_features2d231.dll 859KB
RayTracing.ilk 519KB
opencv_objdetect231.dll 545KB
opencv_highgui231d.dll 1.38MB
opencv_legacy231d.dll 1.15MB
opencv_features2d231d.dll 1.48MB
opencv_contrib231.dll 536KB
RayTracing.pdb 1.18MB
opencv_core231.dll 1.64MB
opencv_flann231d.dll 819KB
opencv_gpu231.dll 217KB
opencv_flann231.dll 387KB
opencv_gpu231d.dll 427KB
opencv_ml231.dll 443KB
RayTracing.exe 50KB
opencv_ml231d.dll 726KB
opencv_highgui231.dll 872KB
tbb_debug.dll 201KB
opencv_calib3d231d.dll 1.19MB
opencv_ffmpeg.dll 7.07MB
opencv_ts231.dll 554KB
opencv_core231d.dll 2.23MB
opencv_imgproc231d.dll 2.22MB
opencv_video231d.dll 441KB
opencv_legacy231.dll 745KB
opencv_video231.dll 283KB
Detecting.suo 32KB
共 49 条
- 1
yinguoqun1963
- 粉丝: 2
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Linux知识体系复习
- 东华深度学习课后习题全部
- 基于STM32+ESP8266+OneNET平台的环境监测报警系统设计.rar
- 基于Django+Django -Rest-Framework实现的在线考试系统
- mt7628 breed 固件
- 基于STM32F103+ESP8266+机智云的室内室外各种环境监测系统设计
- 基于STM32+MPU9250的小型四轴飞行器设计-毕设开题报告&PPT&部分回答问题.rar
- 基于STM32+NB-IOT的风机环境监控系统设计
- STM32单片机FPGA毕设电路原理论文报告一种基于单片机的节能断电保护电路设计
- 基于python+django+vue开发的外卖点餐系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页