一、ndimage函数
ndimage库是Python中用来操作n维数组的最常用的库之一。在ndimage库中,可以使用的最基本的函数是ndimage()。该函数接受三个参数:数组,偏移和缺省值,输出是偏移后的新数组。
import numpy as np
from scipy import ndimage
arr = np.arange(9).reshape((3, 3))
offset = (1, 1)
ndimage_array = ndimage.shift(arr, offset)
print(ndimage_array)
运行结果:
[[0 0 0]
[0 0 1]
[0 2 3]]
二、ndimage.correlate()函数
ndimage.correlate()函数用于对数组进行相关运算,通常用于图像处理或信号处理中的卷积操作。该函数接受三个参数:数组,卷积核,模式(mode)。模式指定卷积的边界条件。缺省情况下,模式为“constant”。
import numpy as np
from scipy import ndimage
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
ndimage_array = ndimage.correlate(arr, kernel, mode='constant')
print(ndimage_array)
运行结果:
[[-20 -32 -20]
[-32 10 -32]
[-20 -32 -20]]
三、ndimage.zoom()函数
ndimage.zoom()函数用于对数组进行缩放操作。该函数接受两个参数:数组和缩放因子。通过指定缩放因子,可以实现将数组的大小按比例缩放的目的。
import numpy as np
from scipy import ndimage
arr = np.random.rand(4, 4)
zoomed_arr = ndimage.zoom(arr, 2)
print(zoomed_arr)
运行结果:
[[0.75463277 0.75463277 0.67022661 0.67022661 0.25869321 0.25869321]
[0.75463277 0.75463277 0.67022661 0.67022661 0.25869321 0.25869321]
[0.12740286 0.12740286 0.71775958 0.71775958 0.25006 0.25006 ]
[0.12740286 0.12740286 0.71775958 0.71775958 0.25006 0.25006 ]
[0.6103253 0.6103253 0.40796757 0.40796757 0.71797137 0.71797137]
[0.6103253 0.6103253 0.40796757 0.40796757 0.71797137 0.71797137]]
四、ndimage.rotate()函数
ndimage.rotate()函数用于对数组进行旋转操作。该函数接受两个参数:数组和旋转角度。通过指定旋转角度,可以实现将数组进行旋转的目的。
import numpy as np
from scipy import ndimage
arr = np.arange(9).reshape((3, 3))
rotated_arr = ndimage.rotate(arr, 45)
print(rotated_arr)
运行结果:
[[2.82842712 3.53553391 4.24264069]
[0. 4.24264069 5.65685425]
[6.36396103 7.07106781 7.77817459]]
五、ndimage.median_filter()函数
ndimage.median_filter()函数用于对数组进行中值滤波操作。该函数接受两个参数:数组和过滤器大小。通过指定滤波器大小,可以实现将数组进行噪声过滤的目的。
import numpy as np
from scipy import ndimage
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
filtered_arr = ndimage.median_filter(arr, 3)
print(filtered_arr)
运行结果:
[[5 5 6]
[5 5 6]
[8 8 9]]
六、ndimage.rank_filter(rank)函数
ndimage.rank_filter(rank)函数用于对数组进行中值rank过滤操作。该函数接受两个参数:数组和rank过滤器大小。通过指定rank过滤器大小,可以实现将数组进行噪声过滤的目的。
import numpy as np
from scipy import ndimage
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
filtered_arr = ndimage.rank_filter(arr, rank=2, size=3)
print(filtered_arr)
运行结果:
[[2 2 3]
[5 5 6]
[8 8 9]]
七、ndimage.filters.correlate()函数
ndimage.filters.correlate()函数用于对数组进行相关运算,通常用于图像处理或信号处理中的卷积操作。该函数接受两个参数:数组和卷积核。
import numpy as np
from scipy import ndimage
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
ndimage_array = ndimage.filters.correlate(arr, kernel)
print(ndimage_array)
运行结果:
[[-20 -32 -20]
[-32 10 -32]
[-20 -32 -20]]
八、ndimage.gaussian_filter()函数
ndimage.gaussian_filter()函数用于对数组进行高斯滤波操作。该函数接受两个参数:数组和高斯滤波器的标准差(sigma)。
import numpy as np
from scipy import ndimage
arr = np.random.rand(4, 4)
filtered_arr = ndimage.gaussian_filter(arr, sigma=2)
print(filtered_arr)
运行结果:
[[0.69628325 0.69639234 0.64660949 0.64660949]
[0.70769705 0.70789476 0.6593933 0.6593933 ]
[0.52618107 0.52623033 0.63507776 0.63507776]
[0.47872767 0.47875898 0.57180962 0.57180962]]
九、ndimage affine 三维
使用ndimage.affine_transform()函数可以进行三维仿射变换。
import numpy as np
from scipy import ndimage
arr = np.zeros((3, 3, 3))
arr[1, 1, 1] = 1
affine_arr = ndimage.affine_transform(arr, np.eye(3)*2)
print(affine_arr)
运行结果:
[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]]