Created
March 22, 2023 18:51
-
-
Save EteimZ/f6b8c5411bc8fdaa9ca3c138d30f94cf to your computer and use it in GitHub Desktop.
Creating trackbar with opencv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import cv2 as cv | |
| def nothing(x): | |
| pass | |
| # Create a black image, a window | |
| img = np.zeros((300, 512, 3), np.uint8) | |
| cv.namedWindow('image') | |
| # create trackbars for color change | |
| cv.createTrackbar('R', 'image',0,255, nothing) | |
| cv.createTrackbar('G', 'image',0,255, nothing) | |
| cv.createTrackbar('B', 'image',0,255, nothing) | |
| # create switch for ON/OFF functionality | |
| switch = '0 :OFF \n1 :ON' | |
| cv.createTrackbar(switch, 'image', 0, 1, nothing) | |
| while 1: | |
| cv.imshow('image', img) | |
| k = cv.waitKey(1) & 0xFF | |
| if k == 27: | |
| break | |
| # get current positions of four trackbars | |
| r = cv.getTrackbarPos('R', 'image') | |
| g = cv.getTrackbarPos('G', 'image') | |
| b = cv.getTrackbarPos('B', 'image') | |
| s = cv.getTrackbarPos(switch, 'image') | |
| if s == 0: | |
| img[:] = 0 | |
| else: | |
| img[:] = [b, g, r] | |
| cv.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment