(二十六)skimage 形态学去噪

参考博客

本文内容引用自 python数字图像处理(13):基本形态学滤波 ,有部分修改

导入依赖

from skimage import data
import skimage.morphology as sm
import matplotlib.pyplot as plt

结构元素

print('正方形')
print(sm.square(5))

print('平面圆形')
print(sm.disk(5))

print('球形')
print(sm.ball(5))

print('立方体形')
print(sm.cube(5))

print('钻石形')
print(sm.diamond(5))

print('矩形')
print(sm.rectangle(5, 4))

print('星形')
print(sm.star(5, int))

print('八角形')
print(sm.octagon(4, 3, int))

print('八面体')
print(sm.octahedron(5))
正方形
[[1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]]
平面圆形
[[0 0 0 0 0 1 0 0 0 0 0]
 [0 0 1 1 1 1 1 1 1 0 0]
 [0 1 1 1 1 1 1 1 1 1 0]
 [0 1 1 1 1 1 1 1 1 1 0]
 [0 1 1 1 1 1 1 1 1 1 0]
 [1 1 1 1 1 1 1 1 1 1 1]
 [0 1 1 1 1 1 1 1 1 1 0]
 [0 1 1 1 1 1 1 1 1 1 0]
 [0 1 1 1 1 1 1 1 1 1 0]
 [0 0 1 1 1 1 1 1 1 0 0]
 [0 0 0 0 0 1 0 0 0 0 0]]
球形
[[[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 ...

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0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0 ... 0 0 0]]

 ...

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]]

腐蚀

# 读入黑白棋盘图
img = data.checkerboard()
# 用结构元素腐蚀
dst1 = sm.erosion(img, sm.square(5))
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)

plt.subplot(132)
plt.title('morphological_5 image')
plt.imshow(dst1, plt.cm.gray)

plt.subplot(133)
plt.title('morphological_25 image')
plt.imshow(dst2, plt.cm.gray)

plt.show()

腐蚀
膨胀

# 读入黑白棋盘图
img = data.checkerboard()
# 用结构元素膨胀
dst1 = sm.dilation(img, sm.square(5))
dst2 = sm.dilation(img, sm.square(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_5 image')
plt.imshow(dst1, plt.cm.gray)

plt.subplot(133)
plt.title('morphological_25 image')
plt.imshow(dst2, plt.cm.gray)

plt.show()

膨胀
开运算

# 先腐蚀再膨胀
# 可以消除小物体或小斑块
# 黑色中去掉白色
img = color.rgb2gray(io.imread('3.jpg'))
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')

plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst, plt.cm.gray)
plt.axis('off')
plt.show()

开运算
闭运算

# 先膨胀再腐蚀
# 可用来填充孔洞
# 白色中去掉黑色
img = color.rgb2gray(io.imread('3.jpg'))
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')

plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst, plt.cm.gray)
plt.axis('off')
plt.show()

这里写图片描述
白帽

# 原图减去开运算
# 查找原图中比邻近点更亮的区域
img = color.rgb2gray(io.imread('3.jpg'))
dst = sm.white_tophat(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')

plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst, plt.cm.gray)
plt.axis('off')
plt.show()

白帽
黑帽

# 原图减去闭运算
# 查找原图中比邻近点较暗的区域
img = color.rgb2gray(io.imread('3.jpg'))
dst = sm.black_tophat(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')

plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst, plt.cm.gray)
plt.axis('off')
plt.show()

黑帽

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章