python+openCV使用滑动条调节阈值进行Canny边缘检测

Canny边缘检测一般流程

  • 高斯滤波,平滑图像,减弱噪声
  • 计算图像梯度大小和方向
  • 非极大值抑制,稀疏边缘
  • 滞后阈值(双阈值检测)

函数cv2.Canny(image, threshold1, threshold2[, apertureSize[, L2gradient]])

参数:

  • image:深度为8位的图像
  • threshold1:滞后阈值中的低阈值
  • threshold2:滞后阈值中的高阈值
  • apertureSize:Sobel边缘检测中的卷积框大小
  • L2gradient:精度设置,默认为False

返回值

单通道且图像深度为8位的图像,即灰度图像

e.g.

设置两个滑动条,分别控制threshold1,threshold2

import cv2
#载入图片
img_original=cv2.imread('E:\ImagesFavour\\5.2.3.jpg')
#设置窗口
cv2.namedWindow('Canny')
#定义回调函数
def nothing(x):
    pass
#创建两个滑动条,分别控制threshold1,threshold2
cv2.createTrackbar('threshold1','Canny',50,400,nothing)
cv2.createTrackbar('threshold2','Canny',100,400,nothing)
while(1):
    #返回滑动条所在位置的值
    threshold1=cv2.getTrackbarPos('threshold1','Canny')
    threshold2=cv2.getTrackbarPos('threshold2','Canny')
    #Canny边缘检测
    img_edges=cv2.Canny(img_original,threshold1,threshold2)
    #显示图片
    cv2.imshow('original',img_original)
    cv2.imshow('Canny',img_edges)  
    if cv2.waitKey(1)==ord('q'):
        break
cv2.destroyAllWindows()

结果显示如下:

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田土豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值