#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <math.h>
#include <cv.h>
#include <cstdio>
#include <cstdlib>
#include<highgui.h>
#include<windows.h>
void facedetection(int x);
void avatar0();
void avatar1();
int main ()
{
CvCapture *capture=cvCreateCameraCapture(0);
cvNamedWindow("camera",CV_WINDOW_AUTOSIZE);
IplImage *image;
char file_name[15];
int i;
while(1)
{
for(i=0;i<1000;i++)
{
image=cvQueryFrame(capture);
if(!image)
break;
cvShowImage("camera",image);
char c=cvWaitKey(20);
sprintf(file_name,"G:\\%d.jpg",i);//G盘根目录下生成
cvSaveImage(file_name,image);
if(c==27)
{
cvReleaseCapture(&capture);
cvDestroyWindow("hh");
exit(0);
}//按esc退出
facedetection(i);
}
}
}
void facedetection(int x)
{
int radius,t;
char fileName[50];
//for (int x = 1; x <= 300; x++)
//{
sprintf_s(fileName, "G:\\iii\\%d.jpg", x);
// 加载Haar特征检测分类器
// haarcascade_frontalface_alt.xml系OpenCV自带的分类器 下面是我机器上的文件路径
if(x%75==0)
{
const char *pstrCascadeFileName1 = "E:\\Opencv249\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
CvHaarClassifierCascade *pHaarCascade1 = NULL;
pHaarCascade1 = (CvHaarClassifierCascade*)cvLoad(pstrCascadeFileName1);
// 载入图像
const char *pstrImageName1 =fileName;
IplImage *pSrcImage1 = cvLoadImage(pstrImageName1, CV_LOAD_IMAGE_UNCHANGED);
IplImage *pGrayImage1 = cvCreateImage(cvGetSize(pSrcImage1), IPL_DEPTH_8U, 1);
cvCvtColor(pSrcImage1, pGrayImage1, CV_BGR2GRAY);
// 人脸识别与标记
if (pHaarCascade1 != NULL)
{
CvScalar FaceCirclecolors[] =
{
{{255, 0, 0}}
};
CvMemStorage *pcvMStorage1 = cvCreateMemStorage(0);
cvClearMemStorage(pcvMStorage1);
// 识别
DWORD dwTimeBegin, dwTimeEnd;
dwTimeBegin = GetTickCount();
CvSeq *pcvSeqFaces = cvHaarDetectObjects(pGrayImage1, pHaarCascade1, pcvMStorage1);
dwTimeEnd = GetTickCount();
printf("正脸个数: %d 识别用时: %d ms\n", pcvSeqFaces->total, dwTimeEnd - dwTimeBegin);
t=(pcvSeqFaces->total);
// printf("**** %d ****\n",t);
// 标记
for(int i = 0; i <pcvSeqFaces->total; i++)
{
CvRect* r = (CvRect*)cvGetSeqElem(pcvSeqFaces, i);
CvPoint center;
center.x = cvRound((r->x + r->width * 0.5));
center.y = cvRound((r->y + r->height * 0.5));
radius = cvRound((r->width + r->height) * 0.25);
printf("半径:%d\n",radius);
if(radius>50)
{
cvCircle(pSrcImage1, center, radius, FaceCirclecolors[0], 2);
}
}
cvReleaseMemStorage(&pcvMStorage1);
const char *pstrWindowsTitle1 = "人脸识别 ";
cvNamedWindow(pstrWindowsTitle1, CV_WINDOW_AUTOSIZE);
cvShowImage(pstrWindowsTitle1, pSrcImage1);
if(radius>50)
{
avatar0();
}
cvDestroyWindow("人脸识别 ");
//cvWaitKey(0);
cvDestroyWindow(pstrWindowsTitle1);
}
//***********************************
if(t==0)
{
const char *pstrCascadeFileName = "E:\\Opencv249\\opencv\\sources\\data\\haarcascades\\haarcascade_profileface.xml";
CvHaarClassifierCascade *pHaarCascade = NULL;
pHaarCascade = (CvHaarClassifierCascade*)cvLoad(pstrCascadeFileName);
const char *pstrImageName =fileName;
IplImage *pSrcImage = cvLoadImage(pstrImageName, CV_LOAD_IMAGE_UNCHANGED);
IplImage *pGrayImage = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 1);
cvCvtColor(pSrcImage, pGrayImage, CV_BGR2GRAY);
if (pHaarCascade != NULL)
{
CvScalar FaceCirclecolors[] =
{
{{255, 0, 0}}
};
CvMemStorage *pcvMStorage = cvCreateMemStorage(0);
cvClearMemStorage(pcvMStorage);
// 识别
DWORD dwTimeBegin, dwTimeEnd;
dwTimeBegin = GetTickCount();
CvSeq *pcvSeqFaces = cvHaarDetectObjects(pGrayImage, pHaarCascade, pcvMStorage);
dwTimeEnd = GetTickCount();
printf("侧脸个数: %d 识别用时: %d ms\n", pcvSeqFaces->total, dwTimeEnd - dwTimeBegin);
// 标记
for(int i = 0; i <pcvSeqFaces->total; i++)
{
CvRect* r = (CvRect*)cvGetSeqElem(pcvSeqFaces, i);
CvPoint center;
center.x = cvRound((r->x + r->width * 0.5));
center.y = cvRound((r->y + r->height * 0.5));
radius = cvRound((r->width + r->height) * 0.25);
printf("半径:%d\n",radius);
if(radius>50)
{
cvCircle(pSrcImage, center, radius, FaceCirclecolors[0], 2);
}
}
cvReleaseMemStorage(&pcvMStorage);
const char *pstrWindowsTitle = "人脸识别侧脸";
cvNamedWindow(pstrWindowsTitle, CV_WINDOW_AUTOSIZE);
cvShowImage(pstrWindowsTitle, pSrcImage);
if(radius>50)
{
avatar1();
}
//cvWaitKey(0);
cvDestroyWindow(pstrWindowsTitle);
}
cvReleaseImage(&pSrcImage);
cvReleaseImage(&pGrayImage);
}
cvReleaseImage(&pSrcImage1);
cvReleaseImage(&pGrayImage1);
}
}
void avatar0()
{
cvNamedWindow("hello",1);
CvCapture* capture = cvCreateFileCapture("C:\\Users\\dell\\Desktop\\hi2.avi");
IplImage* frame;
while(1)
{
frame = cvQueryFrame(capture);
if(!frame)
break;
cvShowImage("hello",frame);
char c = cvWaitKey(33);
if(c == 27)
break; }
cvReleaseCapture(&capture);
cvDestroyWindow("hello");
}
void avatar1()
{
cvNamedWindow("hello",1);
CvCapture* capture = cvCreateFileCapture("C:\\Users\\dell\\Desktop\\hi1.avi");
IplImage* frame;
while(1)
{
frame = cvQueryFrame(capture);
if(!frame)
break;
cvShowImage("hello",frame);
char c = cvWaitKey(33);
if(c == 27)
break; }
cvReleaseCapture(&capture);
cvDestroyWindow("hello");
}
vs2010+opencv249人脸正侧脸摄像头实时检测并调用对应不同小视频
最新推荐文章于 2023-04-27 09:00:00 发布