Sobel算子
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5ac44601e5ee28739647ed8ec3836244.png)
def sobel_(src):
sobel_x = cv2.Sobel(src,cv2.CV_64F,1,0,ksize=3)
sobel_y = cv2.Sobel(src,cv2.CV_64F,0,1,ksize=3)
sobel_x = cv2.convertScaleAbs(sobel_x)
sobel_y = cv2.convertScaleAbs(sobel_y)
sobel_xy = cv2.addWeighted(sobel_x,0.5,sobel_y,0.5,0)
Scharr算子
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3f5b4a15c1be2bcaa9d529c52dfdd97f.png)
def scharr_(src):
scharr_x = cv2.Scharr(src,cv2.CV_64F,1,0)
scharr_y = cv2.Scharr(src,cv2.CV_64F,0,1)
scharr_x = cv2.convertScaleAbs(scharr_x)
scharr_y = cv2.convertScaleAbs(scharr_y)
scharr_xy = cv2.addWeighted(scharr_x,0.5,scharr_y,0.5,0)
Laplacian算子
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ad350c625fe4175f082175daf0e32198.png)
def laplacian_(src):
laplacian = cv2.Laplacian(src,cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)
完整代码
# -*- coding:utf-8 -*-
import numpy as np
import cv2
def display(img):
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
def sobel_(src):
sobel_x = cv2.Sobel(src,cv2.CV_64F,1,0,ksize=3)
sobel_y = cv2.Sobel(src,cv2.CV_64F,0,1,ksize=3)
sobel_x = cv2.convertScaleAbs(sobel_x)
sobel_y = cv2.convertScaleAbs(sobel_y)
sobel_xy = cv2.addWeighted(sobel_x,0.5,sobel_y,0.5,0)
return sobel_xy
def sobel_direct(src):
sobel_xy = cv2.Sobel(src,cv2.CV_64F,1,1,ksize=3)
sobel_xy = cv2.convertScaleAbs(sobel_xy)
return sobel_xy
def scharr_(src):
scharr_x = cv2.Scharr(src,cv2.CV_64F,1,0)
scharr_y = cv2.Scharr(src,cv2.CV_64F,0,1)
scharr_x = cv2.convertScaleAbs(scharr_x)
scharr_y = cv2.convertScaleAbs(scharr_y)
scharr_xy = cv2.addWeighted(scharr_x,0.5,scharr_y,0.5,0)
return scharr_xy
def laplacian_(src):
laplacian = cv2.Laplacian(src,cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)
return laplacian
def main():
pic = "a.png"
img = cv2.imread(pic)
sobel = sobel_(img)
scharr =scharr_(img)
laplacian=laplacian_(img)
res = np.hstack((sobel,scharr,laplacian))
display(res)
if __name__ == "__main__":
main()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/23215e6eef46f61be072a727a91d3211.png)