【Scipy】scipy.ndimage.zoom矩阵放缩

scipy.ndimage.zoom(input, zoom, output=None, order=3, mode='constant', cval=0.0, prefilter=True)

scipy.ndimage.zoom(

input,                         

#array---输入多维矩阵

zoom,                         

#float/sequence---沿轴的缩放系数,如果是浮点型,表示每个轴的缩放是相同的,如果是序列,zoom应包含每个轴的缩放值;

output=None,           

#adrray or dtyoe---放置输出的数组,或返回数组的dtype,默认情况下,将创建与输入相同的dtype数据

order=3,                   

 #int---样条插值的阶数,默认为3,顺序必须在0-5范围内;

mode='constant',     

#{‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’}---mode参数确定输入数组如何扩展到其边界之外。 默认为“constant”;

cval=0.0,                   

#scalar---如果模式为“constant”,则填充输入的过去边缘的值, 默认值为0.0。

perfilter=True)         

 #bool---确定在插值之前是否使用spline_filter对输入数组进行预过滤。 默认值为True,如果order> 1,将创建一个过滤值的临时float64数组。如果将此值设置为False,如果order> 1,输出将略微模糊,除非输入是预 过滤的,即它是调用的结果 原始输入上的spline_filter。

import numpy as np
import scipy.ndimage

x = np.arange(64).reshape(8,8)

print 'Original array:'
print x

print 'Resampled by a factor of 2 with nearest interpolation:'
print scipy.ndimage.zoom(x, 2, order=0)


print 'Resampled by a factor of 2 with bilinear interpolation:'
print scipy.ndimage.zoom(x, 2, order=1)


print 'Resampled by a factor of 2 with cubic interpolation:'
print scipy.ndimage.zoom(x, 2, order=3)

print 'Downsampled by a factor of 0.5 with default interpolation:'
print(scipy.ndimage.zoom(x, 0.5))
结果
Original array:
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47],
       [48, 49, 50, 51, 52, 53, 54, 55],
       [56, 57, 58, 59, 60, 61, 62, 63]])
Resampled by a factor of 2 with nearest interpolation:
[[ 0  0  1  1  2  2  3  3  4  4  5  5  6  6  7  7]
 [ 0  0  1  1  2  2  3  3  4  4  5  5  6  6  7  7]
 [ 8  8  9  9 10 10 11 11 12 12 13 13 14 14 15 15]
 [ 8  8  9  9 10 10 11 11 12 12 13 13 14 14 15 15]
 [16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23]
 [16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23]
 [24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31]
 [24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31]
 [32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39]
 [32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39]
 [40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47]
 [40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47]
 [48 48 49 49 50 50 51 51 52 52 53 53 54 54 55 55]
 [48 48 49 49 50 50 51 51 52 52 53 53 54 54 55 55]
 [56 56 57 57 58 58 59 59 60 60 61 61 62 62 63 63]
 [56 56 57 57 58 58 59 59 60 60 61 61 62 62 63 63]]

Resampled by a factor of 2 with bilinear interpolation:
[[ 0  0  1  1  2  2  3  3  4  4  5  5  6  6  7  7]
 [ 4  4  5  5  6  6  7  7  7  8  8  9  9 10 10 11]
 [ 7  8  8  9  9 10 10 11 11 12 12 13 13 14 14 14]
 [11 12 12 13 13 14 14 14 15 15 16 16 17 17 18 18]
 [15 15 16 16 17 17 18 18 19 19 20 20 21 21 21 22]
 [19 19 20 20 21 21 21 22 22 23 23 24 24 25 25 26]
 [22 23 23 24 24 25 25 26 26 27 27 28 28 28 29 29]
 [26 27 27 28 28 28 29 29 30 30 31 31 32 32 33 33]
 [30 30 31 31 32 32 33 33 34 34 35 35 35 36 36 37]
 [34 34 35 35 35 36 36 37 37 38 38 39 39 40 40 41]
 [37 38 38 39 39 40 40 41 41 42 42 42 43 43 44 44]
 [41 42 42 42 43 43 44 44 45 45 46 46 47 47 48 48]
 [45 45 46 46 47 47 48 48 49 49 49 50 50 51 51 52]
 [49 49 49 50 50 51 51 52 52 53 53 54 54 55 55 56]
 [52 53 53 54 54 55 55 56 56 56 57 57 58 58 59 59]
 [56 56 57 57 58 58 59 59 60 60 61 61 62 62 63 63]]

Resampled by a factor of 2 with cubic interpolation:
[[ 0  0  1  1  2  2  3  3  4  4  5  5  6  6  7  7]
 [ 2  3  3  4  4  5  5  6  6  7  7  8  8  9  9  9]
 [ 7  8  8  9  9 10 10 11 11 12 12 12 13 13 14 14]
 [12 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19]
 [15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22]
 [19 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26]
 [22 23 23 24 24 25 25 26 26 27 27 27 28 28 29 29]
 [26 26 27 28 28 28 29 29 30 30 31 31 32 32 33 33]
 [30 30 31 31 32 32 33 33 34 34 35 35 35 36 37 37]
 [34 34 35 35 36 36 36 37 37 38 38 39 39 40 40 41]
 [37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 44]
 [41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48]
 [44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 51]
 [49 49 50 50 51 51 51 52 52 53 53 54 54 55 55 56]
 [54 54 54 55 55 56 56 57 57 58 58 59 59 60 60 61]
 [56 56 57 57 58 58 59 59 60 60 61 61 62 62 63 63]]


Downsampled by a factor of 0.5 with default interpolation:
[[ 0  2  5  7]
 [19 21 23 26]
 [37 40 42 44]
 [56 58 61 63]]

对源码好奇的小伙伴请点击这里https://github.com/scipy/scipy/blob/8648bfe26e1a631321415fbcb3dfca72f24a8648/scipy/ndimage/src/ni_interpolation.c#L562

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值