User`s guide

Getting Started Doing Image Acquisition Programmatically
1-19
a difference image, and then displays it. Pixels that have changed values in the acquired
frames will have nonzero values in the difference image.
The getdata function removes frames from the memory buffer when it brings them into
the MATLAB workspace. It is important to move frames from the memory buffer into the
MATLAB workspace in a timely manner. If you do not move the acquired frames from
memory, you can quickly exhaust all the memory available on your system.
Note The example uses functions in the Image Processing Toolbox software.
% Create video input object.
vid = videoinput('dcam',1,'Y8_1024x768')
% Set video input object properties for this application.
vid.TriggerRepeat = 100;
vid.FrameGrabInterval = 5;
% Set value of a video source object property.
vid_src = getselectedsource(vid);
vid_src.Tag = 'motion detection setup';
% Create a figure window.
figure;
% Start acquiring frames.
start(vid)
% Calculate difference image and display it.
while(vid.FramesAvailable >= 2)
data = getdata(vid,2);
diff_im = imabsdiff(data(:,:,:,1),data(:,:,:,2));
imshow(diff_im);
drawnow % update figure window
end
stop(vid)
Note that a drawnow is used after the call to imshow in order to ensure that the figure
window is updated. This is good practice when updating a GUI or figure inside a loop.
The following figure shows how the example displays detected motion. In the figure,
areas representing movement are displayed.