Python Forum
Returning Value from Function with Trackbars
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returning Value from Function with Trackbars
#1
I am tinkering with cv2 Trackbars and would like to use one to configure an image capture system. I have a trackbar function that works, but I cannot get it to return the value outside the function. I have returned values before in other functions with the "return" syntax, but the "threshold_value" and "threshold_type" are not visible outside the function.

I have no doubt I am missing something obvious, but I cannot see it. Any help is appreciated.

def Threshold(val):
threshold_type = cv.getTrackbarPos(trackbar_type, window_name)
threshold_value = cv.getTrackbarPos(trackbar_value, window_name)
_, dst = cv.threshold(src_gray, threshold_value, max_binary_value, threshold_type )
cv.imshow(window_name, dst)

print (threshold_value)
print (threshold_type)
Reply
#2
Please include the full output from the program, including any errors (in proper tags).

You don't have any indentation for your function. Can you show the proper indentation? Right now I can't tell if the print() functions are inside or outside the function.
Reply
#3
Sorry, thought I pasted the whole thing. Here is the full, formatted code, but in terms of output it creates a small window with an image and you can adjust the sliders to change the image threshold. The "print" statements are just me checking that the variable are set within the function. The whole thing works fine, but I need to pass the threshold settings to another function and getting them out has been problematic.


from __future__ import print_function
import cv2 as cv
import argparse



max_value = 255
max_type = 1
max_binary_value = 255
trackbar_type = 'Type: \n 0: Binary \n 1: Binary Inverted \n'
trackbar_value = 'Value'
window_name = 'Threshold Set'
def Threshold_Demo(val):
    #0: Binary
    #1: Binary Inverted
    
    threshold_type = cv.getTrackbarPos(trackbar_type, window_name)
    threshold_value = cv.getTrackbarPos(trackbar_value, window_name)
    _,dst = cv.threshold(src_gray, threshold_value, max_binary_value, threshold_type )
    cv.imshow(window_name, dst)
    
    print (threshold_value)
    print (threshold_type)
   
    
   

parser = argparse.ArgumentParser(description='Thresholding Evaluation Tool.')
parser.add_argument('--input', help='Path to input image.', default='1.png')
args = parser.parse_args()
src = cv.imread(cv.samples.findFile(args.input))
if src is None:
    print('Could not open or find the image: ', args.input)
    exit(0)



# Convert the image to Gray
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
cv.namedWindow(window_name)
cv.createTrackbar(trackbar_type, window_name , 3, max_type, Threshold_Demo)
# Create Trackbar to choose Threshold value
cv.createTrackbar(trackbar_value, window_name , 0, max_value, Threshold_Demo)
# Call the function to initialize
Threshold_Demo(0)


# Wait until user finishes program

cv.waitKey()
Reply
#4
You're getting the data out every time the slider changes. So depending on what you need, you could just call this other function (where you want to deliver the data) from within your Threshold_Demo(). Are you looking for something else?

def Threshold_Demo(val):
    #0: Binary
    #1: Binary Inverted
     
    threshold_type = cv.getTrackbarPos(trackbar_type, window_name)
    threshold_value = cv.getTrackbarPos(trackbar_value, window_name)
    _,dst = cv.threshold(src_gray, threshold_value, max_binary_value, threshold_type )
    cv.imshow(window_name, dst)
     
    print (threshold_value)
    print (threshold_type)
    other_function(threshold_value, threshold_type)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why my function is returning None? PauloDAS 6 1,754 Jul-17-2022, 11:17 PM
Last Post: Skaperen
  Pausing and returning to function? wallgraffiti 1 2,157 Apr-29-2021, 05:30 PM
Last Post: bowlofred
  Why is the function returning None for a * b instead of number? omm 10 4,289 Nov-05-2020, 01:17 PM
Last Post: omm
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,479 Aug-15-2020, 08:50 PM
Last Post: deanhystad
  Nested Recursive Function not Returning Etotheitau 2 2,255 May-09-2020, 06:09 PM
Last Post: Etotheitau
  Function not returning correct value ActualNoob 3 2,686 Jan-11-2019, 12:35 AM
Last Post: stullis
  Function not returning expected value Euqinu 4 3,483 Sep-10-2018, 12:48 PM
Last Post: Euqinu
  Recursive function not returning expected output...(Python speech recog module) bigmit37 4 5,867 Jan-10-2017, 02:13 PM
Last Post: bigmit37
  function returning None tkj80 3 5,267 Oct-06-2016, 02:08 AM
Last Post: tkj80

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020