import cv2
import numpy as np
def nothing(x):
pass
#创建一幅黑色图像
img = np.zeros((400,512,3),np.uint8)
cv2.namedWindow('image')
#'R'为滑动条的名字,'image'为滑动条被放置窗口的名字,0:滑动条的默认位置,255:滑动条的最大位置,nothing是回调函数
cv2.createTrackbar('R','image',0,255,nothing)
cv2.createTrackbar('G','image',0,255,nothing)
cv2.createTrackbar('B','image',0,255,nothing)
switch='0:OFF\n1:ON'
cv2.createTrackbar(switch,'image',0,1,nothing)
while(1):
cv2.imshow('image',img)
k = cv2.waitKey(1)& 0xFF
if k==27:
break
r = cv2.getTrackbarPos('R','image')
g = cv2.getTrackbarPos('G','image')
b = cv2.getTrackbarPos('B','image')
s = cv2.getTrackbarPos(switch,'image')
if s==0: #相当于关按钮
img[:]= [0,0,0]
else: #相当于开按钮
img[:]=[b,g,r]
cv2.destroyAllWindows()
结果如下图: