Time for action – deinterlacing with field merging

For this example, we are going to make a simple function implementing the field merging technique. We will assume that our video is small and given as a matrix variable input to the function, and we are also going to include a second input that will help us decide which rows to start replacing. If the input is 1, the odd rows of the first frame will be replaced by the odd rows from the second frame and the even rows of the second frame will be replaced by the even rows of the first frame. If the input is 2, the even rows of the first frame will be replaced by the even rows from the second frame, and the odd rows of the second frame will be replaced by the odd rows of the first frame. Finally, the output will be the deinterlaced video.

  1. Let's write the function implementing the field merging technique:
    function [vid] = FieldMerge(vid,order)
     
    % Function for de-interlacing a video using Field Merging
    % 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)
     
    for fr = 1:size(vid,4)-1% For all frames (but the 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);
            end
          else % For odd frames
            if mod(row,2) ~= 0 % Replace odd rows
              vid(row,:,:,fr) = vid(row,:,:,fr+1);
            end
          end
        case 2
          if mod(fr,2) == 0 % For even frames
              if mod(row,2) ~= 0    % Replace odd rows
                vid(row,:,:,fr) = vid(row,:,:,fr-1);
              end
          else                % For odd frames
          if mod(row,2) == 0 % Replace even rows
            vid(row,:,:,fr) = vid(row,:,:,fr+1);
          end
          end
        otherwise
          error('Unknown method.')     % Error message
        end
      end
    end

What just happened?

The function we just wrote performs deinterlacing based on the process of field merging. It may seem a little complicated, but it really is simple. The first thing the function does is check the choice of order for the row replacement. This check is performed by the switch statement, with two possible acceptable results for case 1 and case 2 (all other inputs will result to an error message being generated by the otherwise command).

When the input is equal to 1, we have to replace the odd rows of the odd frames with the odd rows of the next available frame. Similarly, we will replace the even rows of even frames with the even rows of the previous available frame. We have already used the Modulo 2 command to differentiate odd and even rows in previous examples. We just have to also use it here to differentiate odd and even frames. This is why we used the mod function twice in the for loop; once for the frame (fr) and once for the row (row). The two highlighted lines of code actually did the replacement of even and odd rows.

The process when the input is equal to 2 is identical to the one described in the previous code. The only difference is in the if clauses checking for odd and even rows to replace. This time, odd rows are substituted in even frames and even rows in odd frames.

You may have noticed that in the previous examples we used if and else for the replacement of the rows of the image, while in this example we only used a single if. This is because this time we replaced the rows on the input matrix itself. Therefore, the rows that do not need replacement remain intact; hence skipping the else part of the code.

Note

Note that this method skips the last frame. This can be avoided in half the cases since, for example, the last frame of an even frame scene with order = 1 can be deinterlaced, while for order = 2 cannot (it looks for the next frame and falls off the frame limits). Our previous code does not include this check and therefore always skips the last frame to be safe. This choice was made to reduce complexity in our code.

Have a go hero – evaluating the field merge method

Now it is time for you to check if our code works or not. You should find, or shoot an interlaced video, load it in MATLAB (be careful not to use a video that is too big) and then call the function FieldMerge and compare some frames from its output to the frames of the original video stream. Try to use the function in both the still scenes and motion scenes. What do you see? Is there a difference? What about changing the second input to 2 instead of 1?

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

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