环境配置:VS2013+opencv2.4.10+libfacedetect.lib
libfacedetect.libxi下载: https://github.com/ShiqiYu/libfacedetection/blob/master/example/libfacedetect-example.cpp
安装参考博客日志:DAY1 :http://www.cnblogs.com/yamin/p/7115501.html
参考博客:http://blog.csdn.net/augusdi/article/details/11042329
首先给出视频处理的函数video_process.hpp
#include <stdio.h> #include <opencv2/opencv.hpp> #include "facedetect-dll.h" #include<opencv2/highgui/highgui.hpp> #pragma comment(lib,"libfacedetect.lib") //#pragma comment(lib,"libfacedetect-x64.lib") using namespace cv; #define DETECT_BUFFER_SIZE 0x20000 //facedetect #define UNKNOWN_FLOW_THRESH 1e9 //facedetect #define NUM_FRAME 100 //Video_to_imag中控制截取帧数 //函数声明 void Video_to_image(char* filename, char* Savepath); /* 函数功能:读取视频的每一帧,并将其按帧数命名保存 例如:Video_to_image("F:\tp\1.mp4", "F:\image"); */ void video_to_image(char* Filename, char* Savepath); /* 函数功能:截取视频前三帧图片并将其保存,帧数间隔默认5 可用count_tmp和jiangge控制读取帧数和帧数间隔 用法示例:video_to_image(videopath, "F:\截图\1_") 保存文件名为\后字符和输入序号的拼接 */ int image_cut(char* Filename, char* Savepath); /* 函数功能:对图片进行人脸检测吧,并截取保存人脸及周边区域 其中用到了libfacedetect.lib 用法示例:image_cut("F:\截图\1_1.jpg","F:\截图\CUT1_1.jpg" ); */
给出视频处理的函数video_process.cpp ,对应上面三个函数
#include<video_process.h> void Video_to_image(char* filename, char* Savepath) { printf("------------- video to image ... ---------------- "); CvCapture* capture = cvCaptureFromAVI(filename);//初始化一个视频文件捕捉器 cvQueryFrame(capture);//获取视频信息 int frameH = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); int frameW = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH); int fps = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); int numFrames = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT); printf("video height : %dntvideo width : %dntfps : %dntframe numbers : %dn", frameH, frameW, fps, numFrames);//打印视频信息 //定义和初始化变量 int i = 0; IplImage* img = 0; char image_name[18]; cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); //读取和显示 while (1) { img = cvQueryFrame(capture); //获取一帧图片 cvShowImage("mainWin", img); //将其显示 char key = cvWaitKey(20); sprintf(image_name, "%s%d%s", Savepath, ++i, ".jpg");//保存的图片名 cvSaveImage(image_name, img); //cvSaveImage( image_name, img); //保存一帧图片 if (i == 0) { sprintf(image_name, "%s//%d%s", Savepath, i, ".jpg"); cvSaveImage(image_name, img); //保存一帧图片 } if (i == numFrames) break; //if (i == NUM_FRAME) break; i++; } cvReleaseCapture(&capture); cvDestroyWindow("mainWin"); cvWaitKey(); } void video_to_image(char* Filename, char* Savepath) { printf("------------- video to image ... ----------------n"); //初始化一个视频文件捕捉器 CvCapture *capture = NULL; IplImage *frame = NULL; char *AviFileName = Filename;// "F:\tp\1.mp4";//视频的目录 char *AviSavePath = Savepath;//"F:\截图\";//图片保存的位置 const int jiange = 5;//间隔5帧保存一次图片 capture = cvCaptureFromAVI(AviFileName); cvNamedWindow("AVI player", 1); int count_tmp = 0;//计数总帧数 int i = 1; char tmpfile[100] = { '