from skimage import data
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=data.checkerboard()
用边长为5的正方形滤波器进行膨胀滤波:
dst1=sm.dilation(img,sm.square(5))
/tmp/ipykernel_463/1982442659.py:1: FutureWarning: `square` is deprecated since version 0.25 and will be removed in version 0.27. Use `skimage.morphology.footprint_rectangle` instead. dst1=sm.dilation(img,sm.square(5))
用边长为15的正方形滤波器进行膨胀滤波:
dst2=sm.dilation(img,sm.square(15))
/tmp/ipykernel_463/2403410989.py:1: FutureWarning: `square` is deprecated since version 0.25 and will be removed in version 0.27. Use `skimage.morphology.footprint_rectangle` instead. dst2=sm.dilation(img,sm.square(15))
plt.figure('morphology',figsize=(8,8))
plt.subplot(131)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7f92ac8af0e0>
plt.subplot(132)
plt.title('morphological image')
plt.imshow(dst1,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7f92a4433fe0>
plt.subplot(133)
plt.title('morphological image')
plt.imshow(dst2,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7f92a4493830>
分别用边长为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)
用此函数比处理灰度图像要快。
腐蚀(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()
用边长为5的正方形滤波器进行膨胀滤波:
dst1=sm.erosion(img,sm.square(5))
/tmp/ipykernel_463/1850116054.py:1: FutureWarning: `square` is deprecated since version 0.25 and will be removed in version 0.27. Use `skimage.morphology.footprint_rectangle` instead. dst1=sm.erosion(img,sm.square(5))
用边长为25的正方形滤波器进行膨胀滤波:
dst2=sm.erosion(img,sm.square(25))
/tmp/ipykernel_463/108573180.py:1: FutureWarning: `square` is deprecated since version 0.25 and will be removed in version 0.27. Use `skimage.morphology.footprint_rectangle` instead. dst2=sm.erosion(img,sm.square(25))
plt.figure('morphology',figsize=(8,8))
plt.subplot(131)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7f92a44e16a0>
plt.subplot(132)
plt.title('morphological image')
plt.imshow(dst1,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7f92a46b0050>
plt.subplot(133)
plt.title('morphological image')
plt.imshow(dst2,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7f92a4713f50>
from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('/data/demo/sec09_xzv8.png'))
用边长为9的圆形滤波器进行膨胀滤波:
dst=sm.opening(img,sm.disk(9))
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))
from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('/data/demo/sec09_xzv8.png'))
用边长为5的圆形滤波器进行膨胀滤波:
dst=sm.closing(img,sm.disk(9))
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))
from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('/data/demo/sec09_xzv8.png'))
dst=sm.white_tophat(img,sm.square(21))
/tmp/ipykernel_463/4003695167.py:5: FutureWarning: `square` is deprecated since version 0.25 and will be removed in version 0.27. Use `skimage.morphology.footprint_rectangle` instead. 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')
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))
from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('/data/demo/sec09_xzv8.png'))
dst=sm.black_tophat(img,sm.square(21))
/tmp/ipykernel_463/4184403036.py:5: FutureWarning: `square` is deprecated since version 0.25 and will be removed in version 0.27. Use `skimage.morphology.footprint_rectangle` instead. 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')
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))