Time for action – deinterlacing with field averaging

This example demonstrates the use of the field averaging technique. The code in our function will be based on the previous example, but this time we have to make some minor adjustments to fit our problem. Let's see the function that performs field averaging:

function [vid] = FieldAverage(vid,order)
 
% Function for de-interlacing a video using Field Average
% Inputs:
%        vid     – Input video matrix (we assume color video) 
%        order   - Choice for row replacement
%  (1: odd rows from odd frames,
%   2: even rows from odd frames)
% Output:   
%        vid     - Output video matrix (de-interlaced)
vid = single(vid);  % Convert matrix to single to perform averaging
  for fr = 2:size(vid,4)-1% For all frames (but the first & last)
  for row = 1:size(vid,1) % For all rows in frame
  switch order % Checking choice for the order of merging
    case 1 % Odd rows from odd frames
      if mod(fr,2) == 0 % For even frames
        if mod(row,2) == 0 % Replace even rows
          vid(row,:,:,fr) = ...(vid(row,:,:,fr-1) + vid(row,:,:,fr+1)) / 2 ;
        end
      else % For odd frames
        if mod(row,2) ~= 0 % Replace odd rows
          vid(row,:,:,fr) = ...(vid(row,:,:,fr-1) + vid(row,:,:,fr+1)) / 2 ;
        end
    end
    case 2 % Even rows from odd frames
      if mod(fr,2) == 0 % For even frames
        if mod(row,2) ~= 0   % Replace odd rows
          vid(row,:,:,fr) = ...(vid(row,:,:,fr-1) + vid(row,:,:,fr+1)) / 2 ;
        end
      else % For odd frames
        if mod(row,2) == 0 % Replace even rows
          vid(row,:,:,fr) = ...(vid(row,:,:,fr-1) + vid(row,:,:,fr+1)) / 2 ;
        end
    end
    otherwise
      error('Unknown method.')     % Error message
    end
  end
end
vid = uint8(vid);   % Convert matrix back to uint8

At this point, we may want to check our results on a real interlaced video scene. We have a quite challenging one provided, named inter.avi. It is the driving video stream we used for the still image deinterlacing examples. Let's put our function to the test:

  1. First, we load our video into the Workspace window:
    >> obj = VideoReader('inter.avi'),
  2. Then, we read in all the video frames (the video is small, so it can be done):
    >> vid = read(obj);
  3. This is the part where we put our function to the test. Let's use it once with the second input being 1 and once with the second input being 2:
    >> [vid1] = FieldAverage(vid,1);
    >> [vid2] = FieldAverage(vid,2);
  4. Now, we demonstrate the results for an odd and an even frame:
    >> subplot(2,3,1),imshow(vid(:,:,:,5)),title('Odd frame')
    >> subplot(2,3,4),imshow(vid(:,:,:,6)),title('Even frame')
    >> subplot(2,3,2),imshow(vid1(:,:,:,5)),title('Odd frame-1')
    >> subplot(2,3,3),imshow(vid2(:,:,:,5)),title('Odd frame-2')
    >> subplot(2,3,5),imshow(vid1(:,:,:,6)),title('Even frame-1')
    >> subplot(2,3,6),imshow(vid2(:,:,:,6)),title('Even frame-2')
    Time for action – deinterlacing with field averaging

What just happened?

This time our function had a few tricks. At first, the input video matrix was converted to type single, so that averaging could be performed. We saw this conversion used earlier in the line interpolation example. When the whole process was completed, we converted the matrix back to uint8.

The next alteration we made to our code was to exclude the first frame from the loop. The last frame was excluded in the previous example as well, so we need not change that. Therefore, our loops went through the values of 2:size(vid,4)-1.

Our final change in the code was the rule for replacement. Whatever row we are in, the values of its elements will be replaced by the respective average values of the previous and next frame rows. This is accomplished by the following lines of code:

vid(row,:,:,fr) = … (vid(row,:,:,fr-1)+vid(row,:,:,fr+1))/2;

The three dots are there to indicate that the next line is the continuation of the current line and is not a new command.

The steps 1 to 4 were used for assessment of the method we implemented. The results show that, because of the nature of the scene none of the methods performed very well. In some areas we had a slight improvement of the result, but in other areas we had a deterioration of the result.

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

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