在使用传统算法处理图像时,避免不了使用阈值分割、边缘检测等多次实验的方法,需要多次尝试才能找到合适的数值,最简单最直接的方法是使用拖动进度条,灵活的对于多次尝试的方法也可以使用拖动进度条的方式,快速找到合适值
1.回调函数
def nothing(x):
pass
2.处理函数
def process(path):
img = cv2.imread(path)
img1 = img.copy()
gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
imgg = equalHist(gray)#equalHist为自定义函数
img2 = cv2.GaussianBlur(imgg, (25, 25), 0)
cv2.createTrackbar('threshold1', 'image', 0, 255, nothing)
cv2.createTrackbar('threshold2', 'image', 0, 255, nothing)
while (True):
threshold1 = cv2.getTrackbarPos('threshold1', 'image')
threshold2 = cv2.getTrackbarPos('threshold2', 'image')
img_output = cv2.Canny(img2, threshold1, threshold2)
cv2.imshow('image', img_output)
if cv2.waitKey(1) == ord(' '):#空格停止
break
cv2.destroyAllWindows()
拿到合适的值之后,可以更好地进行后续处理