Time for action – making an edge detection video

In this example, we will try to postprocess an already captured video file, with a size that is too big for our memory.

  1. Suppose that we cannot afford 500 free MB of RAM and select the test.avi file we created previously. The processing task will be to convert each frame to grayscale, perform edge-detection and then save the result in a new file.
  2. In order to avoid exceeding our memory limit, we will process our video in small chunks, of 10 frames each, which will be processed and added to a video file. Processing in small chunks accomplishes a trade-off between processing large videos (might lead to a memory insufficiency error) and processing videos one frame at a time (while in edge detection this approach is acceptable, it is often unfeasible, because some processing tasks require more than one frame to work). The following function will accomplish the task of edge detection:
    functionEdgeDetectChunks(inputFn,outputFn,chunkSz)
    
    % Function for edge detection of frames
    % Inputs:
    %        inputFn – Input video filename
    %        outputFn- Output video filename
    %         chunkSz – Size of chunks   
    % Output:   
    %        No output needed!!
    
    vIn = VideoReader(inputFn);    % Open input file
    numF = get(vIn, 'NumberOfFrames'), % Get size in frames
    vOut = VideoWriter(outputFn);  % Create output file
    vOut.FrameRate = vIn.FrameRate;% Equal framerates
    open(vOut);                    % Open output
    
    start = 1;      % Start frame
    stop = chunkSz; % Stop frame
    
    while (stop <= numF) % As long as we don't exceed the frame limit
    frames = read(vIn,[start stop]); % Read a chunk of frames
    for i = 1:size(frames,4)    % For all frames in chunk
    temp = frames(:,:,:,i); % Read a frame
    temp = rgb2gray(temp);  % Convert it to grayscale
    outF = edge(temp);      % Perform edge detection
    outF = single(outF);    % Convert to single
    writeVideo(vOut,outF);  % Write result
    end
    start = start + chunkSz;    % Next chunk start
    stop = stop + chunkSz;      % Next chunk end
    end
    
    close(vOut); % Close output file

What just happened?

The function that we developed for this example may need some further explanation. First of all, the inputs were two strings, one for the input file and one for the output file, and a number that defined the number of frames to be included in our chunks. No output was needed, since our result was saved straightaway to the output video file.

The first five lines of our function opened the video input file and created an output video file with the same frame rate. Finally, the output file is opened so we can write on it.

The next couple of lines initialized the limits of our first chunk of frames. It started at the first frame and ended at the frame number that is equal to the defined size.

Next, the function entered a while loop, where all the processing will take place. The while condition (stop <= numF) told our program to keep entering the loop until the maximum limit for our chunk exceeded the total number of frames in our video. Just before each time the loop reached its end, the limits were increased by a constant chunkSz so that we moved to the next chunk of frames (see the two highlighted lines of code).

Inside the while loop, our function read the chunk of frames defined by our start and stop values and entered a for loop that processed each of the frames. The processing that took place is a conversion to grayscale, followed by edge detection. The resulting image was converted to type single, so that it could be used in a video. Finally, the edge detection result was saved as a new frame to our output file.

Finally, when our upper limit variable (stop) has exceeded the number of frames in the video, the function closed the output file and ended.

Have a go hero – getting the last chunk of frames processed

Now that you have spotted the weakness of our function, try to fix it by adding a fail-safe scenario. You should make sure that the last chunk of frames is processed no matter what. The way to verify the correctness of your code will be to compare the frame numbers of your output and input files. These two should be equal, even with a chunk size that is not a factor of the total number of frames. While you are at it, you could also experiment with other edge detection techniques, or even edge detection in all three color channels.

Pop quiz – what is the problem with our function?

Q1. Is the following statement correct?

  1. The resulting output video from the function implemented above may not have an equal number of frames as our original one (used as input).
..................Content has been hidden....................

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