Canny边缘检测

canny边缘检测算法
五个步骤:
1. 高斯模糊:GaussianBlur
2. 灰度转换:cvtColor
3. 计算梯度:Sobel/Scharr
4. 非最大信号抑制
5. 高低阈值连接输出二值图像

import cv2


def edge_demo(image):
    blurred = cv2.GaussianBlur(image, (3, 3), 0)
    gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
    gradx = cv2.Sobel(gray, cv2.CV_16SC1, 1, 0)
    grady = cv2.Sobel(gray, cv2.CV_16SC1, 0, 1)
    # edge_output = cv2.Canny(gradx, grady, 50, 150)
    edge_output = cv2.Canny(gray, 50, 150)  # 高低阈值的比例一般为3:1或者2:1
    cv2.imshow("edge output", edge_output)
    dst = cv2.bitwise_and(image, image, mask=edge_output)
    cv2.imshow("color", dst)


src = cv2.imread("0000.jpg")
src_resized = cv2.resize(src, (500, 500))
cv2.imshow("original", src_resized)
edge_demo(src_resized)
cv2.waitKey(0)

cv2.destroyAllWindows()

Canny边缘检测_第1张图片

Canny边缘检测_第2张图片

Canny边缘检测_第3张图片

你可能感兴趣的:(python)