2D numpy数组的重新采样

4

我有一个大小为(3,2)的二维数组,需要使用最近邻、线性和双三次插值法重新采样,使其大小变为(4,3)

我正在使用Pythonnumpyscipy来完成这项任务。

如何实现对输入数组进行重新采样?


"Resampling" 在编程中具体指什么?你能提供一个对此含义解释的链接吗? - Edgar H
实际上,我需要使用numpy将低分辨率图像转换为高分辨率。 - Joel
我认为这是不可能的,除非你有更高分辨率的原始图像。一旦你拍摄了一张图像,该图像中就没有子像素信息... - Edgar H
1
我有一张全色图像(高分辨率)和一张多光谱图像(低分辨率)。我需要将低分辨率的图像转换为高分辨率图像。 - Joel
2个回答

3

这里有一篇关于使用卷积进行重新采样的好教程,点击这里查看。

对于整数因子上采样:

import numpy
import scipy
from scipy import ndimage, signal

# Scale factor
factor = 2

# Input image
a = numpy.arange(16).reshape((4,4))

# Empty image enlarged by scale factor
b = numpy.zeros((a.shape[0]*factor, a.shape[0]*factor))

# Fill the new array with the original values
b[::factor,::factor] = a

# Define the convolution kernel
kernel_1d = scipy.signal.boxcar(factor)
kernel_2d = numpy.outer(kernel_1d, kernel_1d)

# Apply the kernel by convolution, seperately in each axis
c = scipy.signal.convolve(b, kernel_2d, mode="valid")

请注意,每个轴的因子可能不同,并且您还可以在每个轴上连续应用卷积。双线性和双三次的卷积核也在链接中显示,其中双线性插值使用三角信号(scipy.signal.triang)进行计算,而双三次则为分段函数。
您还应该注意哪一部分的插值图像是有效的;沿着边缘,卷积核没有足够的支持。
就卫星图像而言,双三次插值是三种选项中最好的选择。

1

这个问题有一个更简单的解决方案,可以使用https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html

最近邻插值是order=0,双线性插值是order=1,双三次插值是order=3(默认)。

import numpy as np
import scipy.ndimage

x = np.arange(6).reshape(3,2).astype(float)
z = (4/3, 3/2)
print('Original array:\n{0}\n\n'.format(x))
methods=['nearest-neighbor', 'bilinear', 'biquadratic', 'bicubic']
for o in range(4):
    print('Resampled with {0} interpolation:\n {1}\n\n'.
    format(methods[o],  scipy.ndimage.zoom(x, z, order=o)))

这将导致:
Original array:
[[0. 1.]
 [2. 3.]
 [4. 5.]]


Resampled with nearest-neighbor interpolation:
 [[0. 1. 1.]
 [2. 3. 3.]
 [2. 3. 3.]
 [4. 5. 5.]]


Resampled with bilinear interpolation:
 [[0.         0.5        1.        ]
 [1.33333333 1.83333333 2.33333333]
 [2.66666667 3.16666667 3.66666667]
 [4.         4.5        5.        ]]


Resampled with biquadratic interpolation:
 [[1.04083409e-16 5.00000000e-01 1.00000000e+00]
 [1.11111111e+00 1.61111111e+00 2.11111111e+00]
 [2.88888889e+00 3.38888889e+00 3.88888889e+00]
 [4.00000000e+00 4.50000000e+00 5.00000000e+00]]


Resampled with bicubic interpolation:
 [[5.55111512e-16 5.00000000e-01 1.00000000e+00]
 [1.03703704e+00 1.53703704e+00 2.03703704e+00]
 [2.96296296e+00 3.46296296e+00 3.96296296e+00]
 [4.00000000e+00 4.50000000e+00 5.00000000e+00]]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接