参考链接: Trackbar as the Color Palette
代码展示:
import numpy as np
import cv2 as cv
def nothing(x): # 回调函数,x是当前trackbar上光标的取值
# print(x)
pass # 什么都不做
# Create a black image, a window
img = np.zeros((500,1000,3), np.uint8)
cv.namedWindow('image4LinMaZi')
# create trackbars for color change
cv.createTrackbar('R_red','image4LinMaZi',0,255,nothing) # 添加trackbar,名字,默认值,最大值,回调函数
cv.createTrackbar('G_green','image4LinMaZi',0,255,nothing)
cv.createTrackbar('B_blue','image4LinMaZi',0,255,nothing)
# create switch for ON/OFF functionality
switch = '0:OFF\n1:ON'
cv.createTrackbar(switch, 'image4LinMaZi',0,1,nothing)
while True:
cv.imshow('image4LinMaZi',img)
k = cv.waitKey(1) & 0xFF
if k == 27:
break
# get current positions of four trackbars
r = cv.getTrackbarPos('R_red','image4LinMaZi')
g = cv.getTrackbarPos('G_green','image4LinMaZi')
b = cv.getTrackbarPos('B_blue','image4LinMaZi')
s = cv.getTrackbarPos(switch,'image4LinMaZi')
if s == 0:
img[:] = 0
else:
img[:] = [b,g,r]
cv.destroyAllWindows()
运行结果截图展示: