注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

dp: 生活的脚步,进步的点滴...

Cam、DSP、FPGA、PM、Life、More ...

 
 
 

日志

 
 

Handling videos in MATLAB  

2011-12-06 14:28:03|  分类: 专业 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
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 use
functions 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) resolution
fin = '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 video
mov_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 video
frm = im2frame(im_out);
aviobj = addframe(aviobj,frm);
i %Just to display frame number
end;
%Don't forget to close output file
aviobj = close(aviobj);
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 don’t 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
in_dir = 'Q:\temp\ffmpeg.rev11870\images\';
fout = 'q:\out1.avi'; %Output file name
num_images = 341; %Number of images

%Set a suitable frame rate fps
aviobj = avifile(fout, 'compression', 'none', 'fps', 25);


for i = 1:num_images;
temp = sprintf('%d', i);
name = [in_dir, 'image_', temp, '.bmp']; %For ppm, change
img = imread(name);
frm = im2frame(img);
aviobj = addframe(aviobj,frm);
i
end;

aviobj = close(aviobj);
return;
  评论这张
 
阅读(456)| 评论(0)
推荐

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016