zhiqingstudy

Be a young person with knowledge and content

Trackbar controls are often used in experiments. What exactly is a trackbar like? The following figure shows the style of trackbar.
How to use the trackbar control in Opencv
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:
  1. Trackbarname, winname: the name of the trackbar and the window name in which it is displayed
  2. Value: trackbar: current value, which can be set to the default value of 0
  3. Count: the minimum value is 0, and the maximum value is count.
  4. Callback, userdata: default values of callback functions and callback functions
Parameters of getTrackbarPos:
  1. Trackbarname: input parameters, specifically the name of the trackbar
  2. 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()
comment
head sculpture
Code:
Related

Why you shouldn't stay at a job for more than 2 years?

3 harsh facts long-distance relationships

how to keep your girlfriend interested in a long-distance relationship




Unless otherwise specified, all content on this website is original. If the reprinted content infringes on your rights, please contact the administrator to delete it
Contact Email:2380712278@qq.com

Filing number:皖ICP备19012824号