一.重点
createTrackbar(trackBarName,winName,value,count)依次为(轨道线,窗口名,轨道线值,count:最小0,最大count)
getTrackBarPos(trackBarName,winName)获得当前值的api,输出trackBar滑块当前的位置
二.流程
创建trackBar
cv.createTrackbar('R','trackBar',0,255,callback)
cv.createTrackbar('G','trackBar',0,255,callback)
cv.createTrackbar('B','trackBar',0,255,callback)\
创建完后需要获取trackBar的值即:
r = cv.getTrackBarPos('R','trackBar')
g = cv.getTrackbarPos('G','trackBar')
b = cv.getTrackbarPos('B','trackBar')
然后对图片指定像素进行赋值:
img[:] 选中所有像素点
img[:] = [b,g,r] 注意赋值颜色是相反的BGR
参考代码:
import cv2 as cv
import numpy as np
# 空回调函数
def callback():
pass
# 创建窗口
cv.namedWindow('trackBar',cv.WINDOW_NORMAL)
cv.resizeWindow('trackBar',(640,360))
# 创建trackBar
cv.createTrackbar('R','trackBar',0,255,callback)
cv.createTrackbar('G','trackBar',0,255,callback)
cv.createTrackbar('B','trackBar',0,255,callback)
# 创建图片
img = np.zeros((480,640,3), dtype=np.uint8)
while True:
cv.imshow('trackBar', img)
# 获取trackBar
r = cv.getTrackbarPos('R','trackBar')
g = cv.getTrackbarPos('G','trackBar')
b = cv.getTrackbarPos('B','trackBar')
# img[:] 选中所有的像素
img[:] = [b,g,r]
key = cv.waitKey(1)
if key & 0xFF == ord('q'):
break
#释放资源
cv.destroyAllWindows()
运行效果: