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:
>> vIn = VideoReader('testCar.avi')
>> vOut = VideoWriter('carSmooth.avi'), % Create output file >> vOut.FrameRate = vIn.FrameRate; % Equal frame rates >> open(vOut); % Open output
for
loop:>> numF = get(vIn, 'NumberOfFrames'), % Get size in frames
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
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')
>> 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')
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.