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() |
So at the top we have first imported OpenCV , after that you can see that we have an empty method trackChanged(), this is just a method and we give method as parameter when we create our Trackbar. this method is just an empty method but it is usually used for the actions when the OpenCV tracks changed.
if you com down we have cv2.createTrackbar() this is for creating of Trackbars and the parameters that we are going to give the first one is the Max Trackbar the second is the name of color track bar and the third one is the min and max of track bar , and the last one is the method that we have previously mentioned. we do the same process for Min Trackbar in the while loop , we are going to get the track bar position in OpenCV by using cv2.getTrackbarPos() the first parameter is the Max and the second is the name of Trackbar, we do the same process for the min also. and we have used two Thresholds for our images.
If you run the complete code this will be the result
FAQs:
What is HSV green range in OpenCV?
HSV (Hue, Saturation, Value) color space is often used in computer vision and image processing tasks. In OpenCV, the green color range in HSV typically corresponds to a hue range of approximately 60 to 120 degrees. This means that greenish colors in an image would fall inside this range when represented in the HSV color space.
What is HSV in color thresholding?
HSV (Hue, Saturation, Value) is a color space that represents colors based on their hue, saturation, and value. In color thresholding, HSV is often preferred over other color spaces like RGB, because it separates the intensity information (value) from the color information (hue and saturation). This separation makes it easier to define color ranges and perform color-based segmentation tasks.
Subscribe and Get Free Video Courses & Articles in your Email