opencv MP4图像序列下采样 并将保存为avi格式

来源:互联网 发布:中山大学远程网络教育 编辑:程序博客网 时间:2024/05/17 22:26

利用opencv对视频序列中每一帧进行下采样  原分辨率为1280*720   下采样后分辨率为640*360

本来计划的是也保存为mp4格式 但是opencv可以正常读入mp4文件却不能写  在网上搜索说这是opencv以及windows版本之间相互作用的结果难过,所以最后保存为avi格式了。


代码如下  在vs2012+opencv246下编译运行正常


#include<opencv2/opencv.hpp>using namespace std;#define Height 360    //采样后图像的分辨率#define Width 640int main(){CvCapture * capture = NULL;capture =cvCreateFileCapture ("F:\\selfmadetest\\test.mp4");  //原文件路径 注意双斜线int framenum = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);if(framenum == 0)return -1;        IplImage * frame ;IplImage * tempFrame;  //存放中间处理后的frameCvScalar s;                                                                                                                                                                                                                                                     int iStartFrameNum = 60;int iEndFrameNum = 200;      //根据需要,只对其中的某些帧进行转换CvSize sizeTemp;sizeTemp.height = Height;sizeTemp.width = Width;int frame_ps=4;int fcc=CV_FOURCC('M','J','P','G');CvVideoWriter *pW=cvCreateVideoWriter("output_7.avi",fcc,frame_ps,sizeTemp,1);        //从指定帧读取cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES, iStartFrameNum);                                                    for(int i = iStartFrameNum; i < iEndFrameNum; ++i){frame = cvQueryFrame(capture);tempFrame = cvCreateImage(sizeTemp,frame->depth, frame ->nChannels);for(int i = 0; i < Height; ++i)for(int j = 0; j < Width; ++j){s = cvGet2D(frame,i*2,j*2);cvSet2D(tempFrame,i,j,s);}cvWriteFrame(pW, tempFrame);}cvReleaseImage(&frame);cvReleaseImage(&tempFrame);cvReleaseCapture(&capture);cvReleaseVideoWriter(&pW);return 1;}








0 0
原创粉丝点击