3.按ESC确认矩形范围,销毁窗口并进行人眼检测
// FindEyeInVideo.cpp : 定义控制台应用程序的入口点。 //cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib cvcam.lib #include "stdafx.h" #include "cv.h" #include "highgui.h" #include "time.h" //鼠标拖动标志 bool check_line_state = false; IplImage* tmpImg = NULL; IplImage* grayFrame = NULL; CvRect ROI_rect; CvPoint p1,p2; //人眼检测序列 CvMemStorage* storage = cvCreateMemStorage(0); CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad("haarcascade_eye.xml", 0, 0, 0); void on_mouse4(int event, int x,int y,int flags,void* param) { int thickness = 2; if(event==CV_EVENT_LBUTTONDOWN) { //当前鼠标位置(x,y) ROI_rect.x = x; ROI_rect.y = y; check_line_state = true; } else if(check_line_state&&event==CV_EVENT_MOUSEMOVE)//鼠标状态 { cvCopy(grayFrame,tmpImg); p1=cvPoint(ROI_rect.x,ROI_rect.y); p2=cvPoint(x, y); ROI_rect.width = abs(p1.x - p2.x); ROI_rect.height = abs(p1.y - p2.y); cvRectangle(tmpImg,p1,p2,CV_RGB(0,255,150),thickness,CV_AA,0); cvShowImage("image",tmpImg); } else if(check_line_state&&event==CV_EVENT_LBUTTONUP) { check_line_state = false; } } void Decode(CONST char* filename) { CvCapture * capture = cvCreateFileCapture (filename) ; IplImage* frame = cvQueryFrame (capture); char filePath[200]={0}; int n = 0; int iConfirm = 0; CvSeq* eye; IplImage* img; clock_t start,finish; tmpImg = cvCreateImage(cvSize(frame->width, frame->height), frame->depth, 1); grayFrame = cvCreateImage(cvSize(frame->width, frame->height), frame->depth, 1); while (frame) { //转灰度 //反转图片 cvFlip(frame, NULL, 0); cvCvtColor(frame,grayFrame,CV_BGR2GRAY); //设置好人脸区域之后按ESC让程序自动进行人眼检测 if (0 == iConfirm) { cvShowImage("image",grayFrame); if (cvWaitKey(0) == 27) { iConfirm = 1; cvDestroyWindow("image"); start=clock(); } } else { //设置人脸ROI cvSetImageROI(grayFrame, ROI_rect); img = cvCreateImage(cvSize(ROI_rect.width, ROI_rect.height),grayFrame->depth, grayFrame->nChannels); cvCopy(grayFrame,img); //sprintf(filePath,"FaceResult//%d.bmp",n); //cvSaveImage(filePath, img); //在抠图区域搜索人眼 eye = cvHaarDetectObjects(img, cascade, storage, 1.2, 3, 0, cvSize(25,25)); if (2 != eye->total) goto NEXT; ROI_rect.x += ((CvRect*)cvGetSeqElem(eye, 0))->x - 20; ROI_rect.y += ((CvRect*)cvGetSeqElem(eye, 0))->y - 20; ROI_rect.width = ((CvRect*)cvGetSeqElem(eye, 0))->width + 40; ROI_rect.height = ((CvRect*)cvGetSeqElem(eye, 0))->height + 40; //cvResetImageROI(img); //cvSetImageROI(img, *((CvRect*)cvGetSeqElem( eye, 0))); //sprintf(filePath,"EyeResult//%d.bmp",n); //cvSaveImage(filePath, img); //cvResetImageROI(img); NEXT: cvResetImageROI(grayFrame); cvReleaseImage(&img); printf("%d\n",n++); } frame = cvQueryFrame (capture); } finish = clock(); double totaltime=(double)(finish-start)/CLOCKS_PER_SEC/n; printf("\n此程序的运行时间为%lf\n",totaltime); getchar(); cvReleaseCapture (&capture) ; cvReleaseImage(&tmpImg); cvReleaseImage(&grayFrame); } int main() { ROI_rect.x=ROI_rect.y=0; cvNamedWindow("image",CV_WINDOW_AUTOSIZE); cvSetMouseCallback("image",on_mouse4); Decode("..\\..\\DecodeFrame\\移动白天.avi"); }
原文地址:http://blog.csdn.net/lampqiu/article/details/42401215