In this example, we will try to postprocess an already captured video file, with a size that is too big for our memory.
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.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
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.
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.