Create trackbar example in OpenCV

I was not aware that OpenCV has a built-in slider, I usually use cvui for that. But for simple application, the built-in function is more handy


import cv2

cap=cv2.VideoCapture(0)

low_H=99
high_H=125

def on_low_H_thresh_trackbar(var):
    global low_H
    low_H=var

def on_high_H_thresh_trackbar(var):
    global high_H
    high_H=var

cv2.namedWindow('frame')
cv2.createTrackbar('Low H','frame',low_H,255,on_low_H_thresh_trackbar)
cv2.createTrackbar('High H','frame',high_H,255,on_high_H_thresh_trackbar)

while True:
    ret, frame=cap.read()
    
    color=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    frame=cv2.inRange(color,(low_H,0,0),(high_H,255,255))
    cv2.imshow('frame',frame)
    if cv2.waitKey(50) & 0xFF == ord('q'):
        break
        
cap.release()
cv2.destroyAllWindows()

Leave a Reply

Your email address will not be published. Required fields are marked *