前言
视频处理分析的过程中,需要用到将视频一帧帧地读取、写入,本文就涉及此问题。
系统环境
1.系统:win7_64
2.matlab版本:matlab2015a
测试代码
代码一(读视频):
%To read video frames. clc clear close all fileName = 'E:fatigue_detectiondatasetsegVideosP11_5.avi'; obj = VideoReader(fileName); numFrames = obj.NumberOfFrames; for i = 1 : numFrames frame = read(obj,i); imshow(frame); imwrite(frame,strcat(num2str(i),'.jpg'),'jpg'); end
代码二(读视频):
fileName = 'E:fatigue_detectiondatasetsegVideosP11_5.avi'; xyloObj = VideoReader(fileName); vidWidth = xyloObj.Width; vidHeight = xyloObj.Height; % mov = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),'colormap',[]); while hasFrame(xyloObj) frame = readFrame(xyloObj); imshow(frame); end
代码三(写视频):
写视频步骤:
创建视频文件VideoWriter - > 打开视频文件open - > 获取视频帧并写入视频文件writeVideo -> 关闭视频文件close.
fileName = 'E:fatigue_detectiondatasetsegVideosP11_5.avi'; %method2 xyloObj = VideoReader(fileName); vidWidth = xyloObj.Width; vidHeight = xyloObj.Height; fps = xyloObj.FrameRate; out = VideoWriter('out.avi'); out.FrameRate = fps; open(out); while hasFrame(xyloObj) frame = readFrame(xyloObj); writeVideo(out, frame); end close(out);
代码可参考matlab的help文档.
注意:
1.不同版本之间可能会存在一些代码问题,可参考help文档进行修正.
2.写入视频文件之前要先打开文件,写入完毕之后要关闭文件.
完