Python 如何使用numpy实现三维双线性插值?
我已经了解了这段双线性插值代码(添加在这里),但我想将这段代码改进为3D,这意味着将其更新为使用RGB图像(3D,而不仅仅是2D) 如果你有什么建议,我可以这样做,我想知道 这是一维线性插值:Python 如何使用numpy实现三维双线性插值?,python,numpy,image-processing,linear-interpolation,bilinear-interpolation,Python,Numpy,Image Processing,Linear Interpolation,Bilinear Interpolation,我已经了解了这段双线性插值代码(添加在这里),但我想将这段代码改进为3D,这意味着将其更新为使用RGB图像(3D,而不仅仅是2D) 如果你有什么建议,我可以这样做,我想知道 这是一维线性插值: import math def linear1D_resize(in_array, size): """ `in_array` is the input array. `size` is the desired size. "&qu
import math
def linear1D_resize(in_array, size):
"""
`in_array` is the input array.
`size` is the desired size.
"""
ratio = (len(in_array) - 1) / (size - 1)
out_array = []
for i in range(size):
low = math.floor(ratio * i)
high = math.ceil(ratio * i)
weight = ratio * i - low
a = in_array[low]
b = in_array[high]
out_array.append(a * (1 - weight) + b * weight)
return out_array
对于2D:
import math
def bilinear_resize(image, height, width):
"""
`image` is a 2-D numpy array
`height` and `width` are the desired spatial dimension of the new 2-D array.
"""
img_height, img_width = image.shape[:2]
resized = np.empty([height, width])
x_ratio = float(img_width - 1) / (width - 1) if width > 1 else 0
y_ratio = float(img_height - 1) / (height - 1) if height > 1 else 0
for i in range(height):
for j in range(width):
x_l, y_l = math.floor(x_ratio * j), math.floor(y_ratio * i)
x_h, y_h = math.ceil(x_ratio * j), math.ceil(y_ratio * i)
x_weight = (x_ratio * j) - x_l
y_weight = (y_ratio * i) - y_l
a = image[y_l, x_l]
b = image[y_l, x_h]
c = image[y_h, x_l]
d = image[y_h, x_h]
pixel = a * (1 - x_weight) * (1 - y_weight) + b * x_weight * (1 - y_weight) + c * y_weight * (1 - x_weight) + d * x_weight * y_weight
resized[i][j] = pixel # pixel is the scalar with the value comptued by the interpolation
return resized
查看一些scipy ndimage插值函数。他们会做你想做的事情,并且“使用numpy” 它们也非常实用、快速,并且经过多次测试 理查德