博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从视频中提取图片,对图片做人脸检测并截取人脸区域
阅读量:5263 次
发布时间:2019-06-14

本文共 5785 字,大约阅读时间需要 19 分钟。

环境配置:VS2013+opencv2.4.10+libfacedetect.lib

libfacedetect.libxi下载:  

安装参考博客日志:DAY1 :

参考博客:

               

首先给出视频处理的函数video_process.hpp

#include 
#include
#include "facedetect-dll.h"#include
#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
void Video_to_image(char* filename, char* Savepath){ printf("------------- video to image ... ----------------\n"); 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] = { '\0' }; while (count_tmp<15) //每段视频保留3帧 { if (cvGrabFrame(capture)) { if (count_tmp % jiange == 0) { frame = cvRetrieveFrame(capture); cvShowImage("AVI player", frame);//显示当前帧 sprintf(tmpfile, "%s%d.jpg", AviSavePath, i);//使用帧号作为图片名 cvSaveImage(tmpfile, frame); i++; } if (cvWaitKey(10) >= 0) //延时 break; ++count_tmp; } else { break; } } cvReleaseCapture(&capture); cvDestroyWindow("AVI player"); std::cout << "总帧数" << count_tmp << std::endl; cvWaitKey(); return;}int image_cut(char* Filename, char* Savepath){ Mat image = imread(Filename); if (image.empty()) { fprintf(stderr, "Can not load the image file %s.\n"); return -1; } Mat gray; cvtColor(image, gray, CV_BGR2GRAY); int * pResults = NULL; //pBuffer is used in the detection functions. //If you call functions in multiple threads, please create one buffer for each thread! unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE); if (!pBuffer) { fprintf(stderr, "Can not alloc buffer.\n"); return -1; } int doLandmark = 1; /// // reinforced multiview face detection / 68 landmark detection // it can detect side view faces, better but slower than facedetect_multiview(). // //!!! The input image must be a gray one (single-channel) //!!! DO NOT RELEASE pResults !!! pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, 1.2f, 3, 48, 0, doLandmark); printf("%d faces detected.\n", (pResults ? *pResults : 0)); int j = 0; Mat result_multiview_reinforce = image.clone(); Mat image_cut = image.clone(); //print the detection results for (int i = 0; i < (pResults ? *pResults : 0); i++) { short * p = ((short*)(pResults + 1)) + 142 * i; int x = p[0]; int y = p[1]; int w = p[2]; int h = p[3]; int neighbors = p[4]; int angle = p[5]; printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x, y, w, h, neighbors, angle);//(x,y)为检测到人脸左上角像素位置,w为宽,h为高 rectangle(result_multiview_reinforce, Rect(x, y, w, h), Scalar(0, 255, 0), 0.5); int y1 = y - 100; int x1 = x - 90; int x2 = x + 285; int y2 = y + 345; if (y1 < 0) //超出边界判断 y1 = 0; if (x1 < 0) x1 = 0; if (y2 >479) y2 = 479; if (x2>679) x1 = 679; image_cut = image_cut(Range(y1, y2), Range(x1, x2)); //imshow("image_cut", image_cut); imwrite(Savepath, image_cut); } //release the buffer free(pBuffer); return 0;}

 

转载于:https://www.cnblogs.com/yamin/p/7338070.html

你可能感兴趣的文章
IOC容器
查看>>
Windows 2003全面优化
查看>>
URAL 1002 Phone Numbers(KMP+最短路orDP)
查看>>
web_day4_css_宽度
查看>>
electron入门心得
查看>>
格而知之2:UIView的autoresizingMask属性探究
查看>>
我的Hook学习笔记
查看>>
js中的try/catch
查看>>
寄Android开发Gradle你需要知道的知识
查看>>
简述spring中常有的几种advice?
查看>>
整理推荐的CSS属性书写顺序
查看>>
ServerSocket和Socket通信
查看>>
css & input type & search icon
查看>>
源代码的下载和编译读后感
查看>>
Kafka学习笔记
查看>>
Octotree Chrome安装与使用方法
查看>>
Windows 环境下基于 Redis 的 Celery 任务调度模块的实现
查看>>
趣谈Java变量的可见性问题
查看>>
C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
查看>>
ssm框架之将数据库的数据导入导出为excel文件
查看>>