簡體   English   中英

2D numpy 數組的重采樣

[英]resampling of 2D numpy array

我有一個大小為(3,2)的二維數組,我必須使用最近鄰、線性和雙三次插值方法重新采樣,以使大小變為(4,3)

我為此使用Pythonnumpyscipy

如何實現輸入數組的重采樣?

這里有一個很好的關於使用卷積重新采樣的教程。

對於整數因子放大:

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 )和雙三次是分段函數。

您還應該注意插值圖像的哪一部分是有效的; 沿邊緣沒有對內核的足夠支持。

就衛星圖像而言,雙三次插值是三者中的最佳選擇。

這個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]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM