Our goal here is to write MATLAB code that will create a time-lapse video. Let's suppose that we want our video to capture frames overnight that is start shooting when we want it to and then shoot 1 frame every minute, for 8 consecutive hours. This will lead to a video comprising 8 x 60 frames, which is 480 frames in total. An uncompressed video with a resolution of 720 x 576 pixels and 8-bits depth per color channel will require approximately 598 MB. Therefore, you should make sure that you have at least 598 MB of RAM available to save it.
It goes without saying, that your camera must be plugged to the power outlet so that we don't face battery problems. If you work with a laptop, it should be plugged in as well. Let's start using MATLAB as an intervalometer:
>> imaqreset >> vidObj = videoinput('winvideo', 1, 'dvsd_720x576'),
uint8
, so that it matches the video frames that will be acquired. To do this, type in the following command:>> timelapse = uint8(zeros(576,720,3,480));
for
loop that executes 480 times. In the loop, we must use getsnapshot
to acquire a frame and then pause to wait for a time interval of 1 minute (60 seconds). Printing out a message that tells us which frame has been captured will also prove useful, as will displaying the current acquired frame:>> for i = 1:480 timelapse(:,:,:,i) = getsnapshot(vidObj); % Acquire a frame fprintf('Just acquired frame number %d… ',i) % Announcement imshow(timelapse(:,:,:,i)) % Display the current frame pause(60) % Wait for 60 seconds end
timelapse
. They are ready for you to play them back, or save them to a video file. Let's first playback the video to see if we are happy with the result:>> implay(timelapse)
>> vidObj2 = VideoWriter('AcquiredTimelapse.mp4','MPEG-4'), >> vidObj2.FrameRate = 25; >> open(vidObj2);
>>f or k = 1:size(timelapse,4) % For all the frames writeVideo(vidObj2,timelapse(:,:,:,k)); % Write k-th frame to file end
>>close(vidObj2);
This previous example demonstrated the power of MATLAB scripting. In just a few lines of code, you managed to program a camera to shoot a time-lapse video at a frame rateof 1 frame per minute for a total of 8 hours, played back your video and then saved it to a compressed video file. Step 1 was rather common, since we used these two commands in previous sections. Step 2 is rather important when you are unsure if your memory will be enough for the video that will be acquired. It is also important in terms of processing speed, since preallocation generally helps in speeding up the execution of your code. Step 3 included all the magic, since it performed the acquisition of the video frames following the requirements set. In order to ensure that our result was what we wanted, we played back our file after 8 consecutive hours of acquisition, in Step 4. Finally, Steps 5 through 7 wrote the video in compressed MP4 format, to a file called AcquiredTimelapse.mp4
.
At this point, you have learned how to create time-lapse videos in MATLAB using various techniques and tools. For this exercise, you should try to embed some of the previous code in a custom time-lapse function that will get the number of frames to be acquired, the time delay between them and a filename string as inputs, and will save the time-lapse video created using the first two inputs to an MP4 video file with the filename given in the third input.