OpenCV 5.图像梯度处理

5.图像梯度处理

#图像梯度处理   Sobel算子  Schaar算子  Laplacian算子

#加载OpenCV
import cv2
import numpy as np



def imgShow(title,imgData):
    cv2.imshow(title,imgData)
    cv2.waitKey()
    cv2.destroyAllWindows()


img = cv2.imread(r"C:\Users\Administrator\Pictures\Camera Roll\lenna.jpg",cv2.IMREAD_GRAYSCALE)
#Sobel算子求GX轴的边缘 cv2.Sobel(imgData,cv2.CV_64F,GX,GY,ksize=3)          cv2.CV_64F 65-128保留负值
img_x = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
img_x = cv2.convertScaleAbs(img_x)  #取绝对值 cv2.convertScaleAbs(img_x) 
img_y = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
img_y = cv2.convertScaleAbs(img_y)  #取绝对值 cv2.convertScaleAbs(imgData) 
#将Gx与Gy边缘检测图像融合
img_result = cv2.addWeighted(img_x,0.5,img_y,0.5,0)
result = np.hstack((img_x,img_y,img_result))
# imgShow(r"result",result)



#Scharr算子  可以显示更多的细节边缘  cv2.Scharr(imgData,cv2.CV_64F,GX,GY) 
img_sx = cv2.Scharr(img,cv2.CV_64F,0,1)
img_sx = cv2.convertScaleAbs(img_sx)
img_sy = cv2.Scharr(img,cv2.CV_64F,1,0)
img_sy = cv2.convertScaleAbs(img_sy)
img_sresult = cv2.addWeighted(img_sx,0.5,img_sy,0.5,0)
sresult = np.hstack((img_sx,img_sy,img_sresult))
# imgShow(r"result",sresult)


#Laplacian算子  对噪声点过于敏感  cv2.Laplacian(imgData,cv2.CV_64F)
img_lap = cv2.Laplacian(img,cv2.CV_64F)
img_lap = cv2.convertScaleAbs(img_lap)
# imgShow(r"result",img_lap)



ret = np.hstack((img_result,img_sresult,img_lap))
imgShow(r"compare",ret)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值