You are on page 1of 3

11/17/2014

How do I record video from a webcam in MATLAB? - Stack Overflow


sign up

log in

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.

tour

help

stack overflow careers

Take the 2-minute tour

How do I record video from a webcam in MATLAB?

I would like to know how I can record a video in MATLAB with my webcam.
m atlab

video

webcam

record

edited Oct 30 '09 at 4:47


gnovice
73.6k 8 145 215

asked Oct 28 '09 at 14:26


Veronica
204 1 8 17

It is not clear to me from the question if you are trying to use a web cam to record your MATLAB session
(probably not, but I see people doing it). All of my videos are screen captures made and edited with Camtasia.
MatlabDoug Oct 29 '09 at 19:07
add a comment

5 Answers
If you already know how to capture a single image from a webcam, then it should just be a matter of
stitching the images together into a movie. You can convert an image file to a movie frame using
IM2FRAME, then you can use AVIFILE to create a video file from the frames. Here's how the code might
look:
aviObject = avifile('myVideo.avi'); % Create a new AVI file
for iFrame = 1:100
% Capture 100 frames
% ...
% You would capture a single image I from your webcam here
% ...
F = im2frame(I);
% Convert I to a movie frame
aviObject = addframe(aviObject,F); % Add the frame to the AVI file
end
aviObject = close(aviObject);
% Close the AVI file
I just used a for loop as a simple example, but you may want to use a MATLAB Timer Object if you
instead want to capture images and add them to the AVI file at regular time intervals.
edited Oct 28 '09 at 15:29

answered Oct 28 '09 at 15:18


gnovice
73.6k 8 145 215

add a comment

First construct a video input interface


vid = videoinput('winvideo',1,'RGB24_400x300');

http://stackoverflow.com/questions/1637589/how-do-i-record-video-from-a-webcam-in-matlab

1/3

11/17/2014

How do I record video from a webcam in MATLAB? - Stack Overflow


You'll need to adjust the last bit for your webcam. To find a list of webcam devices (and other things
besides) use:
imaqhwinfo
The following makes the first webcam into an object
a=imaqhwinfo('winvideo',1)
Find the list of supported video formats with
a.SupportedFormats
You'll then want to determine your frame rate (more on this here):
set(vid,'FramesPerTrigger',100);
start(vid);
wait(vid,Inf);
% Retrieve the frames and timestamps for each frame.
[frames,time] = getdata(vid, get(vid,'FramesAvailable'));
% Calculate frame rate by averaging difference between each frame's timestamp
framerate = mean(1./diff(time))
The FrameGrabInterval property specifies how often frames are stored from the video stream. For
instance, if we set it to 5, then only 1 in 5 frames is kept -- the other 4 frames will be discarded. Using the
framerate, determine how often you want to get frames
set(vid,'FrameGrabInterval',10);
To determine how many frames to acquire in total, calculate the total number of frames that would be
acquired at the device's frame rate, and then divide by the FrameGrabInterval.
capturetime = 30;
interval = get(vid,'FrameGrabInterval');
numframes = floor(capturetime * framerate / interval)
You are now ready to record and play with video using the getdata command ( peekdata is also
helpful), however...
If a large number of frames will be acquired, it is more practical to log the images to disk rather than to
memory. Using the Image Acquisition Toolbox, you can log images directly to an AVI file. We configure
this using the LoggingMode property.
set(vid,'LoggingMode','disk');
Create an AVI file object to log to, using the avifile command. We must specify the filename to use, and
the frame rate that the AVI file should be played back at. Then, set the DiskLogger property of the video
input object to the AVI file.
avi = avifile('timelapsevideo','fps',framerate);
set(vid,'DiskLogger',avi);
Start the time-lapse acquisition, and wait for the acquisition to complete. Note that the Image Acquisition
Toolbox does not tie up MATLAB while it is acquiring. You can start the acquisition and keep working in
MATLAB.
start(vid);
wait(vid,Inf); % Wait for the capture to complete before continuing.
Once the capture is completed, retrieve the AVI file object, and use the close function to release the
resources associated with it.
avi = get(vid,'DiskLogger');
avi = close(avi);
When you are done with the video input object, you should use the delete function to free the hardware
resources associated with it, and remove it from the workspace using the clear function.
delete(vid);
clear vid;
A large portion, but not all, of the above was drawn from here.
When you hit start(vid) you may notice that there's a bit of a delay before frames begin to be
acquired. This is bad if you're trying to synchronize the video with something. In this case, you'll want to
try working with the trigger:

http://stackoverflow.com/questions/1637589/how-do-i-record-video-from-a-webcam-in-matlab

2/3

11/17/2014

How do I record video from a webcam in MATLAB? - Stack Overflow


triggerconfig(vid,'manual');
start(vid); %There'll be a delay here, but nothing is being captured
trigger(vid); %Use this line when you want the capture to start. There should be
very little delay.
More info on triggers and synchronization is here.
edited Sep 19 '12 at 10:07

answered Sep 19 '12 at 9:45


Richard
6,622 2 34 66

add a comment

Google revealed these:


1. Create Video file from image or device
2. Image Capture using webcam in MATLAB
answered Oct 28 '09 at 14:55
Jacob
23k 6 55 116
add a comment

Here you can see great videos how to capture and process the images from webcam, so recording
shouldn't be hard:
http://blogs.mathworks.com/videos/2008/01/18/cool-feature-video-processing-demos/
answered Oct 30 '09 at 10:13
Gacek
3,799 4 26 52
add a comment

In my college life i have made this project named as


Audio/video Recorder & player application based on MATLAB
In that i have did audio recording and video recording and playback in matlab programming. you can
download Source code from here
answered Dec 26 '11 at 10:58
Mr.32
7,590 11 59 112
add a comment

Not the answer you're looking for? Browse other questions tagged matlab video
webcam

record or ask your own question.

http://stackoverflow.com/questions/1637589/how-do-i-record-video-from-a-webcam-in-matlab

3/3

You might also like