matlab读写视频VideoReader/VideoWriter

前言

视频处理分析的过程中,需要用到将视频一帧帧地读取、写入,本文就涉及此问题。

系统环境

1.系统:win7_64

2.matlab版本:matlab2015a

测试代码

代码一(读视频):

%To read video frames.
clc
clear 
close all

fileName = 'E:\fatigue_detection\dataset\segVideosP1\1_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_detection\dataset\segVideosP1\1_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_detection\dataset\segVideosP1\1_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.写入视频文件之前要先打开文件,写入完毕之后要关闭文件.

做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。 版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/

转:https://www.cnblogs.com/happyamyhope/p/7770622.html

猜你喜欢

转载自blog.csdn.net/eric_e/article/details/88623269