Monday, July 15, 2013

Image zooming with MATLAB Sample Codes


Here I will explain two algorithms of image processing.They are Nearest-neighbor interpolation and Bilinear interpolation.When an image is zoom its' dimensions are larger than the original image.To fill the extra pixels' intensity levels of the zoom image we can use above mentioned algorithms.

Nearest neighbour Interpolation

Following picture shows original image is scaled twice without filling intermediate pixels and after filling intermediate pixels respectively





In Nearest neighbor algorithm intensity value of the v(x,y) is assigned to the nearest intensity value of the original image f(x,y).You can easily understand the logic behind this by referring following image.


You will understand the algorithm furthermore by studying following matlab code.



Original Image before zoom

Zoom Image using Nearest-Neighbor Algorithm


Bilinear Interpolation


If you familiar with linear interpolation, then it is easy to understand what is bilinear interpolation. So lets look what is linear interpolation by going through this link.


Now you are familiar with linear interpolation. It is use to find a point between two points in a two dimensional space. As the word says bilinear interpolation is used to find a point of between two functions on a 2D grid.




Following is the Matlab code for image zooming using Bilinear Interpolation.

clc
clear all
close all
a=imread('Slanka.jpg');      %import image"y.jpg"

[row col d] = size(a);  %3 dimentional array
zoom=3;                 %zooming factor
zr=zoom*row;
zc=zoom*col;

for i=1:zr
    
    x=i/zoom;
    
    x1=floor(x);
    x2=ceil(x);
    if x1==0
        x1=1;
    end
    xint=rem(x,1);
    for j=1:zc
        
        y=j/zoom;
        
        y1=floor(y);
        y2=ceil(y);
        if y1==0
            y1=1;
        end
        yint=rem(y,1);
        
        BL=a(x1,y1,:);
        TL=a(x1,y2,:);
        BR=a(x2,y1,:);
        TR=a(x2,y2,:);
        
        R1=BR*yint+BL*(1-yint);
        R2=TR*yint+TL*(1-yint);
        
        im_zoom(i,j,:)=R1*xint+R2*(1-xint);
    end
end
imshow(im_zoom);

Original Image before zoom

After zooming Bilinear Interpolation





1 comment: