# -*- coding=GBK -*-
import cv2 as cv
# 边缘检测述算法
def edge_image(image):
# blurred = cv.GaussianBlur(image, (3, 3), 0) # 去噪、提取特征
# cv.imshow("GaussianBlur", blurred)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
xgrad = cv.Sobel(gray, cv.CV_16SC1, 1, 0)
ygrad = cv.Sobel(gray, cv.CV_16SC1, 0, 1)
edge_output = cv.Canny(xgrad, ygrad, 50, 150)
# edges = cv2.Canny(image, threshold1, threashold2)
# 参数
# image : 原始图像
# threshold1 : 阈值1 (minVal)
# threshold2 : 阈值2 (maxVal)
# 返回值
# edges : 边缘图像
cv.imshow("canny_edge", edge_output)
dst = cv.bitwise_and(image, image, mask=edge_output)
cv.imshow("color_edge", dst)
src = cv.imread("fengling.jpg")
cv.imshow("before", src)
edge_image(src)
cv.waitKey(0)
cv.destroyAllWindows()
边缘检测前需要对图像进行滤波处理,以减少不必要的噪声和提取重要的特征
如下图所示没做滤波的边缘检测结果:
Canny和GaussianBlur参考资料:
https://www.jianshu.com/p/bfd5dd2566bb
https://blog.csdn.net/wumu720123/article/details/89578530