acquiring a single image in a loop -凯发k8网页登录
this example shows how to use the getsnapshot function. the getsnapshot function allows for quick acquisition of a single video frame.
set up the acquisition object
most interaction with image acquisition toolbox is done through a video input object. these objects are created with the videoinput command. this example uses a webcam that is accessed through the toolbox's "winvideo" adaptor.
vidobj = videoinput('winvideo');
acquire a frame
to acquire a single frame, use the getsnapshot function.
snapshot = getsnapshot(vidobj);
% display the frame in a figure window.
imagesc(snapshot)
acquire multiple frames
a common task is to repeatedly acquire a single image, process it, and then store the result. to do this, getsnapshot can be called in a loop.
for i = 1:5 snapshot = getsnapshot(vidobj); imagesc(snapshot); end
timing implications
the getsnapshot function performs a lot of work when it is called. it must connect to the device, configure it, start the acquisition, acquire one frame, stop the acquisition, and then close the device. this means that the acquisition of one frame can take significantly longer than would be expected based on the frame rate of the camera. to illustrate this, call getsnapshot in a loop.
% measure the time to acquire 20 frames. tic for i = 1:20 snapshot = getsnapshot(vidobj); end elapsedtime = toc % compute the time per frame and effective frame rate. timeperframe = elapsedtime/20 effectiveframerate = 1/timeperframe
elapsedtime = 21.2434 timeperframe = 1.0622 effectiveframerate = 0.9415
the next example illustrates a more efficient way to perform the loop.
using manual trigger mode
you can avoid the overhead of getsnapshot described in the previous setting by using the manual triggering mode of the videoinput object. manual triggering mode allows the toolbox to connect to and configure the device a single time without logging data to memory. this means that frames can be returned to matlab® with less of a delay.
% configure the object for manual trigger mode. triggerconfig(vidobj, 'manual'); % now that the device is configured for manual triggering, call start. % this will cause the device to send data back to matlab, but will not log % frames to memory at this point. start(vidobj) % measure the time to acquire 20 frames. tic for i = 1:20 snapshot = getsnapshot(vidobj); end elapsedtime = toc % compute the time per frame and effective frame rate. timeperframe = elapsedtime/20 effectiveframerate = 1/timeperframe % call the stop function to stop the device. stop(vidobj)
elapsedtime = 1.4811 timeperframe = 0.0741 effectiveframerate = 13.5031
you can see that the elapsed time using manual triggering is much smaller than the previous example.
cleanup
once the video input object is no longer needed, delete the associated variable.
delete(vidobj)