(二十六)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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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 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()

黑帽

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