# Gray Level Image Processing

## Interpolation

Sometimes, in some cases, it is necessary to add new pixels between existing pixels of an image. We will take the example of a zoom. An image of 10x10 pixels which we should expand twice in line and twice in column, so the final image will be four times larger than the original image. The final image will have 20x20 pixels. Look at the first example, the pixels to be added are represented in black. We see the need to be able to create pixels. But we will not create the pixels randomly. We will see two types of interpolation.

### Nearest-neighbor

This is the simplest interpolation possible. We put the new pixels at the value of the nearest pixel. example the top left pixel p (1.1) will be associate to new pixels p (2.1), p (1.2) and p (2.2). We see the result on the first example in the middle.

### Bilinear

This interpolation is a little more complex but provides much better results. Here, we will not use one pixel to determine a new pixel, but several pixels. Let's take as an example the following image:

To calculate the new pixels n we take the average of the nearest pixel.
If they have two neighbors:

If they have two neighbors:

The operation must be repeated for the entire image. We can see a result on the first example on the right.

### Program

```function guitranslation clear all; close all; figure(  'name','interpolation',...             'NumberTitle','off',...             'color',[0.3137 0.3137 0.5098]);         a(1)=axes('units','normalized',...     'position',[0.2 0.4 0.2 0.2]); a(2)=axes('units','normalized',...     'position',[0.5 0.4 0.4 0.4]); uicontrol(  'style','pushbutton',...             'string','load',...             'Position', [10 10 50 20],...             'callback',@loadimage); uicontrol(  'style','pushbutton',...             'string','nearest',...             'Position', [100 10 50 20],...             'callback',@nearest); uicontrol(  'style','pushbutton',...             'string','bilineaire',...             'Position', [200 10 50 20],...             'callback',@bilineaire); %parametre initial setappdata(gcf,'n',10); setappdata(gcf,'x',1); setappdata(gcf,'degree',0);       function loadimage(~,~)     % appeler quand appui check box     n = getappdata(1,'n');     p=0;     x=uint8(zeros(n+1));     for i = 1 : n+1         for j = 1 : n+1             p = 255*j/(2*n)+255*i/(2*n);             x(i,j)=p;         end     end         k=8;         setappdata(1,'x',x);     axes(a(1))     imshow(x)     img=uint8(zeros(2*n));     for i = 1: n         for j = 1 : n             img(2*(i-1)+1,2*(j-1)+1)= x(i,j);             img(2*(i-1)+2,2*(j-1)+1)= 0;             img(2*(i-1)+1,2*(j-1)+2)= 0;             img(2*(i-1)+2,2*(j-1)+2)= 0;         end     end         axes(a(2))     imshow(img)     end function bilineaire(hObj,~)      x = getappdata(1,'x');      x=uint16(x);      n = getappdata(1,'n');      img=uint16(zeros(2*n));      for i = 1: n         for j = 1 : n             img(2*(i-1)+1,2*(j-1)+1)= x(i,j);             img(2*(i-1)+2,2*(j-1)+1)= (x(i,j)+x(i+1,j))/2;             img(2*(i-1)+1,2*(j-1)+2)= (x(i,j)+x(i,j+1))/2;             img(2*(i-1)+2,2*(j-1)+2)= (x(i,j)+x(i+1,j)+x(i,j+1)+x(i+1,j+1))/4;         end     end    img=uint8(img);     axes(a(2))     imshow(img)       end function nearest(hObj,~)      x = getappdata(1,'x');      n = getappdata(1,'n');      img=uint8(zeros(2*n));      for i = 1: n         for j = 1 : n             img(2*(i-1)+1,2*(j-1)+1)= x(i,j);             img(2*(i-1)+2,2*(j-1)+1)= x(i,j);             img(2*(i-1)+1,2*(j-1)+2)= x(i,j);             img(2*(i-1)+2,2*(j-1)+2)= x(i,j);         end     end         axes(a(2))     imshow(img)       end end```

### Example

At the left image expand with the new black pixels, at the middle, interpolation to the nearest and at the right bilinear interpolation.

With a real image. at top right, nearest interpolation and at the bottom right, bilinear interpolation.