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
#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


Messages In This Thread
RE: Returning Value from Function with Trackbars - by vicpylon - May-24-2020, 10:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why my function is returning None? PauloDAS 6 2,030 Jul-17-2022, 11:17 PM
Last Post: Skaperen
  Pausing and returning to function? wallgraffiti 1 2,252 Apr-29-2021, 05:30 PM
Last Post: bowlofred
  Why is the function returning None for a * b instead of number? omm 10 4,602 Nov-05-2020, 01:17 PM
Last Post: omm
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,654 Aug-15-2020, 08:50 PM
Last Post: deanhystad
  Nested Recursive Function not Returning Etotheitau 2 2,442 May-09-2020, 06:09 PM
Last Post: Etotheitau
  Function not returning correct value ActualNoob 3 2,841 Jan-11-2019, 12:35 AM
Last Post: stullis
  Function not returning expected value Euqinu 4 3,678 Sep-10-2018, 12:48 PM
Last Post: Euqinu
  Recursive function not returning expected output...(Python speech recog module) bigmit37 4 6,107 Jan-10-2017, 02:13 PM
Last Post: bigmit37
  function returning None tkj80 3 5,499 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