Image Acquisition Toolbox 3.4
Creating Time-Lapse Video Using Timer Events
The Image Acquisition Toolbox™ makes it easy to produce time-lapse video. In this demo, we will use timer events to acquire frames to an AVI file. This technique of time decimation has the advantage that it allows you to make a decision about each frame before storing it. An application of this would be to only store frames that meet certain illumination levels, have motion relative to the previous frame, etc.
Watch a day long time-lapse sequence. (21 seconds)
Contents
Create a Video Input Object
Before acquiring images using the Image Acquisition Toolbox, create a video input object.
% When executing the following code, you may need to % modify it to match your acquisition hardware. vid = videoinput('winvideo',1,'RGB24_400x300');
Configure the Timer
To generate timer events, we specify two things: what happens when it occurs, and how often it should occur. The TimerFcn property specifies the m-file callback function to execute when a timer event occurs. A timer event occurs when the time period specified by the TimerPeriod property expires.
The configuration we will use is
- timelapse_timer will execute each time the timer elapses
- The timer function will execute every one second
set(vid,'TimerPeriod',1); set(vid,'TimerFcn',@timelapse_timer);
Store the AVI File Object
Store the AVIFile object in the UserData property of the video input object so that it will be available inside the callback.
avi = avifile('timelapsevideo'); set(vid,'UserData',avi);
Configure the Video Input Object to Use Manual Triggering
Each time the timer event occurs
- Manually trigger the acquisition using the triggerconfig command
- Acquire one frame
- Acquire 9 additional triggers worth of data, for a total of 10 frames
triggerconfig(vid, 'manual'); set(vid,'FramesPerTrigger',1); set(vid,'TriggerRepeat',9); vid
Summary of Video Input Object Using 'Creative WebCam (WDM)'.
Acquisition Source(s): input1 is available.
Acquisition Parameters: 'input1' is the current selected source.
1 frames per trigger using the selected source.
'RGB24_400x300' video data to be logged upon STAR
T.
Grabbing first of every 1 frame(s).
Log data to 'memory' on trigger.
Trigger Parameters: 10 'manual' trigger(s) upon TRIGGER.
Status: Waiting for START.
0 frames acquired since starting.
0 frames available for GETDATA.
Perform the Time-Lapse Acquisition
Now, start the time lapse acquisition, and wait for up to 20 seconds for the acquisition to complete.
start(vid); wait(vid,20);
Close the AVI File
Once the capture is completed, retrieve the AVI file object stored in the UserData property, and use the close function to release the resources associated with it.
avi = get(vid,'UserData');
avi = close(avi);
Play Back the Time-Lapse AVI Sequence
To play back the time-lapse AVI sequence, right-click on the filename in the MATLAB® Current Directory browser, and choose Open Outside MATLAB from the context menu.
Clean Up
When you are done with the video input object, you should use the delete function to free the hardware resources associated with it, and remove it from the workspace using the clear function.
delete(vid);
clear vid;
The Timer Callback Function
The following is a description of the callback function executed for each timer event
- Trigger the toolbox to acquire a single frame
- Retrieve the frame
- Determine whether to keep the frame
- Use the addframe function to add the frame to the AVI file
The AVI file object is stored in the UserData property of the object.
type timelapse_timer
function timelapse_timer(vid,event)
% trigger the acquisition and get the frame
trigger(vid);
frame = getdata(vid,1);
% Retrieve the total number of frames acquired
numframes_acquired = get(vid,'FramesAcquired');
% Drop every other frame: If the frame is odd,
% keep it. If it is an even frame, do not keep it.
keepframe = (mod(numframes_acquired,2) == 1);
% Insert your processing code here
if(~keepframe)
return;
end
% Retrieve the AVI file object stored in the UserData
% property.
avi = get(vid,'UserData');
% Add the frame to the AVI
avi = addframe(avi,frame);
% Update the AVI file object stored in the UserData
% property.
set(vid,'UserData',avi);
end
Store