Time for action – creating time-lapses with isolated colors

For this example, we will create a time-lapse video with the red color isolated in the scene. We will use a frame rate of 2 frames per minute and perform the acquisition for 2 hours. This will create a video consisting of 2 (frames/minute) * 2 hours * 60 (minutes / hour) = 240 frames. Between two consecutive acquisitions, the first acquired frame will be processed for color isolation:

  1. The first step is to reset our hardware devices and set the video input to our preferred hardware and resolution:
    >> imaqreset
    >> vidObj = videoinput('winvideo', 1, 'dvsd_720x576'),
  2. Next, we will preallocate the space needed for the 240 frames of our video:
    >> tl = uint8(zeros(576,720,3,240));
  3. Next, we should write a small function to perform the color isolation. It is a simplified version of the ROIColorIsolation.m function developed for Chapter 4, Working with Color Images. It goes as follows:
    function [output] = ColorIsolation(image,thresh)
    
    % Function for color isolation in an image % Inputs:
    %        image  - Input image
    %        thresh - Thresholds matrix ([1st 2nd 3rd])
    % Output:   
    %        output - Output image (masked)
    
    R = image(:,:,1);   % Separate red channel
    G = image(:,:,2);   % Separate green channel
    B = image(:,:,3);   % Separate blue channel
    grayIm = rgb2gray(image); % Keep grayscale version of image
    
    % Create mask from three thresholds
    mask = R < thresh(1) & G < thresh(2) & B < thresh(3);
    
    % Perform masking 
    R(mask==0) = grayIm(mask==0);
    G(mask==0) = grayIm(mask==0);
    B(mask==0) = grayIm(mask==0);
    
    % Join color channels to generate final image
    output = cat(3,R,G,B);
  4. It is now time for the core of our processing program. The essence of the for loop remains almost the same as before. The only thing we must do is to add a step for the color isolation. Let's isolate colors with a green value higher than 100:
    >> for i = 1:240
    temp = getsnapshot(vidObj);  % Acquire a frame
    fprintf('Processing frame number %d… 
    ',i) % Announcement 
    tl(:,:,:,i) = ColorIsolation(temp,[0 100 0]);% Perform isolation
    subplot(1,2,1),imshow(temp ) % Display current frame
    subplot(1,2,2),imshow(tl(:,:,:,i)) % Display processed frame
    pause(30) % Wait for 30 seconds
    end
  5. At this point, we have a matrix called tl, which holds the frames of our time-lapse video. If we want to save it, we can repeat the process of the previous example:
    >> vid = VideoWriter('TimelapseIsolation.mp4','MPEG-4'),
    >> vid.FrameRate = 25;
    >> open(vid);
    >> for k = 1:size(tl,4) % For all the frames 
    writeVideo(vid,tl(:,:,:,k)); % Write k-th frame to file
    end
    >> close(vid);

What just happened?

In this example, we performed a mixture of methods for color image processing and video acquisition. A core function that performs color isolation was written, creating a mask from three user-defined thresholds and using it to remove the colors from all the pixels below the thresholds (making all their colors equal to the pixel values of the grayscale image). This function was then incorporated in a for loop that performs frame acquisition, processing, and displaying with an interval of 30 seconds. In the fifth and final step, the created video was saved to a MP4 video file, using the process described in previous examples.

Tip

Note that the process performed does not lead to a video with frames with an exact time delay of 30 seconds. This is the natural result, caused by the delay added to our process from the commands executed between the frame acquisition and the pause function. A quick solution for more precise timing could be accomplished by timing the delay using tic and toc, and subtracting it from the time delay included in pause. The same change should be also made to previous examples using the same rationale.

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

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