Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 217
Veronica Avatar asked Oct 28 '09 14:10

Veronica


People also ask

How do I record a webcam video in Matlab?

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);

How do I connect my camera to Matlab?

Create a Webcam Object Using Camera NameUse the webcam function with the name of the camera as the input argument to create the object and connect it to the camera with that name. Find the name of your camera using the webcamlist function to ensure that MATLAB is discovering your camera(s). Create an object, cam .

Which Matlab command you can use to take a picture from webcam?

img = snapshot( cam ); acquires a single image from the webcam object cam and assigns it to the variable img .

Can Matlab analyze video?

MATLAB® provides tools and algorithms that let you view, analyze, read, and write videos.


2 Answers

First construct a video input interface

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

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:

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.

like image 137
Richard Avatar answered Nov 01 '22 02:11

Richard


NOTE: This is now updated for use with newer versions of MATLAB, since some of the older functionality has been superseded and removed.

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 use a VideoWriter object to open a movie file and then add sequential images using the writeVideo method. For example:

aviObject = VideoWriter('myVideo.avi');  % Create a new AVI file
for iImage = 1:100                       % Capture 100 frames
  % ...
  % You would capture a single image I from your webcam here
  % ...
  writeVideo(aviObject, I);  % Add the image to the AVI file
end
close(aviObject);  % Close the AVI file

I just used a for loop as a simple example, but you may want to use a timer if you instead want to capture images and add them to the AVI file at regular time intervals.

like image 5
gnovice Avatar answered Nov 01 '22 01:11

gnovice