Time for action – using MATLAB as an intervalometer

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:

  1. We first reset our hardware devices and set the video input to our preferred hardware and resolution:
    >> imaqreset
    >> vidObj = videoinput('winvideo', 1, 'dvsd_720x576'),
  2. It is a good idea to preallocate space for the matrix that will store our 480 frames, since this will be an early indication of whether you have enough memory. Our matrix should be of type 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));
  3. Now, it is time to write the intervalometer code. It will be a 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
  4. After 8 hours, your time-lapse video will be ready! All its frames will have been stored in matrix 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)
  5. Hopefully, our time-lapse video is what we expected to see. If we do not save it now, we will have wasted 8 hours of our lives for nothing. So, let's use VideoWriter to save it in compressed, MP4 format. First, we must create a new video file, assign an object to it, set the frame rate to 25 fps and open it:
    >> vidObj2 = VideoWriter('AcquiredTimelapse.mp4','MPEG-4'),
    >> vidObj2.FrameRate = 25;
    >> open(vidObj2);
  6. Now that we have created the video object, it is time to write our 480 frames to it:
    >>f or k = 1:size(timelapse,4)  % For all the frames 
    writeVideo(vidObj2,timelapse(:,:,:,k)); % Write k-th frame to file
    end
  7. Finally, we have to close our video object, so that we finalize our process:
    >>close(vidObj2);

What just happened?

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.

Have a go hero – creating a time-lapse creation function

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset