【问题标题】:learning open cv can someone decipher a section of code for me?学习 open cv 有人可以帮我破译一段代码吗?
【发布时间】: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


    【解决方案1】:

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

    简单地用当前的 trackbarvalues 填充一个数组 --> 首先是 HSV 颜色空间的三个下限,然后是三个上限

    然后将这 6 个值用于 cv2.inRange(src, lowerb, upperb)

    tuple(values[:3]) 获取前 3 个元素(=下限) tuple(values[3:]) 获取最后 3 个元素(=上限)

    这些不是字符串,而是从

    返回的int值
    cv2.getTrackbarPos(bar, "Control")
    

    字符串仅用于通过其名称来引用不同的控件

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

    如果 hsv 的所有 3 个值都在从值数组中提取的 3 个元素元组中定义的范围内,则 inRange 返回 255

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

    [opencv 文档/数组操作]

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 2015-07-14
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 2023-01-02
      • 1970-01-01
      相关资源
      最近更新 更多