Documentation Center

  • Trial Software
  • Product Updates

read

Class: VideoReader

Read video frame data from file

Syntax

video = read(obj)
video = read(obj,index)
video = read(___,'native')

Description

video = read(obj) reads in all video frames from the file associated with obj. The read method returns a H-by-W-by-B-by-F matrix, video, where H is the image frame height, W is the image frame width, B is the number of bands in the image (for example, 3 for RGB), and F is the number of frames read.

video = read(obj,index) reads only the specified frames. index can be a single number or a two-element array representing an index range of the video stream.

video = read(___,'native') returns data in the format specified by the VideoFormat property, and can include any of the input arguments in the previous syntaxes.

Input Arguments

obj

Name of multimedia object created with VideoReader.

index

Frames to read, where the first frame number is 1. Use Inf to represent the last frame of the file.

For example:

video = read(obj, 1);         % first frame only
video = read(obj, [1 10]);    % first 10 frames
video = read(obj, Inf);       % last frame only
video = read(obj, [50 Inf]);  % frame 50 thru end

MATLAB® cannot determine the number of frames in a variable frame rate file until you read the last frame. If the requested index extends beyond the end of the file, read returns either a warning or an error. For more information, see Read Variable Frame Rate Video.

Default: [1 Inf]

Output Arguments

video

Array of data representing video frames, returned in a format dependent on the VideoFormat property of obj. For most files, the data type and dimensions of video are as follows. Note that when the VideoFormat property of obj is 'Indexed', the data type and dimensions of video depend on whether you call read with the 'native' argument.

Value of obj.VideoFormatData Type of videoDimensions of videoDescription
'RGB24'uint8

H-by-W-by-3-by-F

RGB24 image

'Grayscale', without specifying 'native'uint8

H-by-W-by-1-by-F

Grayscale image

'Indexed', without specifying 'native'uint8

H-by-W-by-3-by-F

RGB24 image

'Grayscale' or 'Indexed', specifying 'native'struct

1-by-F

MATLAB movie, which is an array of frame structure arrays, each containing the fields cdata and colormap.

H is the image frame height, W is the image frame width, and F is the number of frames read.

For Motion JPEG 2000 files, the data type and dimensions of video are as follows.

Value of obj.VideoFormatData Type of videoDimensions of videoDescription
'Mono8'uint8

H-by-W-by-1-by-F

Mono image

'Mono8 Signed'int8

H-by-W-by-1-by-F

Mono signed image

'Mono16'uint16

H-by-W-by-1-by-F

Mono image

'Mono16 Signed'int16

H-by-W-by-1-by-F

Mono signed image

'RGB24'uint8

H-by-W-by-3-by-F

RGB24 image

'RGB24 Signed'uint8

H-by-W-by-3-by-F

RGB24 signed image

'RGB48'uint16

H-by-W-by-3-by-F

RGB48 image

'RGB48 Signed'int16

H-by-W-by-3-by-F

RGB48 signed image

Examples

expand all

Read and Play Back Movie File

Read and play back the movie file xylophone.mp4.

xyloObj = VideoReader('xylophone.mp4');

nFrames = xyloObj.NumberOfFrames;
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;

Preallocate the movie structure.

mov(1:nFrames) = ...
    struct('cdata',zeros(vidHeight,vidWidth, 3,'uint8'),...
           'colormap',[]);

Read one frame at a time.

for k = 1 : nFrames
    mov(k).cdata = read(xyloObj,k);
end

Size a figure based on the video's width and height.

hf = figure;
set(hf, 'position', [150 150 vidWidth vidHeight])

Play back the movie once at the video's frame rate.

movie(hf, mov, 1, xyloObj.FrameRate);

See Also

|

How To

Was this topic helpful?