Skip to content

Instantly share code, notes, and snippets.

@rvalue
Created July 7, 2022 14:35
Show Gist options
  • Save rvalue/061f48d45720bf8cda4c148122a900ba to your computer and use it in GitHub Desktop.
Save rvalue/061f48d45720bf8cda4c148122a900ba to your computer and use it in GitHub Desktop.
OpenCV Trackbar
import numpy as np
import cv2
def update(image, args):
print("Scale: " + str(args))
fontFace = cv2.FONT_HERSHEY_SIMPLEX
pixelHeight = int(image.shape[0] * args / 100)
thickness = 1
org = (0, pixelHeight)
lineType = cv2.LINE_AA
fontScale = cv2.getFontScaleFromHeight(fontFace, pixelHeight, thickness)
color = (255, 255, 255)
imgShow = image.copy()
cv2.putText(imgShow, "Hello World", org, fontFace, fontScale, color, thickness, lineType)
cv2.imshow(WINDOW_NAME, imgShow)
def main():
image = np.zeros((500, 500, 3), dtype=np.uint8)
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
cv2.createTrackbar(TRACKBAR_NAME, WINDOW_NAME, 0, 100, lambda args: update(image, args))
update(image, 10)
cv2.setTrackbarPos(TRACKBAR_NAME, WINDOW_NAME, 10)
# Press any key to quit
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
WINDOW_NAME = "Trackbar Example"
TRACKBAR_NAME = "Scale"
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment