bilinear_interpolation(image,y,x) which takes 2d matrix image and x,y coordinates of the pixel in the image as like they are in the image
y - to the height of the image x - to the width of the image (x,y)
and returns the pixel ( from 0 to 255 ) from the calculation
notice: the x,y in the calculation aren't the same x,y that we take as parameters in the function It can be assumed that the function receives a valid image with a single color channel
examples of the input and output:
bilinear_interpolation([[0, 64], [128, 255]], 0, 0) → 0
bilinear_interpolation([[0, 64], [128, 255]], 1, 1) → 255
bilinear_interpolation([[0, 64], [128, 255]], 0.5, 0.5) → 112
bilinear_interpolation([[0, 64], [128, 255]], 0.5, 1.5) → 160
assert bilinear_interpolation([[0, 64], [128, 255]], 0.5, 1) == 160
assert bilinear_interpolation([[0, 64], [128, 255]], 0.5, 1.5) == 160
assert bilinear_interpolation([[0, 64], [128, 255]], 0, 1) == 64
assert bilinear_interpolation([[0, 64], [128, 255]], 0, 0) == 0
assert bilinear_interpolation([[0, 64], [128, 255]], 1, 1) == 255
assert bilinear_interpolation([[0, 64], [128, 255]], 0.5, 0.5) == 112
assert bilinear_interpolation([[255, 255], [255, 255]], 0.5, 1.5) == 255
assert bilinear_interpolation([[255, 255], [255, 255]], 0, 1) == 255
assert bilinear_interpolation([[255, 255], [255, 255]], 1.5, 1.5) == 255
So what I tried is like this:
def bilinear_interpolation(image, y, x):
xa = math.floor(x)
ya = math.floor(y)
if(xa >= len(image[0]) and ya >= len(image)):
xa = len(image[0]) - 1
ya = len(image) - 1
a = image[ya][xa]
elif (xa >= len(image[0]) ):
xa = len(image[0]) - 1
a = image[ya][xa]
elif (ya >= len(image)):
ya = len(image) - 1
a = image[ya][xa]
else:
a = image[ya][xa]
if(ya + 1 >= len(image)):
b = image[ya][xa]
else:
b = image[ya + 1][xa]
if (xa + 1 >= len(image[0])):
c = image[ya][xa]
else:
c = image[ya][xa + 1]
if(xa + 1 >= len(image[0]) and ya + 1 >= len(image)):
d = image[ya][xa]
elif (xa + 1 >= len(image[0]) ):
d = image[ya + 1][xa]
elif (ya+1 >= len(image)):
d = image[ya][xa + 1]
else:
d = image[ya + 1][xa + 1]
dx = x - math.floor(x)
dy = y - math.floor(y)
interpolation_factor = a *( 1 - dx)*(1 - dy) + b * dy * (1 - dx) + c * dx * (1 - dy) + d * dx * dy
return round(interpolation_factor)
but its still failing for me.. I always get list index out of range in huge matrix like picture with size 460 x 460
any direction?