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.写入视频文件之前要先打开文件,写入完毕之后要关闭文件.

posted on   鹅要长大  阅读(10921)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示