# 滤波操作:去噪
# 均值滤波
blur = cv2.blur(img5, (3, 3))
# 方框滤波
box = cv2.boxFilter(img5, -1, (3, 3), normalize=True)
# 高斯滤波
aussian = cv2.GussianBlur(img5, (5, 5), 1)
# 中值滤波
median = cv2.medianBlur(img5, 5)
# 展示所有,有点像拼接
res = np.hstack((blur, box, aussian, median))
# 腐蚀操作,将线条变小
kernel = np.ones((5, 5), np.uint8)
dige = cv2.erode(img, kernel, iterations=1)
# 膨胀操作,将线条变大
kernel = np.ones((5, 5), np.uint8)
dige = cv2.dilate(img, kernel, iterations=1)
# 开运算:先腐蚀再膨胀
kernel = np.ones((3, 3), np.uint8)
open = cv2.morphologyEx(img22, cv2.MORPH_OPEN, kernel)
# 闭运算:先膨胀再腐蚀
kernel = np.ones((3, 3), np.uint8)
close = cv2.morphologyEx(img22, cv2.MORPH_CLOSE, kernel)
# 梯度 = 膨胀 - 腐蚀 可描述一个图像的轮廓
kernel = np.ones((3, 3), np.uint8)
gradient = cv2.morphologyEx(img22, cv2.MORPH_GRADIENT, kernel)
# 礼帽 = 原始图像 - 开运算
kernel = np.ones((3, 3), np.uint8)
tophat = cv2.morphologyEx(img22, cv2.MORPH_TOPHAT, kernel)
# 黑帽 = 闭运算 - 原始图像
kernel = np.ones((3, 3), np.uint8)
black = cv2.morphologyEx(img22, cv2.MORPH_BLACKHAT, kernel)
# sobel算子 可描述一个图像的轮廓
# cv2.CV_64F:既有正数又有负数,dx,dy
# 水平方向的
sobelx = cv2.Sobel(gray_img, cv2.CV_64F, 1, 0, ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
# 垂直方向的
sobely = cv2.Sobel(gray_img, cv2.CV_64F, 0, 1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
# 设置权重
sobelxy = cv2.addWeighted(sobelx, 0.6, sobely, 0.4, 0)
# Scharr算子:比sobel算子轮廓更多
# 水平方向的
sobelx = cv2.Scharr(img22, cv2.CV_64F, 1, 0)
sobelx = cv2.convertScaleAbs(sobelx)
# 垂直方向的
sobely = cv2.Scharr(img22, cv2.CV_64F, 0, 1)
sobely = cv2.convertScaleAbs(sobely)
# 设置权重
sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
# laplacian算子:比前两种的算子更不明显
lap = cv2.Laplacian(img, cv2.CV_64F)
lap = cv2.convertScaleAbs(lap)
# Canny边缘检测流程
# 1.高斯滤波去噪2.计算梯度方向(sobel算子)3.非极大值抑制 4.双阈值检测(80,150)5.抑制孤立的弱边缘
canny1 = cv2.Canny(gray_img, 80, 150)