opencv-python滑块的使用
创建三个滑块,分别调节R、G、B的值,调节滑块可以获得对应颜色的变化,按ESC键退出。
用到的函数有
createTrackbar(trackbarname,winname,value, count,TrackbarCallback);
创建滑块函数,参数依次为(滑块名、窗口名、初始值、最终值,返回函数)
getTrackbarPos(trackbarname,winname)
获取滑块值
代码如下
import cv2
import numpy as np
cv2.namedWindow('trackbar',cv2.WINDOW_NORMAL)
cv2.resizeWindow('trackbar',400,400)
def callback(value):
print(value)
cv2.createTrackbar('R','trackbar',0,255,callback)
cv2.createTrackbar('G','trackbar',0,255,callback)
cv2.createTrackbar('B','trackbar',0,255,callback)
img=np.zeros((480,640,3),np.uint8)
while True:
r=cv2.getTrackbarPos('R','trackbar')
g=cv2.getTrackbarPos('G','trackbar')
b=cv2.getTrackbarPos('B','trackbar')
img[:]=[b,g,r]
cv2.imshow('trackbar',img)
key=cv2.waitKey(1)
if key==27:
break
cv2.destroyAllWindows()