此实时监测的代码为转载,原网址我忘了,如果原作者看到请联系我
canny函数原型:
输入值依次为原图,阈值1,阈值2返回值为包含边缘的二值图
具体请看:/sunny2038/article/details/9202641
edge = cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient ]]])
代码运行后会有一个横条实时更改canny函数中的阈值从而观测图片状态
import cv2import numpy as npdef CannyThreshold(lowThreshold):detected_edges = cv2.GaussianBlur(gray,(3,3),0)detected_edges = cv2.Canny(detected_edges,lowThreshold,lowThreshold*ratio,apertureSize = kernel_size)dst = cv2.bitwise_and(img,img,mask = detected_edges) # just add some colours to edges from original image.cv2.imshow('canny demo',dst)lowThreshold = 0max_lowThreshold = 100ratio = 3kernel_size = 3img = cv2.imread('82.png')gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)img = cv2.GaussianBlur(img,(3,3),0)cv2.namedWindow('canny demo')cv2.createTrackbar('Min threshold','canny demo',lowThreshold, max_lowThreshold, CannyThreshold)CannyThreshold(0) # initializationif cv2.waitKey(0) == 27:cv2.destroyAllWindows()
左图为原图,右图为阈值为0时的边缘检测
左图为原图,右图为阈值为50时的边缘检测
如果觉得《canny边缘检测 阈值调整》对你有帮助,请点赞、收藏,并留下你的观点哦!