python數位影像處理之骨架提取與分水嶺演算法

來源:互聯網
上載者:User
這篇文章主要介紹了python數位影像處理之骨架提取與分水嶺演算法,現在分享給大家,也給大家做個參考。一起過來看看吧

骨架提取與分水嶺演算法也屬於形態學處理範疇,都放在morphology子模組內。

1、骨架提取

骨架提取,也叫二值映像細化。這種演算法能將一個連通地區細化成一個像素的寬度,用於特徵提取和目標拓撲表示。

morphology子模組提供了兩個函數用於骨架提取,分別是Skeletonize()函數和medial_axis()函數。我們先來看Skeletonize()函數。

格式為:skimage.morphology.skeletonize(image)

輸入和輸出都是一幅二值映像。

例1:

from skimage import morphology,drawimport numpy as npimport matplotlib.pyplot as plt#建立一個二值映像用於測試image = np.zeros((400, 400))#產生目標對象1(白色U型)image[10:-10, 10:100] = 1image[-100:-10, 10:-10] = 1image[10:-10, -100:-10] = 1#產生目標對象2(X型)rs, cs = draw.line(250, 150, 10, 280)for i in range(10): image[rs + i, cs] = 1rs, cs = draw.line(10, 150, 250, 280)for i in range(20): image[rs + i, cs] = 1#產生目標對象3(O型)ir, ic = np.indices(image.shape)circle1 = (ic - 135)**2 + (ir - 150)**2 < 30**2circle2 = (ic - 135)**2 + (ir - 150)**2 < 20**2image[circle1] = 1image[circle2] = 0#實施骨架演算法skeleton =morphology.skeletonize(image)#顯示結果fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))ax1.imshow(image, cmap=plt.cm.gray)ax1.axis('off')ax1.set_title('original', fontsize=20)ax2.imshow(skeleton, cmap=plt.cm.gray)ax2.axis('off')ax2.set_title('skeleton', fontsize=20)fig.tight_layout()plt.show()

產生一幅測試映像,上面有三個目標對象,分別進行骨架提取,結果如下:

例2:利用系統內建的馬圖片進行骨架提取

from skimage import morphology,data,colorimport matplotlib.pyplot as pltimage=color.rgb2gray(data.horse())image=1-image #反相#實施骨架演算法skeleton =morphology.skeletonize(image)#顯示結果fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))ax1.imshow(image, cmap=plt.cm.gray)ax1.axis('off')ax1.set_title('original', fontsize=20)ax2.imshow(skeleton, cmap=plt.cm.gray)ax2.axis('off')ax2.set_title('skeleton', fontsize=20)fig.tight_layout()plt.show()

medial_axis就是中軸的意思,利用中軸變換方法計算前景(1值)目標對象的寬度,格式為:

skimage.morphology.medial_axis(image,mask=None,return_distance=False)

mask: 掩模。預設為None, 如果給定一個掩模,則在掩模內的像素值才執行骨架演算法。

return_distance: bool型值,預設為False. 如果為True, 則除了返回骨架,還將距離變換值也同時返回。這裡的距離指的是中軸線上的所有點與背景點的距離。

import numpy as npimport scipy.ndimage as ndifrom skimage import morphologyimport matplotlib.pyplot as plt#編寫一個函數,產生測試映像def microstructure(l=256): n = 5 x, 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)] = 1 mask = ndi.gaussian_filter(mask, sigma=l/(4.*n)) return mask > mask.mean()data = microstructure(l=64) #產生測試映像#計算中軸和距離變換值skel, distance =morphology.medial_axis(data, return_distance=True)#中軸上的點到背景像素點的距離dist_on_skel = distance * skelfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))ax1.imshow(data, cmap=plt.cm.gray, interpolation='nearest')#用光譜色顯示中軸ax2.imshow(dist_on_skel, cmap=plt.cm.spectral, interpolation='nearest')ax2.contour(data, [0.5], colors='w') #顯示輪廓線fig.tight_layout()plt.show()

2、分水嶺演算法

