Frage

I have a simply script running streaming the image of my webcam and I want to do some operations like canny-filter and hough transformation to detect lines in the liveimage:

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()

The problem is that I need to fine tune the parameters of the canny-filter and the hough-function and thus I'd like to make some input boxes run in parallel to the liveimage-windows where I can input those values.

I thought of the simplest solution possible to reduce the coding overhead and thus choosing tkinter. But I'm having a hard time running the tkinter-boxes in parallel (only found this solution from here: How do you run your own code alongside Tkinter's event loop?) which makes the display of the live image very very slow when I use the code from post #1 and put my liveimage into the task function.

Can you advice me a simple solution displaying input boxes where the user can input the numbers being used as parameters for canny/houghlines in my code?

Thanks

War es hilfreich?

Lösung

i believe they are in cv2 namespace. my version is 2.4.5 i think

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)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top