OpenCV4【3】- GUI特性:轨迹栏作为调色板

操作步骤:

1. cv2.createTrackbar 创建 rgb 轨迹栏 

2. cv2.getTrackbarPos 获取 轨迹栏位置

3. 给图片着色

 

import numpy as np
import cv2 as cv

def nothing(x):
    pass

# 创建一个黑色的图像,一个窗口
img = np.zeros((300, 512, 3), np.uint8)
cv.namedWindow('image')
# 创建颜色变化的轨迹栏
cv.createTrackbar('R', 'image', 0, 255, nothing)
cv.createTrackbar('G', 'image', 0, 255, nothing)
cv.createTrackbar('B', 'image', 0, 255, nothing)

# 为 ON/OFF 功能创建开关
switch = '0 : OFF \n1 : ON'
cv.createTrackbar(switch, 'image', 0, 1, nothing)

while(1):
    cv.imshow('image', img)
    k = cv.waitKey(1) & 0xFF
    if k == 27:
        break
    # 得到四条轨迹的当前位置
    r = cv.getTrackbarPos('R', 'image')
    g = cv.getTrackbarPos('G', 'image')
    b = cv.getTrackbarPos('B', 'image')
    s = cv.getTrackbarPos(switch, 'image')
    if s == 0:
        img[:] = 0
    else:
        img[:] = [b, g, r]
        # print(img)

cv.destroyAllWindows()

 

 

 

参考资料:

https://blog.csdn.net/qq_18995069/article/details/83050060  【Python+OpenCV入门学习】七、轨迹条操作

https://blog.csdn.net/qq_40755643/article/details/84887729  OpenCV-Python-(7)-Trackbar轨迹栏目

https://www.jb51.net/article/182241.htm  Python Opencv 通过轨迹(跟踪)栏实现更改整张图像的背景颜色

上一篇:云集群本地访问


下一篇:初识Opencv4.X----ROI截取