"""首先,由于Canny只能处理灰度图,所以将读取的图像转成灰度图。
用高斯平滑处理原图像降噪。
调用Canny函数,指定最大和最小阈值,其中apertureSize默认为3
edge = cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient ]]])
"""
import cv2
import numpy as np
img = cv2.imread("cat.jpg", 0)
img = cv2.GaussianBlur(img, (3, 3), 0)
canny = cv2.Canny(img, 50, 150)
cv2.imshow('Canny', canny)
cv2.waitKey(0)
cv2.destroyAllWindows()
#这个程序只是静态的,在github上有一个可以在运行时调整阈值大小的程序
def CannyThreshold(lowThreshold):
detected_edges = cv2.GaussianBlur(gray, (3, 3), 0)
detected_edges = cv2.Canny(detected_edges, lowThreshold, lowThreshold * ratio, apertureSize=kernel_size)
dst = cv2.bitwise_and(img, img, mask=detected_edges) # just add some colours to edges from original image.
cv2.imshow('canny demo', dst)
lowThreshold = 0
max_lowThreshold = 100
ratio = 3
kernel_size = 3
img = cv2.imread('D:/lion.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.namedWindow('canny demo')
cv2.createTrackbar('Min threshold', 'canny demo
opencv图像处理(八)canny 边缘检测、轮廓检测
最新推荐文章于 2025-05-10 08:58:54 发布