#include "cv.h"
#include "highgui.h"
int main()
{
//读取摄像头
//声明IplImage指针
IplImage* pFrame = NULL;
CvCapture* pCapture = NULL;
IplImage* pgray=NULL;
IplImage* pcanny;
CvMemStorage* storage=0;
CvHaarClassifierCascade* cascade=0;
char cascadename[100]="D:\\OpenCV2.1\\data\\haarcascades\\haarcascade_frontalface_alt2.xml"; //训练好的级联表可以用于匹配
cascade = (CvHaarClassifierCascade*)cvLoad(cascadename,0,0,0);
storage = cvCreateMemStorage(0);
CvSeq* faces;
//窗口通过窗口名来标识
cvNamedWindow("video", 1);
pCapture = cvCaptureFromCAM(-1); //cvCreateCameraCapture(-1);//-1代表打开哪个摄像头都无所谓或只有一个摄像头
bool first=true;
while(pFrame = cvQueryFrame( pCapture ))
{
if(first)//初次循环,做些初始化
{
first=false;
//创建单通道图片
pgray=cvCreateImage(cvGetSize(pFrame),IPL_DEPTH_8U,1);
//这是canny边缘监测,和我原来做的差分得边缘不是一个级别的
pcanny=cvCreateImage(cvGetSize(pFrame),IPL_DEPTH_8U,1);
}
//将彩色图转化为2值图
cvCvtColor(pFrame,pgray,CV_BGR2GRAY);
cvCanny(pgray,pcanny,30,100,3); //canny边缘检测
/*********************************/
//检测人脸,
faces = cvHaarDetectObjects(pgray,cascade,
storage,1.1,2,0
,cvSize(30,30));
for(short i=0;i<(faces?faces->total:0);i++)
{
//得到人脸的区域
CvRect* rect = (CvRect*)cvGetSeqElem(faces,i);
//在人脸区域画个圆圈
CvPoint center; //中心
center.x=rect->x+rect->width/2;
center.y=rect->y+rect->height/2;
short radius = sqrt(pow(rect->width,2)//半径
+pow(rect->height,2))/2;
CvScalar color={{255,0,0}}; //画圈的颜色
cvCircle(pFrame,center,radius,color,3,8,0);
/**********************************/
}
//显示图像
//cvShowImage("video", pcanny);
cvShowImage("video", pFrame);
//延时 ,不然不会显示图像的,应该是扫描太快了
if(cvWaitKey(10)>= 0)
{
break;
}
}
//释放图像
cvReleaseImage(&pFrame);
cvReleaseImage(&pgray);
cvReleaseImage(&pcanny);
//销毁窗口
cvDestroyWindow("video");
//释放摄像设备
cvReleaseCapture(&pCapture);
return 0;
}