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