readFrame
Read next video frame
Description
Examples
Create VideoReader
Object and Read Video
Create a VideoReader
object for the sample video file xylophone_video.mp4
.
v = VideoReader("xylophone_video.mp4");
Read all the frames from the video, one frame at a time.
while hasFrame(v) frame = readFrame(v); end
Display information about the last frame returned by readFrame
.
whos frame
Name Size Bytes Class Attributes frame 240x320x3 230400 uint8
Clear the VideoReader
object.
clear v
Read Video Frames Starting at Specific Time
Create a VideoReader
object for the sample video file xylophone_video.mp4
.
v = VideoReader("xylophone_video.mp4");
Specify to start reading frames at 2.5 seconds from the beginning of the video.
v.CurrentTime = 2.5;
Create an axes
object to display the frame. Then continue to read and display 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
Clear the VideoReader
object.
clear v
Read and Play Back Movie File
Create a VideoReader
object for the sample video file xylophone_video.mp4
. Then determine the width and height of the video.
xyloObj = VideoReader("xylophone_video.mp4");
vidWidth = xyloObj.Width;
vidHeight = xyloObj.Height;
Create a video structure array.
mov = struct("cdata",zeros(vidHeight,vidWidth,3,"uint8"),colormap=[]);
Read one frame at a time until the end of the video.
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, and then play the video one time.
vf = figure(Position=[0 0 xyloObj.Width xyloObj.Height]);
imshow(mov(1).cdata,Border="tight")
movie(vf,mov,1,xyloObj.FrameRate)
Clear the VideoReader
object.
clear xyloObj
Input Arguments
v
— Input VideoReader
object
VideoReader
object
Input VideoReader
object. Use the VideoReader
function to
create a VideoReader
object from your video file.
Output Arguments
video
— Video frame data
array
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
readFrame
with the 'native'
argument.
Value of obj.VideoFormat | Data Type of video | Dimensions of video | Description |
---|---|---|---|
'RGB24' , with or without specifying 'native' | uint8 |
| RGB24 image |
'Grayscale' , without specifying 'native' | uint8 |
| Grayscale image |
'Indexed' , without specifying 'native' | uint8 |
| 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 |
For Motion JPEG 2000 files, the data type and dimensions of video
are
as follows.
Value of obj.VideoFormat | Data Type of video | Dimensions of video | Description |
---|---|---|---|
'Mono8' | uint8 |
| Mono image |
'Mono8 Signed' | int8 |
| Mono signed image |
'Mono16' | uint16 |
| Mono image |
'Mono16 Signed' | int16 |
| Mono signed image |
'RGB24' | uint8 |
| RGB24 image |
'RGB24 Signed' | int8 |
| RGB24 signed image |
'RGB48' | uint16 |
| RGB48 image |
'RGB48 Signed' | int16 |
| RGB48 signed image |
Limitations
For some AVI, MOV, or MP4 files on Windows®, using the
readFrame
function to read all of the frames in the file can result in a different number of frames from the value returned by theNumFrames
property of theVideoReader
object.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Code generation for
readFrame
does not support the optional positional argumentnative
.Generate Code That Uses Row-Major Layout — Generate Code That Uses Row-Major Array Layout (MATLAB Coder).
Array Size Restrictions — For code generation, the maximum number of elements of an array is constrained by the code generator and the target hardware. For more information, see Array Size Restrictions for Code Generation (MATLAB Coder).
Version History
Introduced in R2014b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)