0

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 calculation

enter image description here

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?

  • `but its still failing for me.. any direction?` Can you elaborate what you mean by "its failing"? – Akshay Sehgal Nov 18 '21 at 16:26
  • Yes, I always get list index out of range at the line interpolation_factor += image[r][c]*(1-image[r-1][c])+image[r][c]*(image[r][c+1]) is there anything am i missing to add? – Schevenz Rs Nov 18 '21 at 16:27
  • can you add this to the question itself? the error, and the error trace pasted in a code block?\ – Akshay Sehgal Nov 18 '21 at 16:29
  • edited now , should be clear – Schevenz Rs Nov 18 '21 at 16:33
  • @AkshaySehgal Got an idea where I could possibly change? – Schevenz Rs Nov 18 '21 at 16:53
  • You may take a look at my MATLAB [implementation](https://stackoverflow.com/a/40248435/4926757). I can't see the computation of `dx`, `dy` in your code. – Rotem Nov 18 '21 at 17:05
  • I noticed now I had some wrong understanding and did some wrong calculation, I added now the dx and dy, but I still having problem with finding the interpolation factor, your matlab implementation is a little different than of what im trying to find here @Rotem – Schevenz Rs Nov 18 '21 at 17:16
  • I just posted a new answer in the same subject in the following [post](https://stackoverflow.com/questions/70024313/resize-using-bilinear-interpolation-in-python) – Rotem Nov 18 '21 at 21:29
  • i fixed my code @Rotem and now it passes all the examples input and output , but i still facing problem in bigger image, like 460 x 460 matrix – Schevenz Rs Nov 18 '21 at 23:26

0 Answers0