Python Forum
Returning Value from Function with Trackbars - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Returning Value from Function with Trackbars (/thread-27066.html)



Returning Value from Function with Trackbars - vicpylon - May-24-2020

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)



RE: Returning Value from Function with Trackbars - bowlofred - May-24-2020

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.


RE: Returning Value from Function with Trackbars - vicpylon - May-24-2020

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()



RE: Returning Value from Function with Trackbars - bowlofred - May-24-2020

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)