May-24-2020, 10:37 PM
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()