用于Image中MouseClick位置的Python OpenCV线程事件

时间:2015-02-16 03:10:54

标签: python opencv python-multithreading

我正在努力使用我的代码来实现它! 图纸正在工作,只有线程没有。

我想从图像中检查一些点并将其存储在全局变量点中。因此,每次单击鼠标都应触发事件集以继续选择下一个点。

当我第一次调用线程时,线程启动,我可以 在图像中创建一些圆圈,但不是我希望它工作。 程序卡在线程中,我只能用esc键停止它。

即使是第一个打印消息也没有弹出。

我真的很感激你的答案!! 该程序在没有e.wait()的情况下运行。所以我尝试检查我想要的所有点,然后使用esc键返回到另一个函数,但这不是线程应该工作的方式吗?如果可能的话,我希望它按照描述运行:)

非常感谢! 汉纳斯

import cv2
import cv
import time
import numpy as np
from threading import Thread
from threading import Event
import sys

points = []
e = Event()
key = Event()
winName3 = "hsv image colors?"
imCalRGB = np.zeros((512,512,3), np.uint8)
calibrationComplete = False


def calibrate():

    cam = cv2.VideoCapture(0)

    global imCalRGB
    imCalRGB = np.zeros((512,512,3), np.uint8)

    global calibrationComplete
    global e
    global key
    e = Event()
    key = Event()
    calibrationComplete = False

    while calibrationComplete == False:

        cv2.namedWindow(winName3, cv2.CV_WINDOW_AUTOSIZE)
        cv2.imshow(winName3, imCalRGB)
        t = Thread(CalibrationWindowThread(imCalRGB), "Get Points")
        t.start()
        time.sleep(4)
        print "Please select the center of the 20 points outermost rim."
        e.wait()
        e.clear()

        cv2.circle(imCalRGB, points[0], 3,(255, 0, 0),2, 8)
        cv2.imshow(winName3, imCalRGB)

        print "Please select the center of the 3 points outermost rim."
        e.wait()
        e.clear()

        cv2.circle(imCalRGB, points[1], 3,(255, 0, 0),2, 8)
        cv2.imshow(winName3, imCalRGB)

        print "Please select the center of the 11 points outermost rim."
        e.wait()
        e.clear()

        cv2.circle(imCalRGB, points[2], 3,(255, 0, 0),2, 8)
        cv2.imshow(winName3, imCalRGB)

        print "Please select the center of the 6 points outermost rim."
        e.wait()
        e.clear()

        cv2.circle(imCalRGB, points[3], 3,(255, 0, 0),2, 8)
        cv2.imshow(winName3, imCalRGB)

        height, width = imCalRGB.shape[:2]


        ...

def CalibrationWindowThread(im):

    cv2.imshow(winName3, im)
    cv2.setMouseCallback(winName3, on_mouse)

    global key

    while True:
        if not key.is_set():
            test = cv2.waitKey(1)
            if test == 27:
                #cv2.destroyWindow(winName3)
                break
        else:
            break


def on_mouse(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        #events
        global points
        global e

        #append user clicked points
        points.append((x, y))
        e.set()
        print points
        cv2.circle(imCalRGB, (x, y), 3,(255, 0, 0),2, 8)
        cv2.imshow(winName3, imCalRGB)
        #key.set()

if __name__ == '__main__':
    print "Welcome to darts!"
    calibrate()][1]

0 个答案:

没有答案