【问题标题】:Python: Input text boxes capturing userinput in parallel to OpenCV Live Webcam imagePython:与 OpenCV Live Webcam 图像并行捕获用户输入的输入文本框
【发布时间】:2014-01-06 21:06:47
【问题描述】:

我有一个运行网络摄像头图像流式传输的简单脚本,我想做一些操作,如 canny-filter 和 hough 变换来检测实时图像中的线条:

import cv2
import math

# Init
cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame1',frame)
    cv2.imshow('frame2',gray)
    cv2.moveWindow('frame1', 200, 10)
    cv2.moveWindow('frame2', 200+660, 10)

    # (3.) 
    edges = cv2.Canny(frame, 40, 100) 
    cv2.imshow('lines',edges)
    cv2.moveWindow('lines', 200, 10+530)

    # hough
    lines = cv2.HoughLinesP(edges, 1, math.pi/2, 20, None, 100, 10)
    edgeimg = gray
    if not lines is None:
        for line in lines[0]:
            pt1 = (line[0],line[1])
            pt2 = (line[2],line[3])
            cv2.line(edgeimg, pt1, pt2, (255,0,0), 3)
        cv2.imshow('hough',edgeimg)
        cv2.moveWindow('hough', 200+660, 10+530)

    # cv2.imshow('img',img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

问题是我需要微调 canny-filter 和 hough-function 的参数,因此我想让一些输入框与我可以输入这些值的 liveimage-windows 并行运行。

我想到了最简单的解决方案来减少编码开销,因此选择了 tkinter。但是我很难并行运行 tkinter-boxes(仅从此处找到此解决方案:How do you run your own code alongside Tkinter's event loop?),当我使用帖子 #1 中的代码并将我的liveimage 到task 函数中。

你能告诉我一个简单的解决方案吗?显示输入框,用户可以在其中输入数字,用作我的代码中 canny/houghlines 的参数?

谢谢

【问题讨论】:

  • 可以用opencv自带的滑块吗?尽管它们是有限的,但它们工作得很好
  • 啊我想知道它们是否在 cv2 中可用,因为我在文档中看不到它们:docs.opencv.org/modules/highgui/doc/… 为什么它们不可用?像import cv2.cv as cv这样导入旧的opencv版本然后通过cv.CreateTrackbar(..)命令使用它的方法是什么??
  • 哦,是的,谢谢,它就像一个魅力!精彩的。您能否向我解释一下为什么 opencv 中的某些命令只能通过 old cv 访问而尚未移植到 cv2?我只是想知道它..

标签: python opencv input hough-transform inputbox


【解决方案1】:

我相信它们在 cv2 命名空间中。我认为我的版本是 2.4.5

import cv2
import numpy as np

def onTrackbarChange(trackbarValue):
    pass

cv2.namedWindow('ctrl') 
cv2.createTrackbar( 'thresh', 'ctrl', 128, 255, onTrackbarChange )  
c = cv2.VideoCapture(0)
while(1):
    im = c.read()[1]
    thresh=cv2.threshold(cv2.cvtColor(im,cv2.COLOR_BGR2GRAY), 
        cv2.getTrackbarPos('thresh','ctrl'),
        255,cv2.THRESH_BINARY)[1]   
    cv2.imshow('thresh',thresh)
    if cv2.waitKey(1)==27:
        exit(0)

【讨论】:

  • 奇怪,但文档中没有这样描述,还是我弄错了什么?但仍然感谢,工作正常:-)
  • 一般情况下我会在print dir(cv2) 那里找到我想要的东西......像help(cv2.threshold) 这样的东西,我很少需要参考文档
  • 啊,我明白了,谢谢你的提示,总是热衷于学习新的东西来优化我的工作流程:-)
  • 有什么方法可以标记轨迹栏?说 0 到 255 左右?
  • 如果您的意思是自动显示轨迹栏的范围,我认为您必须将其编码为标签文本,除了左右滑动并查看值之外,没有默认方法可以做到这一点
猜你喜欢
  • 2022-08-15
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
相关资源
最近更新 更多