python數字影象處理(13):基本形態學濾波
對影象進行形態學變換。變換物件一般為灰度圖或二值圖,功能函式放在morphology子模組內。
1、膨脹(dilation)
原理:一般對二值影象進行操作。找到畫素值為1的點,將它的鄰近畫素點都設定成這個值。1值表示白,0值表示黑,因此膨脹操作可以擴大白色值範圍,壓縮黑色值範圍。一般用來擴充邊緣或填充小的孔洞。
功能函式:skimage.morphology.dilation(image, selem=None)
selem表示結構元素,用於設定區域性區域的形狀和大小。
from skimage import data import skimage.morphology as smimport 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)
分別用邊長為5或15的正方形濾波器對棋盤圖片進行膨脹操作,結果如下:
可見濾波器的大小,對操作結果的影響非常大。一般設定為奇數。
除了正方形的濾波器外,濾波器的形狀還有一些,現列舉如下:
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)
用此函式比處理灰度影象要快。
2、腐蝕(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)
用此函式比處理灰度影象要快。
3、開運算(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)
用此函式比處理灰度影象要快。
4、閉運算(closing)
函式:skimage.morphology.closing(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.closing(img,sm.disk(9)) #用邊長為5的圓形濾波器進行膨脹濾波 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_closing(image, selem=None)
用此函式比處理灰度影象要快。
5、白帽(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')
6、黑帽(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')