日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

scipy.ndimage.morphology

發布時間:2024/1/1 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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值擴充到鄰近像素。擴大黑色部分,減小白色部分。可用來提取骨干信息,去掉毛刺,去掉孤立的像素。

from skimage import data import skimage.morphology as sm import matplotlib.pyplot as plt img=data.checkerboard() dst1=sm.erosion(img,sm.square(5)) #用邊長為5的正方形濾波器進行膨脹濾波 dst2=sm.erosion(img,sm.square(25)) #用邊長為25的正方形濾波器進行膨脹濾波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)

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開始標記。

import numpy as np import scipy.ndimage as ndi from skimage import measure,color import matplotlib.pyplot as plt#編寫一個函數來生成原始二值圖像 def microstructure(l=256):n = 5x, y = np.ogrid[0:l, 0:l] #生成網絡mask = np.zeros((l, l))generator = np.random.RandomState(1) #隨機數種子points = l * generator.rand(2, n**2)mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1mask = ndi.gaussian_filter(mask, sigma=l/(4.*n)) #高斯濾波return mask > mask.mean()data = microstructure(l=128)*1 #生成測試圖片labels=measure.label(data,connectivity=2) #8連通區域標記 dst=color.label2rgb(labels) #根據不同的標記顯示不同的顏色 print('regions number:',labels.max()+1) #顯示連通區域塊數(從0開始標記)fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4)) ax1.imshow(data, plt.cm.gray, interpolation='nearest') ax1.axis('off') ax2.imshow(dst,interpolation='nearest') ax2.axis('off')fig.tight_layout() plt.show()

在代碼中,有些地方乘以1,則可以將bool數組快速地轉換為int數組。
結果如圖:有10個連通的區域,標記為0-9

如果想分別對每一個連通區域進行操作,比如計算面積、外接矩形、凸包面積等,則需要調用measure子模塊的regionprops()函數。該函數格式為:skimage.measure.regionprops(label_image)

返回所有連通區塊的屬性列表

ref:python數字圖像處理(18):高級形態學處理

總結

以上是生活随笔為你收集整理的scipy.ndimage.morphology的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。