This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

readFrame

Read video frame from video file

Syntax

video = readFrame(v)
video = readFrame(v,'native')

Description

example

video = readFrame(v) reads the next available video frame from the file associated with v.

video = readFrame(v,'native') returns data in the format specified by the VideoFormat property.

Examples

collapse all

Create a VideoReader object for the example movie file, xylophone.mp4.

v = VideoReader('xylophone.mp4');

Read all video frames.

while hasFrame(v)
    video = readFrame(v);
end
whos video
  Name         Size                Bytes  Class    Attributes

  video      240x320x3            230400  uint8              

Create an object to read the sample file, xylophone.mp4.

v = VideoReader('xylophone.mp4');

Specify that reading should begin 2.5 seconds from the beginning of the video.

v.CurrentTime = 2.5;

Create an axes. Then, read video frames until no more frames are available to read.

currAxes = axes;
while hasFrame(v)
    vidFrame = readFrame(v);
    image(vidFrame, 'Parent', currAxes);
    currAxes.Visible = 'off';
    pause(1/v.FrameRate);
end

Read and play back the sample movie file, xylophone.mp4.

Create a VideoReader object to read data from the sample file. Then, determine the width and height of the video.

xyloObj = VideoReader('xylophone.mp4');

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

Create a movie structure array, mov.

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

Read one frame at a time until the end of the video is reached.

k = 1;
while hasFrame(xyloObj)
    mov(k).cdata = readFrame(xyloObj);
    k = k+1;
end

Size a figure based on the width and height of the video. Then, play back the movie once at the video frame rate.

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

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

Input Arguments

collapse all

Input VideoReader object. Use the VideoReader function to create a VideoReader object from your video file.

Output Arguments

collapse all

Video frame data, returned as an array. The dimensions and data type of video depend on the VideoFormat property of obj.

The following table lists the data type and dimensions of video for most files. H is the image frame height and W is the image frame width. 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', with or without specifying 'native'uint8

H-by-W-by-3

RGB24 image

'Grayscale', without specifying 'native'uint8

H-by-W-by-1

Grayscale image

'Indexed', without specifying 'native'uint8

H-by-W-by-3

RGB24 image

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

1-by-1

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

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

Mono image

'Mono8 Signed'int8

H-by-W-by-1

Mono signed image

'Mono16'uint16

H-by-W-by-1

Mono image

'Mono16 Signed'int16

H-by-W-by-1

Mono signed image

'RGB24'uint8

H-by-W-by-3

RGB24 image

'RGB24 Signed'int8

H-by-W-by-3

RGB24 signed image

'RGB48'uint16

H-by-W-by-3

RGB48 image

'RGB48 Signed'int16

H-by-W-by-3

RGB48 signed image

Flag to use format specified in VideoFormat property, specified as the character vector or a string scalar, 'native'.

Data Types: char | string

Extended Capabilities

Introduced in R2014b