opencv使用Trackbar

参考链接: 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()

运行结果截图展示:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值