Python OpenCV setTrackbarMax() Method
In this article, we will cover how to setTrackbarMax() python in OpenCV.
A GUI element that let the user select a specific value within a range of values by sliding a slider linearly is known as a trackbar. This element is similar to scrolling but it lets the user select a specific value with its minimum and maximum limits. In this article, we will discuss how to set the maximum position of the trackbar. The maximum value of the track bar can be achieved using the function setTrackbarMax(). This function sets the maximum position of the specified trackbar in the specified window after creating the trackbar.
Syntax: cv.setTrackbarMax(trackbarname, winname, maxval)
Parameters:
- trackbarname: Name of the trackbar
- winname: Name of the window that is the parent of trackbar
- maxval: New maximum position
Return: None
Example 1:
In this example, we have created a window with a black image. Further, we create a trackbar using the createTrackbar() function and set the maximum value of trackbar using setTrackbarMax() function to 200. After that, we will create a loop for displaying an image and trackbar. So, whenever we move the trackbar position, the shades of black change.
Python3
import cv2
import numpy
def nothing(x):
pass
img = numpy.zeros(( 300 , 512 , 3 ), numpy.uint8)
cv2.namedWindow( 'image' )
cv2.createTrackbar( 'color_track' , 'image' , 0 , 255 , nothing)
cv2.setTrackbarMax( 'color_track' , 'image' , 200 )
while ( True ):
cv2.imshow( 'image' , img)
k = cv2.waitKey( 1 ) & 0xFF
if k = = 27 :
break
color = cv2.getTrackbarPos( 'color_track' , 'image' )
img[:] = [color]
cv2.destroyAllWindows()
|
Output:
Example 2:
In this example, we have created a window with a black image. Further, we create three trackbars of red, green, and blue color using the createTrackbar() function and set the maximum values of the trackbar using the setTrackbarMax() function to 100, 200, and 150. Later, we create a loop for displaying an image and trackbar. So, whenever we move the trackbar positions, the shades of red, green, and blue change.
Python
import cv2
import numpy
def nothing(x):
pass
img = numpy.zeros(( 300 , 512 , 3 ), numpy.uint8)
cv2.namedWindow( 'image' )
cv2.createTrackbar( 'Red' , 'image' , 0 , 255 , nothing)
cv2.createTrackbar( 'Green' , 'image' , 0 , 255 , nothing)
cv2.createTrackbar( 'Blue' , 'image' , 0 , 255 , nothing)
cv2.setTrackbarMax( 'Green' , 'image' , 100 )
cv2.setTrackbarMax( 'Red' , 'image' , 200 )
cv2.setTrackbarMax( 'Blue' , 'image' , 150 )
while ( True ):
cv2.imshow( 'image' , img)
k = cv2.waitKey( 1 ) & 0xFF
if k = = 27 :
break
red_color = cv2.getTrackbarPos( 'Red' , 'image' )
green_color = cv2.getTrackbarPos( 'Green' , 'image' )
blue_color = cv2.getTrackbarPos( 'Blue' , 'image' )
img[:] = [blue_color, green_color, red_color]
cv2.destroyAllWindows()
|
Output:
Last Updated :
04 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...