Thursday, January 5, 2012

Zoom Images : Nearest Neighbour & Bilinear Interpolation


The method zoom() will get the image name and the zoom factor as arguments and produce the required zoomed image using both Nearest neighbour and Bilinear interpolation methods to help us compare the results.

In nearest neighbour method, I just repeated the rows and columns to get the required zoom level. 

In bilinear interpolation method, first I copied the original pixel values to a new matrix and did the interpolation in two steps. First, I interpolated the columns (in x-direction) using original pixel values. Then, I interpolated the rows (in y-direction) which finished the bilinear interpolation.

Matlab Code

function zoom (name,n)
    close all
    im=imread(name);
    im=rgb2gray(im);
    imshow(im);
    [r,c]=size(im);
    % NEAREST NEIGHBOUR
    x=zeros(r*n,c*n);
    for i=1:r
        for j=1:c
            x(i*n-n+1:i*n,j*n-n+1:j*n)=repmat(im(i,j),n,n);
        end
    end
    figure
    subplot (1 ,2 ,1)
    imshow(mat2gray(x));
    title('Nearest Neighbour');
    % BILINEAR
    im = im2double(im);
    [r,c]=size(im);
    y=zeros(r*n-n+1,c*n-n+1);
    % COPY THE ORIGINAL VALUES
    for i=1:r
        for j=1:c
            y(i*n-n+1,j*n-n+1)=im(i,j);            
        end
    end         
    % INTERPOLATE IN X-DIRECTION
    [row,col]=size(y);
    for i=1:n:row
        for j=1:col
            m=mod(j,n);
            if(m==0)
                m=n;
            end
            if(m~=1)
                y(i,j)=((m-1)*y(i,j-(m-1)+n) + (n-(m-1))*y(i,j-(m-1)))/n;
            end
        end
    end      
    % INTERPOLATE IN Y-DIRECTION
    for i=1:row
        for j=1:col
            m=mod(i,n);
            if(m==0)
                m=n;
            end            
            if(m~=1)
                y(i,j)=((m-1)*y(i-(m-1)+n,j) + (n-(m-1))*y(i-(m-1),j))/n;
            end
        end
    end
    subplot (1 ,2 ,2)
    imshow(y);
    title('Bilinear');
end

Output


You can notice that, bilinear interpolation method gives more smooth output.


18 comments:

  1. here what is the 'n' mean?

    ReplyDelete
    Replies
    1. Here the 'n' means the scale of the zoom. 2x, 3x like that, where n is 2 or 3.

      Delete
  2. simple and lucid explanation

    ReplyDelete
  3. i am getting error input argument n is undefined

    ReplyDelete
  4. can you please insert bicubic method also?

    ReplyDelete
  5. here it doesnt zoom the image...i need the image to be zoomed using bilinear interpolation....

    ReplyDelete
    Replies
    1. It zooms the image, when you save the image you can verify.
      -> before the "end" keyword, add "imwrite(y,'zoomed.jpg','jpg');" and run the code to save the result.

      Delete
  6. Good explanation!! Just, I don't get the formulas, how did you make them??? Sorry!!

    ReplyDelete
    Replies
    1. http://www.cambridgeincolour.com/tutorials/image-interpolation.htm
      This may be helpful...

      Delete
  7. I was curious about checking the performance of the algorithm in terms of the execution time. I tried interpolating a simple image (using n=3) using the Nearest Neighbor and the algorithm took about 5.666231 seconds to generate the final image (the time, however, will depend on the hardware and software constraints). I developed an alternative code which, for the same machine constraints, took about 0.253769 seconds to reach the same results. Here is my code:

    function [ scaledImg ] = imgResize( inputImg, factor, method )
    %UNTITLED2 Summary of this function goes here
    % Detailed explanation goes here

    [rows, cols, channel] = size(inputImg);
    scaledImg=zeros(rows*factor, cols*factor, channel);

    switch method
    case 0
    tic
    for m=1:channel
    for i=1:(rows*factor)
    for j=1:(cols*factor)
    x=floor(i/factor);
    y=floor(j/factor);
    if x==0
    x=1;
    end
    if y==0
    y=1;
    end
    scaledImg(i,j,m) = inputImg(x,y,m);
    end
    end
    end
    toc
    end

    size(scaledImg)
    figure(1);imshow(inputImg, []);
    figure(2);imshow(uint8(scaledImg), []);
    end

    ReplyDelete
  8. how to do bicubic interpolation

    ReplyDelete
  9. HOW TO PERFORM CUBIC B SPLINE INTERPOLATION???

    ReplyDelete
  10. Thank you. This simple code helped me a lot.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. please give bicubic interpolation code

    ReplyDelete
  14. i need codes on bilinear and bicubic interpolation, anyone help????

    ReplyDelete