Tuesday, August 24, 2010

Alternative for capturing video in matlab

I was a little frustrated in how slow Matlab's video capture toolbox is, so I found an alternative that seems to be a little bit faster and a little easier to understand/use. This requires vcapg2 from here:

http://www.mathworks.com/matlabcentral/fileexchange/2939-vcapg2

My simple example recording code is listed here:

function trecord(camnum,filename,recordtime,fps)
%trecord(camnum,filename,recordtime,fps)
%videoinput is the winvideo cam number
%recordtime in seconds
%fps frames per second
%This requires the vcapg2 dll located here:
%http://www.mathworks.com/matlabcentral/fileexchange/2939-vcapg2
%Other webpage that describes vcapg2:
%http://www.ikko.k.hosei.ac.jp/~matlab/matkatuyo/vcapg2.htm
%Interesting note: this was derived from the beloved amcap utility source
%code :)
%
% Example call: trecord(0,'test',2,10); outputs a file named test.avi that
% contains 2 seconds of video from camera 0 at 10 fps

clear mex;
cardnum=vcapg2(camnum); % specify the camera number..usually 1
figure(1);
set(1,'doublebuffer','on');
% capture mode
delay = 1./fps;
numframes = recordtime * fps;
for i=1:numframes
aa=vcapg2;% grabbing camera image.
pause(delay);%Not quite the correct delay..because processing takes up a few milliseconds
m(i)= im2frame(aa);% Convert to a matlab movie frame
imshow(aa)
drawnow;% this is important to view realtime.
end
movie2avi(m,filename, 'compression', 'Cinepak', 'fps', fps);%Can change the compression codec
clear mex; % remove dll
close(1)

No comments:

Post a Comment