在Python中下采样数组

wuf*_*uff 29 python numpy image-processing gdal scipy

我有基本的2-D numpy数组,我想将它们"缩减"到更粗糙的分辨率.是否有一个简单的numpy或scipy模块可以轻松地做到这一点?我还应该注意,这个数组是通过Basemap模块在地理上显示的.

样品: 在此输入图像描述

K.-*_*Aye 13

scikit-image已经实现了downsampling这里的工作版本,虽然downsampling如果我理解正确的话,他们不愿意调用它,因为它不是DSP的下采样.

http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.block_reduce

但它运行得很好,它是downsampler我在Python中找到的唯一可以处理np.nan图像的东西.我很快就对这个巨大的图像进行了下采样.


Mik*_*e T 10

下采样时,插值是错误的.始终使用聚合方法.

我使用块方法来执行此操作,使用"因子"来降低分辨率.

import numpy as np
from scipy import ndimage

def block_mean(ar, fact):
    assert isinstance(fact, int), type(fact)
    sx, sy = ar.shape
    X, Y = np.ogrid[0:sx, 0:sy]
    regions = sy/fact * (X/fact) + Y/fact
    res = ndimage.mean(ar, labels=regions, index=np.arange(regions.max() + 1))
    res.shape = (sx/fact, sy/fact)
    return res
Run Code Online (Sandbox Code Playgroud)

例如,使用因子5(5x5块)的(100,200)形状阵列导致(20,40)阵列结果:

ar = np.random.rand(20000).reshape((100, 200))
block_mean(ar, 5).shape  # (20, 40)
Run Code Online (Sandbox Code Playgroud)


Ham*_*mer 6

imresizendimage.interpolation.zoom看起来他们做你想做的

我之前没有尝试过 imresize 但这里是我如何使用 ndimage.interpolation.zoom

a = np.array(64).reshape(8,8)
a = ndimage.interpolation.zoom(a,.5) #decimate resolution
Run Code Online (Sandbox Code Playgroud)

a 是一个 4x4 矩阵,其中包含内插值


Kol*_*ril 5

最简单的方法:您可以使用array[0::2]符号,它只考虑每隔一个索引。例如

array= np.array([[i+j for i in range(0,10)] for j in range(0,10)])
down_sampled=array[0::2,0::2]

print("array \n", array)
print("array2 \n",down_sampled)
Run Code Online (Sandbox Code Playgroud)

有输出:

array 
[[ 0  1  2  3  4  5  6  7  8  9]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 2  3  4  5  6  7  8  9 10 11]
 [ 3  4  5  6  7  8  9 10 11 12]
 [ 4  5  6  7  8  9 10 11 12 13]
 [ 5  6  7  8  9 10 11 12 13 14]
 [ 6  7  8  9 10 11 12 13 14 15]
 [ 7  8  9 10 11 12 13 14 15 16]
 [ 8  9 10 11 12 13 14 15 16 17]
 [ 9 10 11 12 13 14 15 16 17 18]]
array2 
[[ 0  2  4  6  8]
 [ 2  4  6  8 10]
 [ 4  6  8 10 12]
 [ 6  8 10 12 14]
 [ 8 10 12 14 16]]
Run Code Online (Sandbox Code Playgroud)