Ask Your Question
0

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

asked 2019-05-01 11:35:18 -0600

Bradleigh gravatar image

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


img = cv2.imread("messi5.png", cv2.IMREAD_GRAYSCALE)##Reads image from area in grayscale 
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 flag offensive close merge delete

Comments

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

supra56 gravatar imagesupra56 ( 2019-05-01 13:25:16 -0600 )edit

Here is snippet code:

values = []
    for j in range(25.5, 255, 10):
        v = cv2.getTrackbarPos("%s" % (j), "Trackbars")
        values.append(v)
supra56 gravatar imagesupra56 ( 2019-05-01 13:55:40 -0600 )edit

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

Bradleigh gravatar imageBradleigh ( 2019-05-01 13:58:35 -0600 )edit

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

supra56 gravatar imagesupra56 ( 2019-05-01 14:08:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-05-01 13:52:07 -0600

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}")


img = cv2.imread("pic4.png", cv2.IMREAD_GRAYSCALE)

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
edit flag offensive delete link more

Comments

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

Bradleigh gravatar imageBradleigh ( 2019-05-01 13:58:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-05-01 11:35:18 -0600

Seen: 3,700 times

Last updated: May 01 '19