Time for action – spatiotemporal averaging filter with the convn function

In order to see how the averaging filter is implemented, we will use the same example as before. We will follow these steps:

  1. First off, we load our video and convert it to grayscale, using the first two steps of the previous example:
    >> obj = VideoReader('inter.avi'),
    >> vid = read(obj);
    >> grayVid = uint8(zeros(size(vid,1), size(vid,2), size(vid,4)));
    >> for i = 1:size(vid,4),
    grayVid(:,:,i) = rgb2gray(vid(:,:,:,i));
    end
  2. Now, we must generate a three-dimensional filter to use for the averaging process. Its dimensions can be the same as the neighborhood we used before. To perform averaging, it must have all its values equal to 1/n, where n are the number of elements in the filter. Let's create it:
    >> avFilt = ones(3,3,3); % Make a 3x3x3 matrix full of ones
    >> avFilt = avFilt/numel(avFilt); % Make all elements equal to 1/n
  3. All we have to do now is to apply convolution between grayVid and avFilt:
    >> filteredVid = convn(grayVid, avFilt); % Apply convolution
    >> filteredVid = uint8(filteredVid );    % Convert to uint8
  4. Let's demonstrate one frame of the filtered stream next to the same frame from the original video:
    >> subplot(1,2,1),imshow(grayVid(:,:,15)),title('Original frame')
    >> subplot(1,2,2),imshow(filteredVid(:,:,15)),title('Filtered frame')
    Time for action – spatiotemporal averaging filter with the convn function

What just happened?

This was another way to implement spatiotemporal smoothing using an averaging filter. The steps were pretty simple and similar to the processes discussed in Chapter 5, 2-Dimensional Image Filtering. The first step was to prepare our video for the process by converting it to grayscale, frame-by-frame. Once this was over, we created the filter for the averaging process. It was a 3 x 3 x 3 filter with all its values equal to 1 / (3*3*3).

Next, we applied n-dimensional convolution for n=3. The result of the convolution was transformed to uint8, and then one of its frames was demonstrated next to the respective original frame for qualitative evaluation purposes.

Pop quiz – videos and filters

Q1. Which of the following are true?

  1. If we filter an image or a video frame using a disk filtering element, we create the so-called blocking effect.
  2. Interlaced videos take up double the space of progressive ones with the same dimensions, color depth, and number of frames.
  3. Some methods for deinterlacing can lead to ghosting effects.
  4. Spatiotemporal averaging leads to frame sharpening.
  5. Using convn for averaging instead of making a triple nested for loop leads to faster processing speed.
..................Content has been hidden....................

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