最近学习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()

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

被折叠的 条评论
为什么被折叠?



