我正在尝试使用双线性插值手动实现调整图像大小。
到目前为止,我得到的是插值本身的函数,它似乎工作正常,但调整大小函数似乎仅在角上正常工作:
def bilinear_interpolation(image, y, x):
height = len(image)
width = len(image[0])
x1 = min(math.floor(x), width - 1)
y1 = min(math.floor(y), height - 1)
x2 = min(math.ceil(x), width - 1)
y2 = min(math.ceil(y), height - 1)
a = image[y1][x1]
b = image[y2][x1]
c = image[y1][x2]
d = image[y2][x2]
new_pixel = a * (1 - x) * (1 - y)
new_pixel += b * y * (1 - x)
new_pixel += c * x * (1 - y)
new_pixel += d * x * y
return round(new_pixel)
def resize(image, new_height, new_width):
new_image = [[0 for _ in range(new_width)] for _ in range(new_height)]
for y in range(new_height):
for x in range(new_width):
x_ = (x / (new_width - 1)) * len(image[0])
y_ = (y / (new_height - 1)) * len(image)
new_image[y][x] = bilinear_interpolation(image, y_, x_)
return new_image
所以对于矩阵:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
I got
[[1, 4], [9, 12], [13, 16]]
代替
[[1, 4], [7, 10], [13, 16]]
提前致谢。