学习开放简历可以为我解密一段代码吗?

时间:2015-12-10 00:52:23

标签: python python-2.7 opencv

正如我所说,我正在学习使用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与trackbar部分中用于滑块的名称相同,但它们只是字符串,我理解的是元组。我不知道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)

1 个答案:

答案 0 :(得分:1)

out.append(cv2.getTrackbarPos(bar, "Control"))

simply populates an array with the current trackbarvalues --> first the three lower limits for the HSV colorspace, and then the three upper limits

These 6 values are then used for cv2.inRange(src, lowerb, upperb)

tuple(values[:3]) gets the first 3 elements (=lower limits) tuple(values[3:]) gets the last 3 elements (=upper limits)

These are not strings, but int values returned from

cv2.getTrackbarPos(bar, "Control")

The strings were only used to reference the different controls by their name as defined in

cv2.createTrackbar("LowH", "Control", 0, 179, callback)

The inRange returns 255 if all 3 values of hsv are within the limits defined in the 3 element tuples extracted from the values array

cv2.inRange(hsv, tuple(values[:3]), tuple(values[3:]))

[opencv Docs/ Array Operations]