一、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.]]]
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/279987.html
微信掃一掃
支付寶掃一掃