import cv2
# 载入原始图片
img = cv2.imread("lena.jpg",1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 计算图像x方向梯度
sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0,ksize=3)
# 计算图像y方向梯度
sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1,ksize=3)
# 由于会出现负值的情况,因此使用cv2.convertScalerAbs() 转换为绝对值的形式
abs_sobel_x = cv2.convertScaleAbs(sobel_x)
abs_sobel_y = cv2.convertScaleAbs(sobel_y)
# sobel算子图
abs_sobel = cv2.addWeighted(abs_sobel_x,0.5,abs_sobel_y,0.5,0)
cv2.imshow("abs_sobel_x",abs_sobel_x)
cv2.imshow("abs_sobel_y",abs_sobel_y)
cv2.imshow("abs_sobel",abs_sobel)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出结果:
来源:CSDN
作者:陨星落云
链接:https://blog.csdn.net/qq_28368377/article/details/104579797