Upload
kellie-baker
View
215
Download
0
Embed Size (px)
Citation preview
7/30/2019 Matlab easy to use video
1/3
Handling videos in MATLAB
Note: MATLAB supports only raw (uncompressed) avi files on Linux and only some
Indeo and Cinepack compressed versions on Windows. But mostly you get videos in
MPEG. So you need to convert to raw or any supported format. For this, FFmpeg can be
used (see Section 2).
Note: Avi means Audio Video Interleave and is only a container format. Avi does not
mean raw video. It is possible to have mpeg or many other compressed avi files.
MATLAB can not read such a compressed avi files. (See
http://en.wikipedia.org/wiki/Audio_Video_Interleave).
/****************************************/
Section 1
If you have raw video
/***************************************/
If you have raw video, then handling in MATLAB is quite easy. You need to usefunctions avifile, aviread, frame2im, im2frame and addframe functions.
Following code converts given video (in.avi) to QVGA (320x240) resolution video
(out.avi). This example shows how to use video related functions in MATLAB. For
further details and other functions see MATLAB help.
/***************************************/
% Example to scale a given video to QVGA (320x240) resolutionfin = 'e:\in.avi';fout = 'd:\out.avi';
fileinfo = aviinfo(fin);nframes = fileinfo.NumFrames;
aviobj = avifile(fout, 'compression', 'none', 'fps',
fileinfo.FramesPerSecond);
for i = 1:nframes%Read frames from input videomov_in = aviread(fin,i);im_in = frame2im(mov_in);
%Do processing on each frame of the video%In this example - scaling
[ht wd ch] = size(im_in);im_out = imresize(im_in, [240 320], 'bilinear');
%Write frames to output videofrm = im2frame(im_out);aviobj = addframe(aviobj,frm);
i %Just to display frame numberend;%Don't forget to close output fileaviobj = close(aviobj);
7/30/2019 Matlab easy to use video
2/3
return;
/***************************************/
/****************************************/
Section 2
If you have compressed video
/***************************************/
You can use any converter tool to convert compressed video to uncompressed avi format.
One such tool is FFmpeg (http://ffmpeg.mplayerhq.hu/index.html). Mplayer, VLC and
many other players are based on this codec.
If you are a Windows user, just download latest binary from
http://ffdshow.faireal.net/mirror/ffmpeg/ (Any virus? I dont know. Download at your
risk!).
(You can use WinRAR to uncompress .7z -
http://www.freedownloadscenter.com/Utilities/Compression_and_Zip_File_Utilities/Win
RAR_Download.html)
Use this command to convert any compressed file (compressed.any) to uncompressed avi
file (uncompressed.avi) (Note: pthreadGC2.dll file should be in the same directory as
ffmpeg.exe).
ffmpeg.exe -i compressed.any -vcodec rawvideo uncompressed.avi
This should work most of the cases. However, sometimes it may not work (because, avi
is a container format by Microsoft! :)). The converted video may have only blank frames
or MATLAB may not be able to read it properly. In such a case, you can try steps
explained in Section 3.
/****************************************/
Section 3
If you have compressed video and
Steps in Section 2 does not work
/***************************************/
Create a folder by name images
Use this command to convert each frame of the compressed video to bmp (or ppm)
images and put it into the folder images.
ffmpeg.exe -i compressed.any images/image_%d.bmp
Now you can use these images for processing. If you want uncompressed avi file, then
use following MATLAB code to combine all raw images into a single uncompressed avi
file.
/***************************************/
%Script file to combine images to an uncompressed avi file%Directory that contains images
7/30/2019 Matlab easy to use video
3/3
in_dir = 'Q:\temp\ffmpeg.rev11870\images\';
fout = 'q:\out1.avi'; %Output file namenum_images = 341; %Number of images
%Set a suitable frame rate fpsaviobj = avifile(fout, 'compression', 'none', 'fps', 25);
for i = 1:num_images;temp = sprintf('%d', i);name = [in_dir, 'image_', temp, '.bmp']; %For ppm, changeimg = imread(name);frm = im2frame(img);aviobj = addframe(aviobj,frm);
iend;
aviobj = close(aviobj);return;
Any feedback? Mail to paresh.kalyan and kv.kirans at gmail