對圖像進行形態學變換。變換對象一般為灰度圖或二值圖,功能函數放在morphology子模塊內。
一 膨脹(dilation)
原理:一般對二值圖像進行操作。找到像素值為1的點,將它的鄰近像素點都設置成這個值。1值表示白,0值表示黑,因此膨脹操作可以擴大白色值范圍,壓縮黑色值范圍。一般用來擴充邊緣或填充小的孔洞。
功能函數:skimage.morphology.dilation(image, selem=None)
selem表示結構元素,用於設定局部區域的形狀和大小。
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)
結果如下圖所示:
可見濾波器的大小,對操作結果的影響非常大。一般設置為奇數。除了正方形的濾波器外,濾波器的形狀還有一些,現列舉如下:
morphology.square: 正方形
morphology.disk: 平面圓形
morphology.ball: 球形
morphology.cube: 立方體形
morphology.diamond: 鑽石形
morphology.rectangle: 矩形
morphology.star: 星形
morphology.octagon: 八角形
morphology.octahedron: 八面體
注意,如果處理圖像為二值圖像(只有0和1兩個值),則可以調用:
skimage.morphology.binary_dilation(image, selem=None)
用此函數比處理灰度圖像要快。
二 腐蝕(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)
結果如下圖所示:
注意,如果處理圖像為二值圖像(只有0和1兩個值),則可以調用:
skimage.morphology.binary_erosion(image, selem=None)
用此函數比處理灰度圖像要快。
三 開運算(opening)
函數:skimage.morphology.openning(image, selem=None)
selem表示結構元素,用於設定局部區域的形狀和大小。
先腐蝕再膨脹,可以消除小物體或小斑塊。
from skimage import io,color import skimage.morphology as sm import matplotlib.pyplot as plt img=color.rgb2gray(io.imread('d:/pic/mor.png')) dst=sm.opening(img,sm.disk(9)) #用邊長為9的圓形濾波器進行膨脹濾波
plt.figure('morphology',figsize=(8,8)) plt.subplot(121) plt.title('origin image') plt.imshow(img,plt.cm.gray) plt.axis('off') plt.subplot(122) plt.title('morphological image') plt.imshow(dst,plt.cm.gray) plt.axis('off')
結果如下圖所示:
注意,如果處理圖像為二值圖像(只有0和1兩個值),則可以調用:
skimage.morphology.binary_opening(image, selem=None)
用此函數比處理灰度圖像要快。
四 閉運算(closing)
函數:skimage.morphology.closing(image, selem=None)
selem表示結構元素,用於設定局部區域的形狀和大小。
先膨脹再腐蝕,可用來填充孔洞。
注意,如果處理圖像為二值圖像(只有0和1兩個值),則可以調用:
skimage.morphology.binary_closing(image, selem=None)
用此函數比處理灰度圖像要快。
五 白帽(white-tophat)
函數:skimage.morphology.white_tophat(image, selem=None)
selem表示結構元素,用於設定局部區域的形狀和大小。
將原圖像減去它的開運算值,返回比結構化元素小的白點
from skimage import io,color import skimage.morphology as sm import matplotlib.pyplot as plt img=color.rgb2gray(io.imread('d:/pic/mor.png')) dst=sm.white_tophat(img,sm.square(21)) plt.figure('morphology',figsize=(8,8)) plt.subplot(121) plt.title('origin image') plt.imshow(img,plt.cm.gray) plt.axis('off') plt.subplot(122) plt.title('morphological image') plt.imshow(dst,plt.cm.gray) plt.axis('off')
結果如下圖所示:
六 黑帽(black-tophat)
函數:skimage.morphology.black_tophat(image, selem=None)
selem表示結構元素,用於設定局部區域的形狀和大小。
將原圖像減去它的閉運算值,返回比結構化元素小的黑點,且將這些黑點反色。
from skimage import io,color import skimage.morphology as sm import matplotlib.pyplot as plt img=color.rgb2gray(io.imread('d:/pic/mor.png')) dst=sm.black_tophat(img,sm.square(21)) plt.figure('morphology',figsize=(8,8)) plt.subplot(121) plt.title('origin image') plt.imshow(img,plt.cm.gray) plt.axis('off') plt.subplot(122) plt.title('morphological image') plt.imshow(dst,plt.cm.gray) plt.axis('off')
結果如下圖所示:
參考:https://www.cnblogs.com/denny402/p/5132677.html