python+opencv——canny算子阈值在线调整

本文介绍了一个使用OpenCV实现的Canny边缘检测算法实例。通过调整两个阈值,可以改变边缘检测的效果。代码中实现了使用回调函数来动态调整阈值,并通过滑动条直观地展示效果变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近学习opencv,从图像处理最基础的开始,练习了一些最简单的边缘检测算法,参考了这篇博文。讲的挺细,就是到最后的canny程序有点缺失,无奈自己动手咯。正好也学学使用回调函数。参考博文链接:https://blog.youkuaiyun.com/qq_40962368/article/details/81416954

直接上代码了,完成的任务是可以使用滑标调整canny的两个阈值,更改一下图片的名称即可使用,希望能帮到各位。

import cv2
import numpy as np
import time

lowThreshold = 0
max_lowThreshold = 100

maxThreshold = 100
max_maxThreshold = 200
kernel_size = 3

def canny_low_threshold(intial):
    blur = cv2.GaussianBlur(img, (3, 3), 0)
    canny = cv2.Canny(blur, intial,maxThreshold)  # x是最小阈值,y是最大阈值
    cv2.imshow('canny', canny)

def canny_max_threshold(intial):
    blur = cv2.GaussianBlur(img, (3, 3), 0)
    canny = cv2.Canny(blur, lowThreshold,intial)  # x是最小阈值,y是最大阈值
    cv2.imshow('canny', canny)

img = cv2.imread('./image_pack/shusongdai.jpg',0)   # 后面参数0即是以灰度读取

cv2.namedWindow('canny', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
cv2.createTrackbar('Min threshold', 'canny', lowThreshold, max_lowThreshold, canny_low_threshold)
cv2.createTrackbar('Max threshold', 'canny', maxThreshold, max_maxThreshold, canny_max_threshold)
canny_low_threshold(0)

if cv2.waitKey(0) == 27:    # 27是ESC键值
    cv2.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值