Open In App

Python OpenCV setTrackbarMax() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python program for setTrackbarMax()
#Python OpenCV
  
# Importing the libraries OpenCV and numpy
import cv2
import numpy
  
# Create a function 'nothing' for creating trackbar
def nothing(x):
    pass
  
# Creating a window with black image
img = numpy.zeros((300, 512, 3), numpy.uint8)
cv2.namedWindow('image')
  
# Creating trackbars for color change
cv2.createTrackbar('color_track', 'image', 0, 255, nothing)
  
# Setting maximum position of 'color_track' trackbar to 200
cv2.setTrackbarMax('color_track', 'image', 200)
  
# Create a loop for displaying image and trackbar
while(True):
  
    # Display the image
    cv2.imshow('image', img)
  
    # Create a button for pressing and changing the window
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
  
    # Get current positions of trackbar
    color = cv2.getTrackbarPos('color_track', 'image')
  
    # Display color mixture
    img[:] = [color]
  
# Close the window
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




# Python program for setTrackbarMax()
#Python OpenCV
  
# Importing the libraries OpenCV and numpy
import cv2
import numpy
  
# Create a function 'nothing' for creating trackbar
def nothing(x):
    pass
  
# Creating a window with black image
img = numpy.zeros((300, 512, 3), numpy.uint8)
cv2.namedWindow('image')
  
# Creating trackbars for red color change
cv2.createTrackbar('Red', 'image', 0, 255, nothing)
  
# Creating trackbars for Green color change
cv2.createTrackbar('Green', 'image', 0, 255, nothing)
  
# Creating trackbars for Blue color change
cv2.createTrackbar('Blue', 'image', 0, 255, nothing)
  
# Setting maximum values of green color trackbar
cv2.setTrackbarMax('Green', 'image', 100)
  
# Setting maximum values of red color trackbar
cv2.setTrackbarMax('Red', 'image', 200)
  
# Setting maximum values of blue color trackbar
cv2.setTrackbarMax('Blue', 'image', 150)
  
# Create a loop for displaying image and trackbar
while(True):
  
    # Display the image
    cv2.imshow('image', img)
  
    # Create a button for pressing and changing the window
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
  
    # Get current positions of red color trackbar
    red_color = cv2.getTrackbarPos('Red', 'image')
  
    # Get current positions of green color trackbar
    green_color = cv2.getTrackbarPos('Green', 'image')
  
    # Get current positions of blue color trackbar
    blue_color = cv2.getTrackbarPos('Blue', 'image')
  
    # Display color mixture
    img[:] = [blue_color, green_color, red_color]
  
# Close the window
cv2.destroyAllWindows()


Output:

 



Last Updated : 04 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads