OpenCV---图像形态学操作-腐蚀操作

【OpenCV+TensorFlow】清华博士带你做项目!计算机视觉实战+深度学习,项目可写进简历!(附源码资料)-人工智能/深度学习框架/RNN/池化层/感受_哔哩哔哩_bilibili
import cv2  # 读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np


def cv_show(name, img):
    cv2.imshow('image', img)  # 第一个为窗口名字
    cv2.waitKey(0)  # 等待时间,毫秒级,0表示任意键终止
    cv2.destroyAllWindows()


# # 形态学-腐蚀操作
# img = cv2.imread('renwen.jpg')
# # cv_show('img', img)
# kernel = np.ones((5, 5), np.uint8)
# erosion = cv2.erode(img, kernel, iterations=1)
# cv_show('erosion', erosion)


# pie = cv2.imread('pie.jpg')
# # cv_show('pie', pie)
# kernel = np.ones((30, 30), np.uint8)
# erosion_1 = cv2.erode(pie, kernel, iterations=1)
# erosion_2 = cv2.erode(pie, kernel, iterations=2)
# erosion_3 = cv2.erode(pie, kernel, iterations=3)
# res = np.hstack((erosion_1, erosion_2, erosion_3))
# cv_show('res', res)


# # 形态学-膨胀操作
# img = cv2.imread('renwen.jpg')
# # cv_show('img', img)
# kernel = np.ones((3, 3), np.uint8)
# dige_erosion = cv2.erode(img, kernel, iterations=1)
# dige_dilate = cv2.dilate(dige_erosion, kernel, iterations=1)
# total = np.hstack((dige_dilate, dige_erosion))
# cv_show('total', total)


# pie = cv2.imread('pie.jpg')
# kernel = np.ones((30, 30), np.uint8)
# dilate_1 = cv2.dilate(pie, kernel, iterations=1)
# dilate_2 = cv2.dilate(pie, kernel, iterations=2)
# dilate_3 = cv2.dilate(pie, kernel, iterations=3)
# res = np.hstack((dilate_1, dilate_2, dilate_3))
# cv_show('res', res)


# # 开运算与闭运算
# # 开:先腐蚀,再膨胀
# img = cv2.imread('renwen.jpg')
# kernel = np.ones((5, 5), np.uint8)
# opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
# cv_show('opening', opening)

# # 闭:先膨胀,再腐蚀
# img = cv2.imread('renwen.jpg')
# kernel = np.ones((5, 5), np.uint8)
# closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
# cv_show('closing', closing)


# # 梯度运算
# pie = cv2.imread('pie.jpg')
# 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))
# cv_show('res', res)
#
# gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)
# cv_show('gradient',gradient)


# 礼帽与黑帽
# 礼帽=原始输入-开运算结果
# 黑帽=闭运算-原始输入
# 礼帽
img = cv2.imread('renwen.jpg')
kernel = np.ones((5, 5), np.uint8)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv_show('tophat', tophat)

# 黑帽
img = cv2.imread('renwen.jpg')  # 原始轮廓
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
cv_show('blackhat', blackhat)

OpenCV---图像形态学操作-腐蚀操作_第1张图片

OpenCV---图像形态学操作-腐蚀操作_第2张图片

OpenCV---图像形态学操作-腐蚀操作_第3张图片

OpenCV---图像形态学操作-腐蚀操作_第4张图片

 cv2.MORPH_GRADIENT

OpenCV---图像形态学操作-腐蚀操作_第5张图片

 OpenCV---图像形态学操作-腐蚀操作_第6张图片OpenCV---图像形态学操作-腐蚀操作_第7张图片

 

 

你可能感兴趣的:(opencv,opencv,计算机视觉,python)