import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("Cat.jpg")
img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
x = cv2.Sobel(img,cv2.CV_16S,1,0)
y = cv2.Sobel(img,cv2.CV_16S,0,1)
absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
dst = cv2.addWeighted(absX,0.5,absY,0.5,0)
cv2.imshow("image",img);
cv2.imshow("Result", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b9ae4f4ed9553ea9eda0cee4cf76347e.png)
plt.imshow(absX,cmap="gray")
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/caf54e2c7a6658e39805da99d233cdb8.png)
plt.imshow(absY,cmap = "gray")
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e40c38a47c22d70c8eaa22e3453ed4b4.png)
plt.imshow(dst,cmap = "gray")
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bb2cb0ca2576d955d4c47749f7094ea7.png)
kernel = np.array((
[1,0,-1],
[1,0,-1],
[1,0,-1]),dtype="float32")
img_1 = cv2.filter2D(img,-1,kernel)
plt.imshow(img_1,cmap = "gray")
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cab934ad7be87e2045ecc5e6fccb055b.png)
img_1 = cv2.filter2D(img,-1,kernel)
plt.imshow(img_1,cmap = "gray")
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f224e8c4f7be62ecd392a8eb30677348.png)