码迷,mamicode.com
首页 > 其他好文 > 详细

图像分析之图像形态学处理

时间:2020-11-20 12:19:31      阅读:23      评论:0      收藏:0      [点我收藏+]

标签:nump   nbsp   des   col   输入   osi   plot   code   绘制   

1.腐蚀操作

import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB

img = cv2.imread(dige.png)
cv2.imshow(img, img)

# 腐蚀操作
#kernel 腐蚀范围, iterations 迭代次数
kernel = np.ones((3, 3), np.uint8) erosion = cv2.erode(img, kernel, iterations = 1) cv2.imshow(erosion, erosion) cv2.waitKey(0) cv2.destroyAllWindows()

 

 

2.膨胀操作

img = cv2.imread(dige.png)
cv2.imshow(img, img)
# 膨胀操作
#kernel 腐蚀范围, iterations 迭代次数
kernel = np.ones((3,3),np.uint8) dige_dilate = cv2.dilate(img,kernel,iterations = 1) cv2.imshow(dilate, dige_dilate) cv2.waitKey(0) cv2.destroyAllWindows()

3.开运算与闭运算

# 开:先腐蚀,再膨胀
img = cv2.imread(dige.png)

kernel = np.ones((5,5),np.uint8) 
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

cv2.imshow(opening, opening)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 闭:先膨胀,再腐蚀
img = cv2.imread(dige.png)

kernel = np.ones((5,5),np.uint8) 
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

cv2.imshow(closing, closing)
cv2.waitKey(0)
cv2.destroyAllWindows()

4.梯度运算(绘制图像的边框)

# 梯度=膨胀-腐蚀
pie = cv2.imread(pie.png)
kernel = np.ones((7, 7), np.uint8)
dilate = cv2.dilate(pie, kernel, iterations = 5)
erosion = cv2.erode(pie, kernel, iterations = 5)
res = np.hstack((dilate, erosion))
cv2.imshow(res, res)

# 梯度运算
gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)
cv2.imshow(gradient, gradient)

cv2.waitKey(0)
cv2.destroyAllWindows()

5.礼帽与黑帽

- 礼帽 = 原始输入-开运算结果
- 黑帽 = 闭运算-原始输入
img = cv2.imread(dige.png)
kernel = np.ones((7, 7), np.uint8)

#礼帽
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow(tophat, tophat)

#黑帽
blackhat  = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT, kernel)
cv2.imshow(blackhat , blackhat )

 

 

 

图像分析之图像形态学处理

标签:nump   nbsp   des   col   输入   osi   plot   code   绘制   

原文地址:https://www.cnblogs.com/mango1997/p/13985109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!