averaging images over time -凯发k8网页登录
this example shows how to average images acquired over time.
for some advanced applications, the acquisition process may require that images be processed as they are acquired, while your processing results are recorded to disk.
using image acquisition toolbox™ callbacks, triggering, and logging features, this example illustrates how to accomplish the following task:
acquire 5 frames every 10 seconds
repeat the acquisition 10 times
while acquiring images, average the acquired frames and save results to disk.
the experimental setup consists of an hourglass with white sand trickling down over time. the example uses a callback function that averages acquired image frames using image processing toolbox™ functions.
configuring the acquisition
create and configure a video input object for the acquisition.
% access a device using a 24 bit rgb format. vid = videoinput('winvideo', 1, 'rgb24_320x240'); % assuming data logging can begin immediately upon start, % an immediate trigger is used. triggerconfig(vid, 'immediate'); % configure the acquisition to collect 5 frames... framespertrigger = 5; vid.framespertrigger = framespertrigger; % ...and repeat the trigger 9 additional times % (for a total of 10 trigger executions). nadditionaltrigs = 9; vid.triggerrepeat = nadditionaltrigs;
to control the rate at which frames will be logged, there are 2 options available:
configure the device frame rate
use a timerfcn to execute a callback
first, a solution using the device's frame rate will be shown, followed by an alternative solution using a timer callback.
using the frame rate option will provide acquisition results that are most closely aligned with the device's actual video stream rate, whereas using the timer approach provides acquisition results independent of the device's streaming rate.
frame rate based acquisition (solution 1)
the device frame rate can only be configured if it is supported by the device. as it is a device specific property, it can be found on the video source object.
% access the video source selected for acquisition. src = getselectedsource(vid); % notice this device provides a framerate property. get(src)
general settings: parent = [1x1 videoinput] selected = on sourcename = input1 tag = type = videosource userdata = [] device specific properties: backlightcompensation = on brightness = 255 brightnessmode = auto contrast = 127 exposure = 511 exposuremode = auto focus = 58 framerate = 15.1500 gamma = 0 iris = 4 saturation = 108 sharpness = 127 whitebalance = 100 whitebalancemode = auto
% using the framerate property, one can configure the acquisition source % to provide the toolbox 30 frames per second. fps = 30; src.framerate = num2str(fps); % since the goal is to acquire 5 frames every 10 seconds, the toolbox % should not acquire any frames until the device provides the 300'th % frame: acqperiod = 10; framedelay = fps * acqperiod
framedelay = 300
% if the trigger is delayed by this value, the toolbox will not buffer % any frames until the 300'th frame is provided by the device. vid.triggerframedelay = framedelay; % to ensure the acquisition does not come close to timing out, configure % the time out value slightly above the expected acquisition duration. totaltrigs = nadditionaltrigs 1; acqduration = (acqperiod * totaltrigs) 3
acqduration = 103
vid.timeout = acqduration;
image averaging
in order to save processed images to disk, a videowriter object is used. each set of acquired frames is averaged using image processing toolbox functions, and then written to disk.
% create an avi file and configure it. vwobj = videowriter('imaverages.avi', 'uncompressed avi'); vwobj.framerate = fps; % use the video input object's userdata to store processing information. userdata.average = {}; userdata.avi = vwobj; vid.userdata = userdata; % configure the video input object to process every 5 acquired frames by % specifying a callback routine that is executed upon every trigger. vid.triggerfcn = {'util_imaverage', framespertrigger}; % now that the image acquisition and processing configuration is complete, % the acquisition is started. start(vid) % wait for the acquisition to complete. this provides the acquisition % time to complete before the object is deleted. wait(vid, acqduration); % verify the averaged frames were saved to the avi file. userdata = vid.userdata; vwobj = userdata.avi; frameswritten1 = vwobj.framecount
frameswritten1 = 10
% display the resulting averages of the acquired frames. % notice the change in the lower chamber of the hourglass over time. imaqmontage(userdata.average); title('averaging results - frame rate based');
% once the video input object is no longer needed, delete % it and clear it from the workspace. also delete and clear the videowriter object. delete(vid) delete(vwobj) clear vid vwobj
timer based acquisition (solution 2)
an alternative solution for this task is to use a timerfcn. the timerfcn can be executed every 10 seconds, at which point 5 frames are acquired and averaged. in order to initiate the acquisition at the correct moment, manual triggers are used.
note, this approach is independent of configuring the device's frame rate.
% access a device and configure the acquisition. have % the timerfcn trigger the acquisition every 10 seconds. vid = videoinput('winvideo', 1, 'rgb24_320x240'); triggerconfig(vid, 'manual'); vid.timerfcn = @trigger; vid.timerperiod = acqperiod; % configure the acquisition to collect 5 frames each time the % device is triggered. repeat the trigger 9 additional times. vid.framespertrigger = framespertrigger; vid.triggerrepeat = nadditionaltrigs; % configure the processing routine and avi file. vid.triggerfcn = {'util_imaverage', framespertrigger}; vwobj2 = videowriter('imaverages2.avi', 'uncompressed avi'); vwobj2.framerate = fps; % use the video input object's userdata to store processing information. userdata2.average = {}; userdata2.avi = vwobj2; vid.userdata = userdata2; % start the acquisition. start(vid); wait(vid, acqduration); % verify the averaged frames were saved to the avi file. userdata2 = vid.userdata; vwobj2 = userdata2.avi; frameswritten2 = vwobj2.framecount
frameswritten2 = 10
% display the resulting averages of the acquired frames. % notice the change in the lower chamber of the hourglass over time. imaqmontage(userdata2.average); title('averaging results - timer based');
% once the video input object is no longer needed, delete % it and clear it from the workspace. also delete and clear the videowriter object. delete(vid) delete(vwobj2) clear vid vwobj2