码迷,mamicode.com
首页 > 编程语言 > 详细

初识OpenCV-Python - 004: Trackbar as the color palette

时间:2019-10-20 01:21:09      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:info   palette   style   numpy   wait   windows   ==   nal   UNC   

此次学习了如何用OpenCV建立一个色调盘。其中会用到cv2.getTrackbarPos(), cv2.createTrackbar()函数。

code:

import cv2
import numpy as np

def nothing(x):
pass

#Create a black image, a window
img = np.zeros((300,512,3),np.uint8)
cv2.namedWindow(‘image‘)

#create trackbars for color change
cv2.createTrackbar(‘R‘,‘image‘,0,255,nothing) #建立一个调节红色的横条
cv2.createTrackbar(‘G‘,‘image‘,0,255,nothing)
cv2.createTrackbar(‘B‘,‘image‘,0,255,nothing)

#create switch for ON/OFF functionality
switch = ‘0 : OFF \n1 : ON‘
cv2.createTrackbar(switch, ‘image‘,0,1,nothing)

while(1):
cv2.imshow(‘image‘,img)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break

#get current positions of four trackbars
r = cv2.getTrackbarPos(‘R‘,‘image‘) #记录当前红色条的数据
g = cv2.getTrackbarPos(‘G‘, ‘image‘)
b = cv2.getTrackbarPos(‘B‘, ‘image‘)
s = cv2.getTrackbarPos(switch, ‘image‘)

if s == 0:
img[:] = 0
else:
img[:] = [b,g,r]

cv2.destroyAllWindows()

Code结果展示:

技术图片

 

 

 

初识OpenCV-Python - 004: Trackbar as the color palette

标签:info   palette   style   numpy   wait   windows   ==   nal   UNC   

原文地址:https://www.cnblogs.com/August2019/p/11706419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!