分水嶺在地理學上就是指一個山脊,水通常會沿著山脊的兩邊流向不同的“匯水盆”。分水嶺演算法是一種用於映像分割的經典演算法,是基於拓撲理論的數學形態學的分割方法。如果映像中的目標物體是連在一起的,則分割起來會更困難,分水嶺演算法經常用於處理這類問題,通常會取得比較好的效果。

分水嶺演算法可以和距離變換結合,尋找“匯水盆地”和“分水嶺界限”,從而對映像進行分割。二值映像的距離變換就是每一個像素點到最近非零值像素點的距離,我們可以使用scipy包來計算距離變換。

在下面的例子中,需要將兩個重疊的圓分開。我們先計算圓上的這些白色像素點到黑色背景像素點的距離變換,選出距離變換中的最大值作為初始標記點(如果是反色的話,則是取最小值),從這些標記點開始的兩個匯水盆越集越大,最後相交於分山嶺。從分山嶺處斷開,我們就得到了兩個分離的圓。

例1:基於距離變換的分山嶺映像分割

import numpy as npimport matplotlib.pyplot as pltfrom scipy import ndimage as ndifrom skimage import morphology,feature#建立兩個帶有重疊圓的映像x, y = np.indices((80, 80))x1, y1, x2, y2 = 28, 28, 44, 52r1, r2 = 16, 20mask_circle1 = (x - x1)**2 + (y - y1)**2 < r1**2mask_circle2 = (x - x2)**2 + (y - y2)**2 < r2**2image = np.logical_or(mask_circle1, mask_circle2)#現在我們用分水嶺演算法分離兩個圓distance = ndi.distance_transform_edt(image) #距離變換local_maxi =feature.peak_local_max(distance, indices=False, footprint=np.ones((3, 3)),       labels=image) #尋找峰值markers = ndi.label(local_maxi)[0] #初始標記點labels =morphology.watershed(-distance, markers, mask=image) #基於距離變換的分水嶺演算法fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))axes = axes.ravel()ax0, ax1, ax2, ax3 = axesax0.imshow(image, cmap=plt.cm.gray, interpolation='nearest')ax0.set_title("Original")ax1.imshow(-distance, cmap=plt.cm.jet, interpolation='nearest')ax1.set_title("Distance")ax2.imshow(markers, cmap=plt.cm.spectral, interpolation='nearest')ax2.set_title("Markers")ax3.imshow(labels, cmap=plt.cm.spectral, interpolation='nearest')ax3.set_title("Segmented")for ax in axes: ax.axis('off')fig.tight_layout()plt.show()

分水嶺演算法也可以和梯度相結合,來實現映像分割。一般梯度映像在邊緣處有較高的像素值,而在其它地方則有較低的像素值,理想情況 下,分山嶺恰好在邊緣。因此,我們可以根據梯度來尋找分山嶺。

例2:基於梯度的分水嶺映像分割

import matplotlib.pyplot as pltfrom scipy import ndimage as ndifrom skimage import morphology,color,data,filterimage =color.rgb2gray(data.camera())denoised = filter.rank.median(image, morphology.disk(2)) #過濾雜訊#將梯度值低於10的作為開始標記點markers = filter.rank.gradient(denoised, morphology.disk(5)) <10markers = ndi.label(markers)[0]gradient = filter.rank.gradient(denoised, morphology.disk(2)) #計算梯度labels =morphology.watershed(gradient, markers, mask=image) #基於梯度的分水嶺演算法fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(6, 6))axes = axes.ravel()ax0, ax1, ax2, ax3 = axesax0.imshow(image, cmap=plt.cm.gray, interpolation='nearest')ax0.set_title("Original")ax1.imshow(gradient, cmap=plt.cm.spectral, interpolation='nearest')ax1.set_title("Gradient")ax2.imshow(markers, cmap=plt.cm.spectral, interpolation='nearest')ax2.set_title("Markers")ax3.imshow(labels, cmap=plt.cm.spectral, interpolation='nearest')ax3.set_title("Segmented")for ax in axes: ax.axis('off')fig.tight_layout()plt.show()

相關推薦:

python數位影像處理之進階形態學處理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.