创建跟踪条与指定窗口绑定
常用函数:
createTrackbar()创建滑动条且与窗口绑定
cv.createTrackbar(trackbarname,winname,value,count,onChange,userdata)
trackbarname:跟踪条名称
winname:绑定窗口名称
value:初始滑块位置
count:滑动最大值
onChange:回调函数
userdata:数据,可选
getTrackbarPos() 获取滑动条数据
cv.getTrackbarPos(trackbarname, winname)
trackbarname:滑动条名称
winname:窗口名称
返回值:当前滑动条的位置
综合示例:
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) 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') img[:] = [b, g, r] cv.destroyAllWindows()