Matlab读取视频并处理成帧保存

Matlab读取视频并处理成帧保存

本文介绍主要介绍VideoReader读取视频,并用imwrite将视频帧保存。

  • VideoReader和imwrite的用法
    请先help VideoReader
    OBJ = VideoReader(FILENAME) constructs a multimedia reader object, OBJ, that can read in video data from a multimedia file. FILENAME is a string specifying the name of a multimedia file. There are no restrictions on file extensions. By default, MATLAB looks for the file FILENAME on the MATLAB path.
    该语句将视频文件读到OBJ结构,FILENAME为文件路径,可以为绝对路径,默认路径为工程目录。具体实现如下:

VideoReader介绍

obj = VideoReader('D://Media//111.mp4');%输入视频位置

难点在于详解obj结构体的含义,如下(见doc VideoReader):
Name - -视频文件名
Path – 视频文件路径
Duration – 视频的总时长(秒)
FrameRate - -视频帧速(帧/秒)
NumberOfFrames – 视频的总帧数
Height – 视频帧的高度
Width – 视频帧的宽度
BitsPerPixel – 视频帧每个像素的数据长度(比特)
VideoFormat – 视频的类型, 如 ‘RGB24’.
Tag – 视频对象的标识符,默认为空字符串”
Type – 视频对象的类名,默认为’VideoReader’.
UserData – Generic field for data of any class that you want to add to the object. Default: []

obj_numberofframe = obj.NumberOfFrame;%读取总的帧数
obj_height = obj.Height;%读取视频帧高度
%%%以此类推

read - 读取视频帧

frame = read(obj),获取该视频对象的所有帧
frame = read(obj,index),获取该视频对象的制定帧
frame = read(obj, 1);         % first frame only 获取第一帧
frame = read(obj, [1 10]);    % first 10 frames 获取前10帧
frame = read(obj, Inf);       % last frame only 获取最后一帧
frame = read(obj, [50 Inf]);  % frame 50 thru end 获取第50帧之后

imwrite介绍

同样,help imwrite
imwrite(A,FILENAME,FMT) writes the image A to the file specified by FILENAME in the format specified by FMT.
直接上代码:

 imwrite(frame,strcat('D:\image\cankao1\1.jpg'),'jpg');% 保存帧
%%%frame为待保存的某一帧 
%%%strcat('D:\image\cankao1\1.jpg')为保存目录
%%%'jpg'为保存格式

整体代码

obj = VideoReader('D://Media//111.mp4');%输入视频位置
numFrames = obj.NumberOfFrames;% 帧的总数
 for k = 1 : 15% 读取前15帧
     frame = read(obj,k);%读取第几帧
    % imshow(frame);%显示帧
      imwrite(frame,strcat('D:\image\cankao1\',num2str(k),'.jpg'),'jpg');% 保存帧
 end

你可能感兴趣的:(Matlab)