Time for action – reducing the blocking effect

The process shown here should seem very familiar. Each of the frames in the video will be filtered using a disk filtering element and saved in a new video file, which hopefully will appear less distorted by the blocking effect:

  1. Let's start by importing the video file in our Workspace window:
    >> vIn = VideoReader('testCar.avi')
  2. Now, we have to make a new video file and set its frame rate equal to the one we opened:
    >> vOut = VideoWriter('carSmooth.avi'), % Create output file
    >> vOut.FrameRate = vIn.FrameRate;      % Equal frame rates
    >> open(vOut);                          % Open output
  3. We should also get the number of total frames of the original video, so that we can use it in a for loop:
    >> numF = get(vIn, 'NumberOfFrames'),   % Get size in frames
  4. Finally, we will write a for loop, for smoothing and writing the frames:
    >> for i = 1:numF % For all frames in video
    frame = read(vIn,i); % read i-th frame
    fKernel = fspecial('disk',5); % Create filter kernel
    for j = 1:3 % For all color channels
    % Filter each channel using a 5 pixel radius disk element  
    out(:,:,j,i) =imfilter(frame(:,:,j),fKernel); 
    end % End inner for
    end % End outer for
  5. At this point, matrix variable out contains all 81 frames of the video, smoothed using a 5 pixel radius disk element. Using a disk element, reduces the blocking effect, but also leads to loss of detail (because of smoothing). We can verify this by displaying the original last frame of the video next to its smoothed version:
    >> subplot(1,2,1), imshow(frame), title('Original Frame')
    >> subplot(1,2,2), imshow(out(:,:,:,end)), title('Smoothed Frame')
    Time for action – reducing the blocking effect
  6. An even better idea of the smoothing result can be gained by showing a smaller area of the two images. Let's use a rectangular area in the middle-left part of the image:
    >> original = frame(250:350,10:210,:); % Crop original frame  
    >> filtered = out(250:350,10:210,:,end); % Crop smoothed frame 
    >> figure, subplot(1,2,1), imshow(original),title('Original Area')
    >> subplot(1,2,2), imshow(filtered), title('Smoothed Area')  
    Time for action – reducing the blocking effect

What just happened?

This example demonstrated a simple way to reduce the blocking effect that often appears in videos and images due to compression or resizing. The rationale of this process is quite simple and requires only a few basic steps, most of which you have already learned.

The first two steps handled the input and output video files, using identical functions as before. The step 3 was necessary so that we know how many frames the input video had.

The number of steps were used in the outer for loop of step 4, so that we processed every single one. In this loop, once we assigned a frame to a temporary variable called frame, we filtered each one of its three color channels using the aforementioned filter. The filter was constructed using the function fspecial. Each filtered frame (with index i) was assigned to a new layer of matrix out (also with index i), one color channel at a time.

Once the two for loops are terminated, we visualized our results in steps 5 and step 6. As you can easily observe, the blocking effect was reduced by the filtering process, but as a consequence there was an apparent loss of detail due to smoothing.

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

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