在 Matlab 中表示视频的一种标准方法是 4D 矩阵。尺寸是高度 x 宽度 x 颜色通道 x 帧。一旦你有了矩阵,就很容易通过指定你想要的帧范围来获取时间片。
例如,您可以在 for 循环中一次抓取 8 帧
%Loads video as 4D matrix
v = VideoReader('xylophone.mp4');
while hasFrame(v)
video = cat(4, video, readFrame(v));
end
%iterate over the length of the movie with step size of 8
for i=1:8:size(video, 4)-8
video_slice = video(:,:,:,i:i+7); %get the next 8 frames
% do something with the 8 frames here
% each frame is a slice across the 4th dimension
frame1 = video_slice(:,:,:,1);
end
%play movie
implay(video)
另一种最常见的视频表示方式是结构数组。您可以索引具有一系列值的结构数组以切片 8 帧。我的示例中的实际帧值存储在结构元素cdata 中。根据您的结构,元素可能有不同的名称;寻找具有 3d 矩阵值的元素。
% Loads video as structure
load mri
video = immovie(D,map);
%iterate over the length of the movie with step size of 8
for i=1:8:size(video, 4)-8
video_slice = video(i:i+7); %get the next 8 frames
% do something with the 8 frames here
% to access the frame values use cdata
frame1 = video_slice(1).cdata
end
%play movie
implay(video)
棘手的部分是您的视频格式。 Matlab 的VideoReader 不支持 Y4M,这是加载视频的最常见方式。 FFmpeg Toolbox 也不支持它,它只提供几种媒体格式(MP3、AAC、mpeg4、x264、动画 GIF)。
还有一些其他问题正在寻找解决此问题的方法,包括
- how to read y4m video(get the frames) file in matlab
- How to read yuv videos in matlab?
我也会查看the Matlab File Exchange,但我对这些方法中的任何一种都没有个人经验。