边缘检测(Canny算子、Sobel算子)

原始图像

边缘检测(Canny算子、Sobel算子)_第1张图片
####(1)canny算子

# coding=utf-8
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)
效果:

边缘检测(Canny算子、Sobel算子)_第2张图片
(2)sobel算子

# coding=utf-8
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)
效果:

边缘检测(Canny算子、Sobel算子)_第3张图片

你可能感兴趣的:(Python实例,opencv,python,计算机视觉)