日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

scipy.ndimage.morphology

發(fā)布時間:2024/1/1 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 scipy.ndimage.morphology 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

scipy.ndimage.morphology.generate_binary_structure

scipy.ndimage.morphology.generate_binary_structure(rank, connectivity)

為二元形態(tài)操作生成二進制結(jié)構(gòu)。

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只能創(chuàng)建尺寸等于3的結(jié)構(gòu)元素,即最小尺寸。 對于較大的結(jié)構(gòu)化元素,這是有用的,例如, 為了侵蝕大對象,可以使用iterate_structure,也可以直接創(chuàng)建具有numpy功能的自定義數(shù)組,如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)

用給定的結(jié)構(gòu)元素進行多維二元膨脹。

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:
擴張是一種數(shù)學形態(tài)學操作,它使用結(jié)構(gòu)化元素來擴展圖像中的形狀。 當結(jié)構(gòu)元素的中心位于圖像的非零點內(nèi)時,結(jié)構(gòu)元素對圖像的二元膨脹是結(jié)構(gòu)元素所覆蓋的點的軌跡。
原理:一般對二值圖像進行操作。找到像素值為1的點,將它的鄰近像素點都設(shè)置成這個值。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)

函數(shù):skimage.morphology.erosion(image, selem=None)
selem表示結(jié)構(gòu)元素,用于設(shè)定局部區(qū)域的形狀和大小。
和膨脹相反的操作,將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數(shù)字圖像處理(13):基本形態(tài)學濾波

measure.label() 連通區(qū)域標記

在二值圖像中,如果兩個像素點相鄰且值相同(同為0或同為1),那么就認為這兩個像素點在一個相互連通的區(qū)域內(nèi)。而同一個連通區(qū)域的所有像素點,都用同一個數(shù)值來進行標記,這個過程就叫連通區(qū)域標記。在判斷兩個像素是否相鄰時,我們通常采用4連通或8連通判斷。在圖像中,最小的單位是像素,每個像素周圍有8個鄰接像素,常見的鄰接關(guān)系有2種:4鄰接與8鄰接。4鄰接一共4個點,即上下左右,如下左圖所示。8鄰接的點一共有8個,包括了對角線位置的點,如下右圖所示。

在skimage包中,我們采用measure子模塊下的label()函數(shù)來實現(xiàn)連通區(qū)域標記。
函數(shù)格式:skimage.measure.label(image,connectivity=None)
參數(shù)中的image表示需要處理的二值圖像,connectivity表示連接的模式,1代表4鄰接,2代表8鄰接。
輸出一個標記數(shù)組(labels), 從0開始標記。

import numpy as np import scipy.ndimage as ndi from skimage import measure,color import matplotlib.pyplot as plt#編寫一個函數(shù)來生成原始二值圖像 def microstructure(l=256):n = 5x, y = np.ogrid[0:l, 0:l] #生成網(wǎng)絡(luò)mask = np.zeros((l, l))generator = np.random.RandomState(1) #隨機數(shù)種子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連通區(qū)域標記 dst=color.label2rgb(labels) #根據(jù)不同的標記顯示不同的顏色 print('regions number:',labels.max()+1) #顯示連通區(qū)域塊數(shù)(從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數(shù)組快速地轉(zhuǎn)換為int數(shù)組。
結(jié)果如圖:有10個連通的區(qū)域,標記為0-9

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

返回所有連通區(qū)塊的屬性列表

ref:python數(shù)字圖像處理(18):高級形態(tài)學處理

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。