Python OpenCV Color Trackbar
In this Python OpenCV tutorial we are going to talk about Python OpenCV Color Trackbar, basically we using THRESH_BINARY and THRESH_TOZERO in this article with Color Trackbar. This is the complete source code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import cv2 # Function to be called when the trackbar value changes (placeholder) def trackChanged(x): pass # Create a named window to display color trackbars cv2.namedWindow('Color Track Bar') # Define names for the trackbars hh = 'Max' hl = 'Min' # Create trackbars for maximum and minimum values cv2.createTrackbar("Max", 'Color Track Bar', 0, 255, trackChanged) cv2.createTrackbar("Min", 'Color Track Bar', 0, 255, trackChanged) # Load and resize an image img = cv2.imread('lena.tif') img = cv2.resize(img, (0, 0), fx=0.55, fy=0.55) while (True): # Get the current position of the trackbars hul = cv2.getTrackbarPos("Max", 'Color Track Bar') huh = cv2.getTrackbarPos("Min", 'Color Track Bar') # Apply thresholding with different methods based on the trackbar values ret, thresh1 = cv2.threshold(img, hul, huh, cv2.THRESH_BINARY) ret, thresh4 = cv2.threshold(img, hul, huh, cv2.THRESH_TOZERO) # Display the thresholded images cv2.imshow("thresh1", thresh1) cv2.imshow("thresh4", thresh4) # Break the loop if the ESC key is pressed if cv2.waitKey(1) == 27: break # Close all windows cv2.destroyAllWindows() |
…