Why does this bilinear interpolation algorithm have two Y(i,j) outputs (i.e. Y(i,j)=I(c​eil(y),cei​l(x)) and Y(i,j)=(y-y2)*P+(y1-y)*Q )? How difference are they ?

4 views (last 30 days)
What is the meaning of each bolded-lines in the traditional bilinear interpolation algorithm source code shown below ? Why these TWO 'outputs' and HOW different are they ? Can you please explain with an illustration, if possible ?
----------------------------------- clc close all clear all I=imread('LENNA64.bmp'); [originheight,originwidth]=size(I); height=4*originheight; width=4*originwidth;
Y=zeros(height,width);
[height,width]=size(Y); hscale=originheight/height; wscale=originwidth/width;
for i=1:height y=hscale*i; for j=1:width x=wscale*j;
if(floor(x)==0)||(floor(y)==0)||(ceil(x)==originwidth)||(ceil(y)==originheight)
*Y(i,j)=I(ceil(y),ceil(x));*
else
x1=floor(x);
x2=x1+1;
y2=floor(y);
y1=y2+1;
N1=I(y2,x1);
N2=I(y2,x2);
N3=I(y1,x2);
N4=I(y1,x1);
Q=(x2-x)*(N1)+(x-x1)*(N2);
P=(x2-x)*(N4)+(x-x1)*(N3);
*Y(i,j)=(y-y2)*P+(y1-y)*Q;*
end
end
end
Y=uint8(Y); figure;imshow(Y);

Accepted Answer

Geoff Hayes
Geoff Hayes on 10 Oct 2014
OL - look closely at the if condition
(floor(x)==0)||(floor(y)==0)||(ceil(x)==originwidth)||(ceil(y)==originheight)
As x and y are converted to integers via either floor or ceil, then the former may set x or y to zero, and the latter may set the x and y to an the number of rows (height) or columns (width) in the image.
If either x or y are zero, then those are invalid indices into the image matrix, so the lines (in the else block) would fail. If either are identical to the number or rows or columns in the matrix, then adding one to either of these indices (as is done in the else block) would create an index that exceeds the dimension of the matrix and so would fail.
Basically, the if block is a guard against the code trying to make use of neighbouring pixels that fall outside of the image. In the interpolated image, the pixels along its edges are identical to those from the original image (because of the if block), and it is only those pixels within that are determined using the bilinear interpolation algorithm (see the else block).

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!