Reading and Writing Videos and Image Sequences in Matlab

advertisement
Reading and Writing Videos and Image
Sequences in Matlab
EGGN 512 Computer Vision Colorado School of Mines, Engineering Division
Prof. William Hoff
Reading an image sequence
• You can read a sequence of files with a loop
– You can use Matlab’s “sprintf” function to create a string with the filename
of each image
– Then just read in the image using “imread”
• Use of sprintf:
str = sprintf(format, A, ...);
– where
• str is the output resulting string
• format is a string in single quotes that describes the format of the output fields
• A is the output variables
– See the Matlab help page on sprintf for a complete description of
the formatting possibilities
EGGN 512 Computer Vision Colorado School of Mines, Engineering Division
Prof. William Hoff
2
Reading an image sequence
• Example:
szDirectory = 'sequence300';
% Directory name where images are stored
% Read in images. Note - it is slow for Matlab to incrementally "grow" an
% array; in this case, when reading in each image from 1 to 300.
% It is much faster if you pre-allocate the storage. We can get the same
% effect here by reading in the images from 300 down to 1.
for i=300:-1:1
fname = sprintf('%s/image%04d.jpg', szDirectory, i);
I = imread(fname);
:
Note – if you are going to do computations on the images
end
(such as estimating the mean or median) you should convert
the images to type double
• You can also use Matlab’s “dir” command to give you a list of
all files in a directory
EGGN 512 Computer Vision Colorado School of Mines, Engineering Division
Prof. William Hoff
3
Reading Movie Files in Matlab
• Matlab can read “avi”, “mpg”, and “wmv” movie files
VideoReader.getFileFormats()
% see full list
• To get information about the movie:
movieObj = VideoReader('xylophone.mpg'); % open file
get(movieObj)
% display all info
nFrames = movieObj.NumberOfFrames;
width = movieObj.Width;
% get image width
height = movieObj.Height;
% get image height
• To read images one at a time:
for i=1:nFrames
img = read(movieObj,i);
:
end
% get one RGB image
EGGN 512 Computer Vision Colorado School of Mines, Engineering Division
Prof. William Hoff
4
Example
clear all
close all
movieObj = VideoReader('xylophone.mpg'); % open file
get(movieObj)
% display all information about movie
nFrames = movieObj.NumberOfFrames;
% Read every other frame from this movie.
for iFrame=1:2:nFrames
I = read(movieObj,iFrame);
% get one RGB image
fprintf('Frame %d\n', iFrame);
imshow(I,[]);
% Display image
% Pause a little so we can see the image.
% waits until a key is pressed.
pause(0.1);
If no argument is given, it
end
EGGN 512 Computer Vision Colorado School of Mines, Engineering Division
Prof. William Hoff
Reading (continued)
• Reading frames one at a time is slow ... an alternative is to
read all of them at once (takes more memory)
images = read(movieObj);
% get all images
– This creates a 4-dimensional array, of size (height, width, 3, nFrames)
I = images(:,:,:,i); % get the ith image
• You can also read an interval (say from 100 to 200)
images = read(movieObj, [100 200]);
• Note on wmv files (see Matlab help page for more
information)
– Some formats (including wmv) store video at a variable frame rate
– On these files, VideoReader cannot determine the number of frames
until you read the last frame
– It may return a warning that it can’t determine the number of frames
EGGN 512 Computer Vision Colorado School of Mines, Engineering Division
Prof. William Hoff
Writing Movie Files in Matlab
• To create a movie (avi format)
vidObj = VideoWriter(‘mymovie.avi'); % create avi file
open(vidObj);
:
% Add next frame to movie
imshow(img);
newFrameOut = getframe;
writeVideo(vidObj,newFrameOut);
:
close(vidObj);
% all done, close file
EGGN 512 Computer Vision Colorado School of Mines, Engineering Division
Prof. William Hoff
7
Download