您的位置:首页 > 其它

视频文件按帧抓取图像并保存

2016-08-18 14:21 127 查看
一.原材料:

       win10,opencv3.0,vs2013,test.MP4。只需要配置好opencv的环境就好,3.0可以直接用MP4类型的视频数据。

二.实现功能:

      1.显示视频的具体信息,帧数,帧率,分辨率等;

      2.对视频进行逐帧抓取图像;

      3.对图像尺寸进行变换--缩放,我电脑是AMD显卡无gpu加速,只能缩小图像,减少以后的训练时间;

      4.将图像保存到特定的文件夹下。便于以后制作数据集。

三.代码实现:

<span style="font-size:14px;color:#000000;background-color: rgb(255, 255, 255);"></span><pre class="html" name="code">#include<cstring>    <span style="color:#ff0000;">////实现对视频进行按帧抓取图像并保存////</span>
#include "cv.h"
#include "highgui.h"
#include"stdlib.h"

using namespace std;

int main()
{
CvCapture *capture;
capture = cvCreateFileCapture("6515_10.124.6.230_2010-12-08_12-19-23(1).mp4");
assert(capture!=NULL);        <span style="color:#ff0000;"> //使用断言对函数参数进行确认,“假设”不成立则中断。
//显示视频信息--我的视频h576*w704,fps25,frame number15036
</span>	 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("\tvideo height : %d\n\tvideo width : %d\n\tfps : %d\n\tframe numbers : %d\n", frameH, frameW, fps,          numFrames);

IplImage *frame=0;            //初始化定义
int n = 0;                    //初始化
char image_name[256];         //image_name的名字、类型、存放路径的字符串长度,小了可能会内存溢出中断

while(1)
{
frame = cvQueryFrame(capture);     //获取一帧图像

sprintf(image_name,"%s%d%s","D:\\OPenCV\\images\\",++n,".jpg");
//保存的文件地址、类型、名称,中文路径保存不了。
IplImage*out = cvCreateImage(CvSize(frame->width / 2, frame->height / 2), frame->depth,frame->nChannels);
cvPyrDown(frame,out,CV_GAUSSIAN_5x5); //下采样缩小图像
cvSaveImage(image_name,out);     //保存一帧图像
cvReleaseImage(&out);

if(n==15011)break;               //共15011帧
}

cvReleaseCapture(&capture);
frame = NULL;  //cvReleaseImage()和cvCreateImage()相对应的。在程序中如果没有使用cvCreateImage()“创建”就不能“释放”。
system("pause");                 //是为了让输出结果在cmd停留,前提是有#include"stdlib.h"
return 0;
}



四.常见问题:

     报错:sprintf不安全请替换巴拉巴拉。。。 解决方法:视图>>其他窗口>>属性管理器>>Debug Win32  or Relesae Win32>>右键MicrosftCppWin32.user>>属性页>>C/C++>>预处理器>>预处理器定义>>编辑里面添加    _CRT_SECURE_NO_WARNINGS 保存即可 。

    其他内存溢出,无图片产生等问题代码注释的很仔细了。

五.待解决问题:

   指定区域的逐帧抓取,下一篇研究用这篇抓取到的图像制作caffe需要的leveldb型数据集。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