Time for action – deinterlacing with line repetition

The first method we will implement will be deinterlacing with line repetition. This method is based on repeating the odd (or even) rows of the image to fill the blank even (or odd) ones, respectively. In this example, we will replace each even line in the image with the previous odd one. Let's start:

  1. First, we load our original, interlaced image:
    >> A = imread('interlaced.bmp'),  % Load interlaced image
  2. Then, we initialize with zero values a matrix of equal size to A, so that we can store the deinterlaced image, which is as follows:
    >> B = uint8(zeros(size(A)));  % Pre-allocate space for the result
  3. Next, we must perform the line repetition process, using a for loop for all rows of the image. The even rows of matrix B will be replaced by the previous odd row of matrix A, while the odd rows of both images will be equal:
    >> for i = 1:size(A,1),   % For all rows in A
    if mod(i,2) == 0,       % if i is even
    B(i,:,:) = A(i-1,:,:);  % Replace i-th row of B with (i-1)-th of A
    else     % if i is odd	
    B(i,:,:)= A(i,:,:);    % Replace i-th row of B with i-th row of A
    end     % End if
    end    % end for
  4. Now, matrix B should have the deinterlaced version of image A in it. In the preallocation step we used uint8 as the type of the elements, so we will need no further processing. Let's display the cropped area result again:
    >> A2 = imcrop(A,[480 400 200 100]); % Crop interlaced image
    >> B2 = imcrop(B,[480 400 200 100]); % Crop deinterlaced frame
    >> subplot(1,2,1),imshow(A2);title('Cropped Interlaced Area')
    >> subplot(1,2,2),imshow(B2);title('CroppedDeinterlaced Area') 
    Time for action – deinterlacing with line repetition

What just happened?

This time we wrote our own deinterlacing code, based on functions we already knew and one little trick to check if the row we are checking in the for loop is odd or even. After we loaded the image we have been using for our deinterlacing examples, we created some space for us to store the result of our process. The result was an 8-bit per channel color image, that's why we forced matrix B to be uint8. In the beginning it didn't have to include specific values (although it could be set equal to A to save some lines of code), so we set all of them to zero, using the zeros function.

Our main procedure comprised a for loop, that will let us visit all rows in the image. As already described, the odd lines of the result should be identical to the odd lines of the input, while the even rows of the result should be equal to the previous odd rows of the input. To accomplish this, we needed to use a trick based on discrete mathematics. The trick is to check the result of the Modulo 2 operation, which in MATLAB is calculated using the mod function. The first input of the function is the number we want to check and the second being the base of the operation. The function returns the remainder of the division of the first input by the second input In the case of even numbers being divided by 2, the remainder is always zero; hence this is the check we perform in the first highlighted line of code to see if i is even. If it is, we make the i-th row of matrix B (the result), equal to the (i-1)-th row of the input image A. In the opposite case, row i of matrix B is assigned the values of row i of matrix A.

Finally, we followed the same steps as before to compare a cropped area of the original image to the same cropped area of the resulting image. The result was as expected and should actually be equivalent to the one produced by applying the default Deinterlacer object to the input.

Tip

There is an easy way to check if the result of our code and the result from the previous example using the line repetition method are equal. To do it, you should subtract them and check if the result of the subtraction is a matrix containing only zero values, or alternatively use the function isequal.

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

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