学无先后,达者为师

网站首页 编程语言 正文

Python cv.Canny()方法参数与使用方法_python

作者:乔卿   更新时间: 2022-09-09 编程语言

函数原型与参数详解

OpenCV提供了cv.Canny()方法,该方法将输入的原始图像转换为边缘图像。

该方法的原型为:

cv.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient]]]) -> 	edges
cv.Canny(dx, dy, threshold1, threshold2[, edges[, L2gradient]]) -> edges
  • image参数是array格式的输入图像。
  • threshold1与threshold2分别是我们的下界阈值与上界阈值。
  • apertureSize是用于查找图像梯度的Sobel核的大小,默认为3。
  • L2gradient指定了求梯度幅值的公式,是一个布尔型变量,默认为False。当它为True时,使用L2,否则使用L1。

下面是具体代码:

def canny_detect(image_path, show=True):
    # 读取图像
    image = cv2.imread(image_path, 0)
    # 获取结果
    edges = cv2.Canny(image, 100, 200)
    if show:
        # 绘制原图
        plt.subplot(121)
        plt.imshow(image, cmap='gray')
        plt.title('Original Image')
        plt.xticks([])
        plt.yticks([])

        # 绘制边缘图
        plt.subplot(122)
        plt.imshow(edges, cmap='gray')
        plt.title('Edge Image')
        plt.xticks([])
        plt.yticks([])
        plt.show()
    return edges
canny_detect('images/2.jpeg')

效果

原文链接:https://qiaoxs.blog.csdn.net/article/details/125728902

栏目分类
最近更新