
For opencv, it is very simple to create a trackbar control. You only need to call an api, createTrackbar.
In addition, the getTrackbarPos API is also very important, specifically the API for obtaining the current value of the trackbar.
Parameters of createTrackbar:
- Trackbarname, winname: the name of the trackbar and the window name in which it is displayed
- Value: trackbar: current value, which can be set to the default value of 0
- Count: the minimum value is 0, and the maximum value is count.
- Callback, userdata: default values of callback functions and callback functions
- Trackbarname: input parameters, specifically the name of the trackbar
- Winname: Input parameters, specifically the name of the window where the trackbar is located
使用trackbar滑动实现改变窗帘背景色案例 import cv2 import numpy as np def callback(): pass # 创建窗口 cv2.namedWindow('trackbar',cv2.WINDOW_NORMAL) cv2.resizeWindow('trackbar',640,360) # 创建trackbar cv2.createTrackbar('R','trackbar',0,255,callback) cv2.createTrackbar('G','trackbar',0,255,callback) cv2.createTrackbar('B','trackbar',0,255,callback) # 创建一个背景图片 img=np.zeros((360,640,3),np.uint8) while 1: # 获取当前trackbar的值 r=cv2.getTrackbarPos('R','trackbar') g=cv2.getTrackbarPos('G','trackbar') b=cv2.getTrackbarPos('B','trackbar') # 改变背景图片颜色 img[:]=[b,g,r] cv2.imshow('trackbar',img) # 按q键退出 key=cv2.waitKey(10) if key==ord('q'): break cv2.destroyAllWindows()