【发布时间】:2015-12-10 00:52:23
【问题描述】:
正如我所说,我正在与我的机器人团队一起学习使用 OpenCV,而我的编程负责人为我们的子小组提供了一些视觉代码以供探索和使用。我知道有一段代码与我正在尝试做的事情相关。 (我正在用调色板和精度调整替换他的 6 个滑块)但我不知道这部分代码在做什么。
def getVars():
bars = ("LowH", "LowS", "LowV", "HighH", "HighS", "HighV")
out = []
for bar in bars:
out.append(cv2.getTrackbarPos(bar, "Control"))
return out
我知道 High 和 Lows 与轨迹栏部分中用于滑块的名称相同,但它们只是我理解的元组中的字符串。我不知道 out.append 行是做什么的。 这是上下文的其余代码,我添加了一些未使用的部分,但尚未实现。
from Tkinter import *
from tkColorChooser import askcolor
import colorsys
import cv2
import numpy
camera = cv2.VideoCapture(0)
lowH = 1
highH = 179
lowS = 1
highS = 255
lowV = 1
highV = 255
def callback(x): pass
def getColor():
color = askcolor()
#Lines above this initiate the color wheel
#Below lines extract the output and convert to HSV
((red, green, blue), hexcode ) = color
rfloat = float(red)
gfloat = float(green)
bfloat = float(blue)
r, g, b = rfloat/255, gfloat/255, bfloat/255
print (red, green, blue), "RGB"
print (r, g, b), "RGB 0-1"
h, s, v = colorsys.rgb_to_hsv(r, g, b)
print (h, s, v), "HSV"
hsv = (h, s, v)
return hsv
def getVars():
bars = ("LowH", "LowS", "LowV", "HighH", "HighS", "HighV")
out = []
for bar in bars:
out.append(cv2.getTrackbarPos(bar, "Control"))
return out
Button(text='Select Color', command=getColor).pack()
print getVars()
cv2.namedWindow("Control")
cv2.createTrackbar("LowH", "Control", 0, 179, callback)
cv2.createTrackbar("HighH", "Control", 0, 179, callback)
cv2.createTrackbar("LowS", "Control", 0, 255, callback)
cv2.createTrackbar("HighS", "Control", 0, 255, callback)
cv2.createTrackbar("LowV", "Control", 0, 255, callback)
cv2.createTrackbar("HighV", "Control", 0, 255, callback)
while True:
values = getVars()
#print(values, values[:3], values[3:])
(cap, frame) = camera.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#threshold = hsv
threshold = cv2.inRange(hsv, tuple(values[:3]), tuple(values[3:]))
#contours,hierarchy = cv2.findContours(threshold, 1, 2)
#cnt = contours[0]
#M = cv2.moments(cnt)
#cx = int(M['m10']/M['m00'] if M["m00"] != 0 else 0)
#cy = int(M['m01']/M['m00'] if M["m00"] != 0 else 0)
#cv2.circle(threshold, (cx,cy), 3, (0,250,0), -1)
cv2.imshow("Output", threshold)
cv2.imshow("Original", frame)
cv2.waitKey(1)
【问题讨论】:
标签: python python-2.7 opencv