笔记和用法都在代码注释中 :
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
#-----------------------------------------------------------------------腐蚀操作(erode)
# ------------------------------彩色图片被黑色腐蚀!!!!:
# img = cv.imread('666.jpg')
# img = cv.resize(img,(700,700))
#
# kernel = np.ones((10,10),np.uint8) #数值也大,效果越明显!!
#
# erosion = cv.erode(img,kernel,iterations=1)
# cv.imshow('erosion',erosion)
# cv.waitKey(0)
# cv.destroyAllWindows()
# ---------------------------------------------------第三种直接体验:
#------------------------------------------被黑色腐蚀:
# pie = cv.imread('666.jpg',cv.IMREAD_GRAYSCALE) #将图像读取为黑白的!!!
# pie = cv.resize(pie,(500,500)) #控制读取照片的大小,保证三张照片都可以看见!!!
#
# kernel = np.ones((3,3),np.uint8) #其中的3为像素矩阵大小,我们可以改变大小,来感受不同的效果!
#对比处理!!!
# erosion_1 = cv.erode(pie,kernel,iterations=1) #设置1,2,3种读取,进行对比!!!
# erosion_2 = cv.erode(pie,kernel,iterations=2) #执行两次腐蚀
# erosion_3 = cv.erode(pie,kernel,iterations=3) #执行三次腐蚀
#
# res = np.hstack((erosion_1,erosion_2,erosion_3)) #将三张图片横着排列出来!!
#
# cv.imshow('res',res)
# cv.waitKey(0)
# cv.destroyAllWindows()
#-----------------------------------------------------------------------碰撞操作: (dilate)
#------------------------------------------被白色腐蚀:
# pie = cv.imread('666.jpg',cv.IMREAD_GRAYSCALE) #将图像读取为黑白的!!!
# pie = cv.resize(pie,(500,500)) #控制读取照片的大小,保证三张照片都可以看见!!!
#
# kernel = np.ones((3,3),np.uint8) #其中的3为像素矩阵大小,我们可以改变大小,来感受不同的效果!
# erosion_1 = cv.dilate(pie,kernel,iterations=1) #设置1,2,3种读取,进行对比!!!
# erosion_2 = cv.dilate(pie,kernel,iterations=2)
# erosion_3 = cv.dilate(pie,kernel,iterations=3)
#
# res = np.hstack((erosion_1,erosion_2,erosion_3)) #将三张图片横着排列出来!!
#
# cv.imshow('res',res)
# cv.waitKey(0)
# cv.destroyAllWindows()
#----------------------------------------------------------------------------开运算: 先进行腐蚀,再进行膨胀!!!
# img = cv.imread('666.jpg',cv.IMREAD_GRAYSCALE) #将图像读取为黑白的!!!
# img = cv.resize(img,(500,500)) #控制读取照片的大小,保证三张照片都可以看见!!!
# kernel = np.ones((3,3),np.uint8)
#
# opening1 = cv.morphologyEx(img,cv.MORPH_OPEN,kernel,iterations=1) #开运算!!!
# opening2 = cv.morphologyEx(img,cv.MORPH_OPEN,kernel,iterations=2) #开运算!!!
# opening3 = cv.morphologyEx(img,cv.MORPH_OPEN,kernel,iterations=3) #开运算!!!
#
# opening = np.hstack((opening1,opening2,opening3))
#
# cv.imshow('opening',opening)
# cv.waitKey(0)
# cv.destroyAllWindows()
#----------------------------------------------------------------------------闭运算: 先进行膨胀,再进行腐蚀!!!
# img = cv.imread('666.jpg',cv.IMREAD_GRAYSCALE) #将图像读取为黑白的!!!
# img = cv.resize(img,(500,500)) #控制读取照片的大小,保证三张照片都可以看见!!!
# kernel = np.ones((3,3),np.uint8)
#
#对比处理!!!
# opening1 = cv.morphologyEx(img,cv.MIXED_CLONE,kernel,iterations=1) #闭运算!!!
# opening2 = cv.morphologyEx(img,cv.MIXED_CLONE,kernel,iterations=2) #闭运算!!!
# opening3 = cv.morphologyEx(img,cv.MIXED_CLONE,kernel,iterations=3) #闭运算!!!
#
# opening = np.hstack((opening1,opening2,opening3))
#
# cv.imshow('opening',opening)
# cv.waitKey(0)
# cv.destroyAllWindows()
#-------- ----梯度运算 : 膨胀-腐蚀--------------
# -----------------------------------------------第一种对比一下:
# img = cv.imread('666.jpg',cv.IMREAD_GRAYSCALE) #将图像读取为黑白的!!!
# img = cv.resize(img,(500,500)) #控制读取照片的大小,保证三张照片都可以看见!!!
# kernel = np.ones((3,3),np.uint8)
# dilate = cv.dilate(img,kernel,iterations=5)
# erosion = cv.erode(img,kernel,iterations=5)
#
# res = np.hstack((dilate.erosion))
#
# cv.imshow('res',res)
# cv.waitKey(0)
# cv.destroyAllWindows()
# -------------------------------------------第二种:梯度运算
# img = cv.imread('666.jpg',cv.IMREAD_GRAYSCALE) #将图像读取为黑白的!!!
# img = cv.resize(img,(500,500))
#
# kernel = np.ones((3,3),np.uint8)
#
# #对比处理!!!
# gradient1 = cv.morphologyEx(img,cv.MORPH_GRADIENT,kernel,iterations=1) #梯度运算!!!
# gradient2 = cv.morphologyEx(img,cv.MORPH_GRADIENT,kernel,iterations=2) #梯度运算!!!
# gradient3 = cv.morphologyEx(img,cv.MORPH_GRADIENT,kernel,iterations=3) #梯度运算!!!
#
# # opening = np.hstack((opening1,opening2,opening3))
# gradient = np.hstack((gradient1,gradient2,gradient3))
# cv.imshow('gradient',gradient)
# cv.waitKey(0)
# cv.destroyAllWindows()
# -------------------------------------------第四种:礼帽
#------------原始输入 - 开运算结果
# img = cv.imread('666.jpg',cv.IMREAD_GRAYSCALE) #将图像读取为黑白的!!!
# img = cv.resize(img,(500,500))
#
# kernel = np.ones((3,3),np.uint8)
#
# topHat = cv.morphologyEx(img,cv.MORPH_TOPHAT,kernel) #效果:差不多全部变黑!!!
#
# cv.imshow('topHat',topHat)
# cv.waitKey(0)
# cv.destroyAllWindows()
# -------------------------------------------第五种:黑帽
#-----------黑帽 = 闭运算 - 原始输入
img = cv.imread('666.jpg',cv.IMREAD_GRAYSCALE) #将图像读取为黑白的!!!
img = cv.resize(img,(500,500))
kernel = np.ones((3,3),np.uint8)
blackHat = cv.morphologyEx(img,cv.MORPH_BLACKHAT,kernel) #效果:差不多全部变黑!!!
cv.imshow('blackHat',blackHat)
cv.waitKey(0)
cv.destroyAllWindows()