Introduction:
Through this LAB, I have known the principle and formula of nearest neighbor interpolation and bilinear interpolation, and Use nearest neighbor interpolation and bilinear interpolation to interpolate a grey scale image. At the same time, I find some aspects that can be optimized in these two methods.
Lab results & Analysis:
- Use nearest neighbor interpolation to interpolate a grey scale image:
- Introduction of the nearest neighbor interpolation algorithm:
Figure 1 the area of pixels
Let i + u, j + v (i, j is a positive integer, u, v is a decimal greater than zero and less than 1) be the pixel coordinate we want to get, then the greyscale we want to know is f (i + u, j + v).
If (i + u, j + v) falls in area A, that is u<0.5 and v<0.5, the greyscale of the upper-left pixel will be assigned to the pixel we want to get. Similarly, if it falls in area B, the greyscale of the upper-right pixel will be assigned; if it falls in area C, the greyscale of the lower-left pixel will be assigned; if it falls in area D, the greyscale of the lower-right pixel will be assigned.
This algorithm is simple and easy to understand and program, so that I will not write the pseudo code for this algorithm. I will give detailed comments in the program.
- Matlab codes:
clear;
close all;
clc;
img = imread('rice.tif');
[ input_img,new_img ] = Nearest_11711118(img,[.8,.8]);
show(img,new_img);
function show(img,new_img)%print the figure
[height,width] = size(img);
figure;imshow(img);
axis on
title(['Input Figure with Size(',num2str(height),'*',num2str(width),'):']);
[new_height,new_width,] =size(new_img);
figure;imshow(new_img);
axis on
title(['Output Figure with Size( ',num2str(new_height),'*',num2str(new_width),'):']);
end
function[ input_img,new_img ]= Nearest_11711118(input_img, dim)
input_img=double(input_img);
swh=size(input_img); %Get the width and height of the original image
inputWidth=swh(:,2); %Get the width of the original image
inputHigh=swh(:,1); %Get the height of the original image
r=dim(:,1);
c=dim(:,2);
outputHigh=ceil(inputHigh * c);
outputWidth=ceil(inputWidth * r);
new_img=zeros(outputHigh,outputWidth);
orignalx = [1:outputHigh];
orignaly = [1:outputWidth];
for i=1:outputHigh
orignalx(i)=round(i/r);
end
for j=1:outputWidth
orignaly(j)=round(j/c);
end
for i=1:outputHigh
for j=1:outputWidth
if(orignalx(i) < 1) %If the coordinates cross the border, make adjustments
orignalx(i) = 1;
end
if(orignalx(i) > inputHigh)
orignalx(i) = swh;
end
if(orignaly(j) < 1)
orignaly(j) = 1;