Making a time-lapse video can also be as simple as skipping several frames from a video, to keep only the number of frames per second that will achieve the desired effect. Let's see how we can achieve this.
VideoReader
to import a 2-minute driving video called car2min.avi
into MATLAB:>> E=VideoReader('car2min.avi'),
>> k = 1; % This will be used as a counter for the frames we keep >> for i = 1:12:E.NumberOfFrames % Visit every 12th frame v(:,:,:,k)=read(E,i); % Save the frame in the kth position of v k=k+1; % Increase the counter by 1 end
v
. We can now inspect it using montage
:>> montage(v,'Size',[7 15])
>> lapse = VideoWriter('timelapse.avi'), >> lapse.FrameRate = 15; >> open(lapse); >> for i = 1:size(v,4) writeVideo(lapse,v(:,:,:,i)); end close(lapse);
implay
:>> implay('timelapse.avi')
Congratulations! You have just made your first time-lapse video. Of course, it was a little less sophisticated than the average videos you might have seen in the documentaries, but nevertheless, it is a first step. You have achieved it by first importing a regular video and then selecting a large enough step to take sample frames from this video and save them in a new matrix. Finally, after inspecting all the frames of your newly constructed matrix using montage
, you looped through all the frames to save them in a new video file with a frame rate of 15 fps. In the following chapters, we will get into the details of how to shoot the frames for a time-lapse using a photographic camera, a USB cable and a laptop with MATLAB.
So, now that you know how to make a time-lapse, why not use another little trick to make it spin? You should try to rotate each frame by a fixed, arbitrary angle, and then save the result using the process you have already learned. If you do it correctly, the size of each frame will remain unaffected and you will have in your hands, a spinning timelapse video. Make sure that the angle you choose is not very small, or very large, so that the result is as smooth as possible.