影象分割演算法:基於梯度分水嶺的分割演算法
阿新 • • 發佈:2019-02-07
程式碼實現:
參考部落格:https://www.cnblogs.com/denny402/p/5167414.htmlimport matplotlib.pyplot as plt from scipy import ndimage as ndi from skimage import morphology,color,data,filters import cv2 # image =color.rgb2gray(data.camera()) image =cv2.imread('./Project/test/test.jpg',0) denoised = filters.rank.median(image, morphology.disk(2)) #過濾噪聲 #將梯度值低於10的作為開始標記點 markers = filters.rank.gradient(denoised, morphology.disk(5)) <10 markers = ndi.label(markers)[0] gradient = filters.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 = axes ax0.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()