scipy.ndimage.morphology
scipy.ndimage.morphology.generate_binary_structure
scipy.ndimage.morphology.generate_binary_structure(rank, connectivity)
為二元形態操作生成二進制結構。
Parameters:
rank : int
Number of dimensions of the array to which the structuring element will be applied, as returned by np.ndim.
connectivity : int
connectivity determines which elements of the output array belong to the structure, i.e. are considered as neighbors of the central element. Elements up to a squared distance of connectivity from the center are considered neighbors. connectivity may range from 1 (no diagonal elements are neighbors) to rank (all elements are neighbors).
output : ndarray of bools
Structuring element which may be used for binary morphological operations, with rank dimensions and all dimensions equal to 3.
Notes:
generate_binary_structure只能創建尺寸等于3的結構元素,即最小尺寸。 對于較大的結構化元素,這是有用的,例如, 為了侵蝕大對象,可以使用iterate_structure,也可以直接創建具有numpy功能的自定義數組,如numpy.ones。
Examples:
>>> struct = ndimage.generate_binary_structure(2, 1) >>> struct array([[False, True, False],[ True, True, True],[False, True, False]], dtype=bool) >>> a = np.zeros((5,5)) >>> a[2, 2] = 1 >>> a array([[ 0., 0., 0., 0., 0.],[ 0., 0., 0., 0., 0.],[ 0., 0., 1., 0., 0.],[ 0., 0., 0., 0., 0.],[ 0., 0., 0., 0., 0.]]) >>> b = ndimage.binary_dilation(a, structure=struct).astype(a.dtype) >>> b array([[ 0., 0., 0., 0., 0.],[ 0., 0., 1., 0., 0.],[ 0., 1., 1., 1., 0.],[ 0., 0., 1., 0., 0.],[ 0., 0., 0., 0., 0.]]) >>> ndimage.binary_dilation(b, structure=struct).astype(a.dtype) array([[ 0., 0., 1., 0., 0.],[ 0., 1., 1., 1., 0.],[ 1., 1., 1., 1., 1.],[ 0., 1., 1., 1., 0.],[ 0., 0., 1., 0., 0.]]) >>> struct = ndimage.generate_binary_structure(2, 2) >>> struct array([[ True, True, True],[ True, True, True],[ True, True, True]], dtype=bool) >>> struct = ndimage.generate_binary_structure(3, 1) >>> struct # no diagonal elements array([[[False, False, False],[False, True, False],[False, False, False]],[[False, True, False],[ True, True, True],[False, True, False]],[[False, False, False],[False, True, False],[False, False, False]]], dtype=bool)scipy.ndimage.morphology.binary_dilation
scipy.ndimage.morphology.binary_dilation(input, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False)
用給定的結構元素進行多維二元膨脹。
Parameters:
input : array_like
Binary array_like to be dilated. Non-zero (True) elements form the subset to be dilated.
structure : array_like, optional
Structuring element used for the dilation. Non-zero elements are considered True. If no structuring element is provided an element is generated with a square connectivity equal to one.
iterations : {int, float}, optional
The dilation is repeated iterations times (one, by default). If iterations is less than 1, the dilation is repeated until the result does not change anymore.
mask : array_like, optional
If a mask is given, only those elements with a True value at the corresponding mask element are modified at each iteration.
output : ndarray, optional
Array of the same shape as input, into which the output is placed. By default, a new array is created.
origin : int or tuple of ints, optional
Placement of the filter, by default 0.
border_value : int (cast to 0 or 1)
Value at the border in the output array.
Returns:
binary_dilation : ndarray of bools
Dilation of the input by the structuring element.
Notes:
擴張是一種數學形態學操作,它使用結構化元素來擴展圖像中的形狀。 當結構元素的中心位于圖像的非零點內時,結構元素對圖像的二元膨脹是結構元素所覆蓋的點的軌跡。
原理:一般對二值圖像進行操作。找到像素值為1的點,將它的鄰近像素點都設置成這個值。1值表示白,0值表示黑,因此膨脹操作可以擴大白色值范圍,壓縮黑色值范圍。一般用來擴充邊緣或填充小的孔洞。
Examples:
>>> a = np.zeros((5, 5)) >>> a[2, 2] = 1 >>> a array([[ 0., 0., 0., 0., 0.],[ 0., 0., 0., 0., 0.],[ 0., 0., 1., 0., 0.],[ 0., 0., 0., 0., 0.],[ 0., 0., 0., 0., 0.]]) >>> ndimage.binary_dilation(a) array([[False, False, False, False, False],[False, False, True, False, False],[False, True, True, True, False],[False, False, True, False, False],[False, False, False, False, False]], dtype=bool) >>> ndimage.binary_dilation(a).astype(a.dtype) array([[ 0., 0., 0., 0., 0.],[ 0., 0., 1., 0., 0.],[ 0., 1., 1., 1., 0.],[ 0., 0., 1., 0., 0.],[ 0., 0., 0., 0., 0.]]) >>> # 3x3 structuring element with connectivity 1, used by default >>> struct1 = ndimage.generate_binary_structure(2, 1) >>> struct1 array([[False, True, False],[ True, True, True],[False, True, False]], dtype=bool) >>> # 3x3 structuring element with connectivity 2 >>> struct2 = ndimage.generate_binary_structure(2, 2) >>> struct2 array([[ True, True, True],[ True, True, True],[ True, True, True]], dtype=bool) >>> ndimage.binary_dilation(a, structure=struct1).astype(a.dtype) array([[ 0., 0., 0., 0., 0.],[ 0., 0., 1., 0., 0.],[ 0., 1., 1., 1., 0.],[ 0., 0., 1., 0., 0.],[ 0., 0., 0., 0., 0.]]) >>> ndimage.binary_dilation(a, structure=struct2).astype(a.dtype) array([[ 0., 0., 0., 0., 0.],[ 0., 1., 1., 1., 0.],[ 0., 1., 1., 1., 0.],[ 0., 1., 1., 1., 0.],[ 0., 0., 0., 0., 0.]]) >>> ndimage.binary_dilation(a, structure=struct1,\ ... iterations=2).astype(a.dtype) array([[ 0., 0., 1., 0., 0.],[ 0., 1., 1., 1., 0.],[ 1., 1., 1., 1., 1.],[ 0., 1., 1., 1., 0.],[ 0., 0., 1., 0., 0.]]) from skimage import data import skimage.morphology as sm import matplotlib.pyplot as plt img=data.checkerboard() dst1=sm.dilation(img,sm.square(5)) #用邊長為5的正方形濾波器進行膨脹濾波 dst2=sm.dilation(img,sm.square(15)) #用邊長為15的正方形濾波器進行膨脹濾波plt.figure('morphology',figsize=(8,8)) plt.subplot(131) plt.title('origin image') plt.imshow(img,plt.cm.gray)plt.subplot(132) plt.title('morphological image') plt.imshow(dst1,plt.cm.gray)plt.subplot(133) plt.title('morphological image') plt.imshow(dst2,plt.cm.gray)腐蝕(erosion)
函數:skimage.morphology.erosion(image, selem=None)
selem表示結構元素,用于設定局部區域的形狀和大小。
和膨脹相反的操作,將0值擴充到鄰近像素。擴大黑色部分,減小白色部分。可用來提取骨干信息,去掉毛刺,去掉孤立的像素。
ref: python數字圖像處理(13):基本形態學濾波
measure.label() 連通區域標記
在二值圖像中,如果兩個像素點相鄰且值相同(同為0或同為1),那么就認為這兩個像素點在一個相互連通的區域內。而同一個連通區域的所有像素點,都用同一個數值來進行標記,這個過程就叫連通區域標記。在判斷兩個像素是否相鄰時,我們通常采用4連通或8連通判斷。在圖像中,最小的單位是像素,每個像素周圍有8個鄰接像素,常見的鄰接關系有2種:4鄰接與8鄰接。4鄰接一共4個點,即上下左右,如下左圖所示。8鄰接的點一共有8個,包括了對角線位置的點,如下右圖所示。
在skimage包中,我們采用measure子模塊下的label()函數來實現連通區域標記。
函數格式:skimage.measure.label(image,connectivity=None)
參數中的image表示需要處理的二值圖像,connectivity表示連接的模式,1代表4鄰接,2代表8鄰接。
輸出一個標記數組(labels), 從0開始標記。
在代碼中,有些地方乘以1,則可以將bool數組快速地轉換為int數組。
結果如圖:有10個連通的區域,標記為0-9
如果想分別對每一個連通區域進行操作,比如計算面積、外接矩形、凸包面積等,則需要調用measure子模塊的regionprops()函數。該函數格式為:skimage.measure.regionprops(label_image)
返回所有連通區塊的屬性列表
ref:python數字圖像處理(18):高級形態學處理
總結
以上是生活随笔為你收集整理的scipy.ndimage.morphology的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java开发高端说法_扣丁学堂教你如何成
- 下一篇: Android开发基础 -- andro