How to make the track bar move in steps e.g. step 1 = 25.5 and step 10= 255

Hello all, I am currently finding a solution to my problem very hard to come by what I want to do is create a trackbar with 10 settings corresponding to 0-255 and use this trackbar to carry out image thresholding.

I have tried all sorts of approaches and none have worked, I will show my code below.

import cv2

def bar(x):
pass

cv2.namedWindow("Image")##Names the window with the trackbar "image"
cv2.createTrackbar("R", "Image", 255, 10, bar)##Creates trackbar with values 0-10

while True:##Loop for getting trackbar position and processing it

R = cv2.getTrackbarPos('R','Image')
r=2*R
value_threshold = cv2.getTrackbarPos("R", "Image") == 199 ((199//25.5) +1) * 25,5 == 204) ##Gets trackbar position

_, bar = cv2.threshold(img, value_threshold,  255,r, cv2.THRESH_BINARY)##Applies threshold

##Shows images in window
##cv2.imshow("Image", img)
cv2.imshow("Image", bar)

key = cv2.waitKey(10)
if key == 27:##If escape is pressed break
break


cv2.destroyAllWindows()

edit retag close merge delete

What is this value_threshold = cv2.getTrackbarPos("R", "Image") == 199 ((199//25.5) +1) * 25,5 == 204)

Here is snippet code:

values = []
for j in range(25.5, 255, 10):
v = cv2.getTrackbarPos("%s" % (j), "Trackbars")
values.append(v)


I will try this and let you know the outcome :)

I let you know. Are you using Spyder python? What version opencv?

Sort by » oldest newest most voted

It looks like you are confused about the last parameter in createTrackbar. You are using it as a result to threshold and in your imshow statement as if it is an image. It is actually a method that's called when the user interacts with the trackbar. In my example, bar() prints the value of the trackbar whenever it is changed. (When you don't need to see that value any more, you can change the body of the method to pass.) I'm using a variable to set the maximum value of the trackbar so that you can easily change the number of thresholding steps. You multiply the trackbar value by 255 / thresholdSteps to get your threshold value. When thresholdSteps is set to 10, you'll get your values of 0 - 255 in steps of 25.5.

import cv2

def bar(x):
""" Print the value of the trackbar whenever it changes """
print(f"value: {x}")

thresholdSteps = 10
beginningTrackbarValue = 3  # Used in the call to createTrackbar to set the initial trackbar value
cv2.namedWindow("Image")
cv2.createTrackbar("trackbar1", "Image", beginningTrackbarValue, thresholdSteps, bar)

previousTrackbarValue = -1  # Set this to -1 so the threshold will be applied and the image displayed the first time through the loop
while True:
newTrackBarValue = cv2.getTrackbarPos("trackbar1", "Image")
# Don't process the image if the trackbar value is still the same
if newTrackBarValue != previousTrackbarValue:
thresholdValue = newTrackBarValue * 255 / thresholdSteps
print(f"threshold value: {thresholdValue}")
_, threshImg = cv2.threshold(img, thresholdValue, 255, cv2.THRESH_BINARY)
cv2.imshow("Image", threshImg)
previousTrackbarValue = newTrackBarValue
key = cv2.waitKey(10)
if key == 27:
break

more