Skip to content

How do I Acquire Images at Timed Intervals using MATLAB?

An answer to this question on Stack Overflow.

Question

I'm a MATLAB beginner and I would like to know how I can acquire and save 20 images at 5 second intervals from my camera. Thank you very much.

Answer

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 start up the interface:

start(vid);
preview(vid);

Now you can do the following:

pics=cell(1,20)
for i=1:20
   pause(5);
   pics{i}=getsnapshot(vid);
end

Or, as other commentators have noted, you could also use a Matlab timer for the interval.

If you wish to capture images with a considerably shorter interval (1 or more per second), it may be more useful to consider the webcam as a video source. I've left an answer to this question which lays out methods for achieving that.