opencv_python学习笔记六
8使用滑动条作调色板
cv2.getTrackbarPos()
cv2.createTrackbar()
函数原型
def getTrackbarPos(trackbarname, #滚动条名字
winname)#放置窗口的名字
def createTrackbar(trackbarName,
windowName,
value,
count,
onChange)
使用三个滚动条设置RGB
示例代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2016/11/14 10:17
# @Author : Retacn
# @Site : 使用滑动条作调色板
# @File : trackbarTest.py
# @Software: PyCharm
import cv2
import numpy as
np
def nothing(x):
pass
#创建图像
img=np.zeros((300,512,3),np.uint8)
cv2.namedWindow("Trackbar")
cv2.createTrackbar("R","Trackbar",0,255,nothing)
cv2.createTrackbar("G","Trackbar",0,255,nothing)
cv2.createTrackbar("B","Trackbar",0,255,nothing)
switch='0:OFF\n1:ON'
cv2.createTrackbar(switch,'Trackbar',0,1,nothing)
while(1):
cv2.imshow('Trackbar',img)
k=cv2.waitKey(1)&0xFF
if
k==27:
break
r=cv2.getTrackbarPos('R','Trackbar')
g=cv2.getTrackbarPos('G','Trackbar')
b=cv2.getTrackbarPos('B','Trackbar')
s=cv2.getTrackbarPos(switch,'Trackbar')
if s==0:
img[:]=0
else:
img[:]=[b,g,r]
cv2.destroyAllWindows()