Skip to content

Instantly share code, notes, and snippets.

@previtus
Created March 3, 2021 11:33
Show Gist options
  • Save previtus/4c933ffcad8b44e398e40997fb2f7d0b to your computer and use it in GitHub Desktop.
Save previtus/4c933ffcad8b44e398e40997fb2f7d0b to your computer and use it in GitHub Desktop.
# https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_trackbar/py_trackbar.html#trackbar
import cv2
import numpy as np
class GUIHandler(object):
def onChangeSend(self,x):
self.A = cv2.getTrackbarPos('A', self.window_name)
self.B = cv2.getTrackbarPos('B', self.window_name)
self.update_text()
print("Data=", [self.A, self.B])
def update_text(self):
self.text = ""+str(self.A)+" "+str(self.B)
def __init__(self):
self.text = ""
def start_window_rendering(self):
# Create a black image, a window
h = 75
img = np.zeros((h, 512, 3), np.uint8)
font = cv2.FONT_HERSHEY_SIMPLEX
position = (0, h - 25)
fontScale = 0.8
fontColor = (128, 128, 128)
lineType = 2
self.window_name = 'InteractiveWindow'
cv2.namedWindow(self.window_name)
# create trackbars for color change
cv2.createTrackbar('A', self.window_name, 0, 1000, self.onChangeSend)
cv2.createTrackbar('B', self.window_name, 0, 100, self.onChangeSend)
self.onChangeSend(x=None) # toggle once at start
while (1):
# also keep another inf. loop
cv2.imshow(self.window_name, img)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
# get current positions of four trackbars
r = cv2.getTrackbarPos('A', self.window_name)
r = int(r)
img[:] = [r, r, r]
#text = 'Select value: (0 to 1000 => %)'
text = self.text
cv2.putText(img, text, position, font, fontScale, fontColor, lineType)
cv2.destroyAllWindows()
from threading import Thread
gui = GUIHandler()
thread = Thread(target=gui.start_window_rendering())
thread.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment