原始图像
####(1)canny算子
import cv2
import numpy as np
img = cv2.imread("1.jpg", 0)
img = cv2.GaussianBlur(img, (3, 3), 0)
canny = cv2.Canny(img, 50, 150)
cv2.imshow('Canny', canny)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('picture\\candy.jpg', canny)
效果:
(2)sobel算子
import cv2
import numpy as np
img = cv2.imread("1.jpg", 0)
img = cv2.GaussianBlur(img, (3, 3), 0)
sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
sobel_x = cv2.convertScaleAbs(sobel_x)
sobel_y = cv2.convertScaleAbs(sobel_y)
sobel = cv2.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0)
cv2.imshow('Sobel', sobel)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('picture/sobel.jpg', sobel)
效果: