Creating Time-Lapse Video Using Postprocessed Data
This example shows how to create a time-lapse video by deleting unnecessary frames during postprocessing.
The Image Acquisition Toolbox™ makes it easy to produce time-lapse photography. In this example, we will use postprocessing to delete frames from an acquired sequence of images. This is ideal for situations where you are not sure which frames are relevant during capture, or where your processing would take too long to occur during the acquisition. A possible application would be to delete frames that have no motion relative to the previous frame. The primary disadvantage of this method of time decimation is that it requires large amounts of memory to store all the frames. This example acquires to memory, but you would likely acquire to an AVI file, and then use the VideoReader command to postprocess the frames.
Watch a day long time-lapse sequence. (21 seconds)
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_352x288');
The configuration we will use is
- Acquire 100 frames
vid.FramesPerTrigger = 100; vid
Summary of Video Input Object Using 'Logitech QuickCam Fusion'. Acquisition Source(s): input1 is available. Acquisition Parameters: 'input1' is the current selected source. 100 frames per trigger using the selected source. 'RGB24_352x288' video data to be logged upon START. Grabbing first of every 1 frame(s). Log data to 'memory' on trigger. Trigger Parameters: 1 'immediate' trigger(s) on START. Status: Waiting for START. 0 frames acquired since starting. 0 frames available for GETDATA.
start(vid); wait(vid); framesavailable = vid.FramesAvailable;
framesavailable = 100
frames = getdata(vid,framesavailable);
Here, simply remove every other frame. However, you could do processing that is much more complex.
toberemoved_index = [2:2:framesavailable]; frames(:,:,:,toberemoved_index) = ; numframes = size(frames,4)
numframes = 50
Render the frames to an AVI file. To do this, create a |VideoWriter| object, call the |writeVideo| function to add all the frames to the AVI file, and then use the |close| function to release the resources associated with the AVI file.
vwObj = VideoWriter('timelapsevideo', 'Uncompressed AVI'); vwObj.FrameRate = 15; open(vwObj); writeVideo(vwObj, frames); close(vwObj);
To play back the time-lapse AVI sequence, right-click on the filename in the MATLAB® Current Folder browser, and choose Open Outside MATLAB from the context menu.
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. Also delete and clear the VideoWriter object.
delete(vid); delete(vwObj); clear vid vwObj;