Python OpenCV – Canny()函数
在本文中,我们将看到 OpenCV 中的Canny Edge过滤器。 OpenCV 中的 Canny()函数用于检测图像中的边缘。
Syntax: cv2.Canny(image, T_lower, T_upper, aperture_size, L2Gradient)
Where:
- Image: Input image to which Canny filter will be applied
- T_lower: Lower threshold value in Hysteresis Thresholding
- T_upper: Upper threshold value in Hysteresis Thresholding
- aperture_size: Aperture size of the Sobel filter.
- L2Gradient: Boolean parameter used for more precision in calculating Edge Gradient.
Canny 边缘检测是一个由 4 个主要步骤组成的算法:
- 使用高斯平滑减少噪声。
- 使用 Sobel 滤波器计算图像梯度。
- 应用非最大值抑制或 NMS 来仅吉普车局部最大值
- 最后,应用在 Canny()函数中使用的 2 个阈值 T_upper 和 T_lower 的滞后阈值。
输入图像:
Canny()函数的基本示例
Python3
import cv2
img = cv2.imread("test.jpeg") # Read image
# Setting parameter values
t_lower = 50 # Lower Threshold
t_upper = 150 # Upper threshold
# Applying the Canny Edge filter
edge = cv2.Canny(img, t_lower, t_upper)
cv2.imshow('original', img)
cv2.imshow('edge', edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
Python3
import cv2
img = cv2.imread("test.jpeg") # Read image
# Setting All parameters
t_lower = 100 # Lower Threshold
t_upper = 200 # Upper threshold
aperture_size = 5 # Aperture size
# Applying the Canny Edge filter
# with Custom Aperture Size
edge = cv2.Canny(img, t_lower, t_upper,
apertureSize=aperture_size)
cv2.imshow('original', img)
cv2.imshow('edge', edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
Python3
import cv2
img = cv2.imread("test.jpeg") # Read image
t_lower = 100 # Lower Threshold
t_upper = 200 # Upper threshold
aperture_size = 5 # Aperture size
L2Gradient = True # Boolean
# Applying the Canny Edge filter with L2Gradient = True
edge = cv2.Canny(img, t_lower, t_upper, L2gradient = L2Gradient )
cv2.imshow('original', img)
cv2.imshow('edge', edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
Python3
import cv2
img = cv2.imread("test.jpeg") # Read image
# Defining all the parameters
t_lower = 100 # Lower Threshold
t_upper = 200 # Upper threshold
aperture_size = 5 # Aperture size
L2Gradient = True # Boolean
# Applying the Canny Edge filter
# with Aperture Size and L2Gradient
edge = cv2.Canny(img, t_lower, t_upper,
apertureSize = aperture_size,
L2gradient = L2Gradient )
cv2.imshow('original', img)
cv2.imshow('edge', edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
带有 Aperture_size 的Canny()函数
这是一个可选参数,用于指定用于计算 Canny 算法中的梯度的 Sobel 滤波器的阶数。默认值为 3,其值应为 3 到 7 之间的奇数。当您想要检测更详细的特征时,可以增加 Aperture 大小。
Python3
import cv2
img = cv2.imread("test.jpeg") # Read image
# Setting All parameters
t_lower = 100 # Lower Threshold
t_upper = 200 # Upper threshold
aperture_size = 5 # Aperture size
# Applying the Canny Edge filter
# with Custom Aperture Size
edge = cv2.Canny(img, t_lower, t_upper,
apertureSize=aperture_size)
cv2.imshow('original', img)
cv2.imshow('edge', edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
带有 L2Gradient 的Canny()函数
它是一个布尔参数,指定您是要计算通常的梯度方程还是 L2Gradient 算法。同样,它是一个可选参数。 L2gradient 不是我的 sqrt(gradient_x_square + gradient_y_square),而 L1gradient 只是 abs(gradient_x) + abs(gradient_y)。
Python3
import cv2
img = cv2.imread("test.jpeg") # Read image
t_lower = 100 # Lower Threshold
t_upper = 200 # Upper threshold
aperture_size = 5 # Aperture size
L2Gradient = True # Boolean
# Applying the Canny Edge filter with L2Gradient = True
edge = cv2.Canny(img, t_lower, t_upper, L2gradient = L2Gradient )
cv2.imshow('original', img)
cv2.imshow('edge', edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
具有孔径大小和 L2gradient 的Canny()函数
在这里,我们将在函数中使用这两个属性。
Python3
import cv2
img = cv2.imread("test.jpeg") # Read image
# Defining all the parameters
t_lower = 100 # Lower Threshold
t_upper = 200 # Upper threshold
aperture_size = 5 # Aperture size
L2Gradient = True # Boolean
# Applying the Canny Edge filter
# with Aperture Size and L2Gradient
edge = cv2.Canny(img, t_lower, t_upper,
apertureSize = aperture_size,
L2gradient = L2Gradient )
cv2.imshow('original', img)
cv2.imshow('edge', edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出: