dst = cv2.Sobel(src, ddepth, dx, dy, ksize)
ddepth:图像的深度
dx和dy分别表示水平和竖直方向
ksize是Sobel算子的大小
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('./noisy.jpg', cv2.IMREAD_GRAYSCALE)
#滤波
img = cv2.medianBlur(img, 5)
#计算x方向梯度
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
#计算y方向梯度
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
#梯度融合
sobel = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
plt.figure()
plt.subplot(2, 2, 1)
plt.imshow(img, 'gray')
plt.title('ORIGINAL')
plt.subplot(2, 2, 2)
plt.imshow(sobel, 'gray')
plt.title('SOBEL')
plt.subplot(2, 2, 3)
plt.imshow(sobelx, 'gray')
plt.title('sobelx')
plt.subplot(2, 2, 4)
plt.imshow(sobely, 'gray')
plt.title('sobely')
plt.show()
1.Scharr算子
2.Laplacian算子
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('./noisy.jpg', cv2.IMREAD_GRAYSCALE)
#滤波
img = cv2.medianBlur(img, 5)
#sobel算子
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
sobel = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
#scharr算子
scharrx = cv2.Scharr(img, cv2.CV_64F, 1, 0)
scharrx = cv2.convertScaleAbs(scharrx)
scharry = cv2.Scharr(img, cv2.CV_64F, 0, 1)
scharry = cv2.convertScaleAbs(scharry)
scharr = cv2.addWeighted(scharrx, 0.5, scharry, 0.5, 0)
#laplacian算子
laplacian = cv2.Laplacian(img, cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)
plt.figure()
plt.subplot(2, 2, 1)
plt.imshow(img, 'gray')
plt.title('ORIGINAL')
plt.subplot(2, 2, 2)
plt.imshow(sobel, 'gray')
plt.title('SOBEL')
plt.subplot(2, 2, 3)
plt.imshow(scharr, 'gray')
plt.title('SCHARR')
plt.subplot(2, 2, 4)
plt.imshow(laplacian, 'gray')
plt.title('LAPLACIAN')
plt.show()