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.

Was it helpful?

Solution

To acquire the image, does the camera comes with some documented way to control it from a computer? MATLAB supports linking to outside libraries. Or you can buy the appropriate MATLAB toolbox as suggested by MatlabDoug.

To save the image, IMWRITE is probably the easiest option.

To repeat the action, a simple FOR loop with a PAUSE will give you roughly what you want with very little work:

 for ctr = 1:20
   img = AcquireImage(); % your function goes here
   fname = ['Image' num2str(ctr)]; % make a file name
   imwrite(img, fname, 'TIFF');
   pause(5); % or whatever number suits your needs
 end

If, however, you need exact 5 second intervals, you'll have to dive into TIMERs. Here's a simple example:

function AcquireAndSave
  persistent FileNum;
  if isempty(FileNum)
    FileNum = 1;
  end
  img = AcquireImage();
  fname = ['Image' num2str(FileNum)];
  imwrite(img, fname, 'TIFF');
  disp(['Just saved image ' fname]);
  FileNum = FileNum + 1;
end

>> t = timer('TimerFcn', 'ShowTime', 'Period', 5.0, 'ExecutionMode', 'fixedRate');
>> start(t); 
...you should see the disp line from AcquireAndSave repeat every 5 seconds...
>> stop(t);
>> delete(t);

OTHER TIPS

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.

There are several ways to go about this, each with advantages and disadvantages. Based on the information that you've posted so far, here is how I would do this:

vid = videoinput('dcam', 1'); % Change for your hardware of course.
vid.FramesPerTrigger = 20;
vid.TriggerRepeat = inf;
triggerconfig(vid, 'manual');
vid.TimerFcn = 'trigger(vid)';
vid.TimerPeriod = 5;
start(vid);

This will acquire 20 images every five seconds until you call STOP. You can change the TriggerRepeat parameter to change how many times acquisition will occur.

This obviously doesn't do any processing on the images after they are acquired.

Here is a quick tutorial on getting one image http://www.mathworks.com/products/imaq/description5.html Have you gotten this kind of thing to work yet?

EDIT:

Now that you can get one image, you want to get twenty. A timer object or a simple for loop is what you are going to need.

Simple timer object example

Video example of timers in MATLAB

Be sure to set the "tasks to execute" field to twenty. Also, you should wrap up all the code you have for one picture snap into a single function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top