图像中鼠标单击位置的Python OpenCV线程事件

图像中鼠标单击位置的Python OpenCV线程事件,python,opencv,python-multithreading,Python,Opencv,Python Multithreading,我正在努力与我的代码,使其工作! 图形正在运行,只有螺纹未运行 我想检查图像中的一些点,并将其存储在全局变量points中。因此,每次鼠标单击都会触发一个事件集,以继续选择下一个点 当我第一次调用线程时,线程启动,我可以 在图像中创建一些圆圈,但不是我想要的那样。 程序在线程中卡住了,我只能用esc键停止它 即使是第一条打印消息也不会弹出 我非常感谢你的回答!! 程序运行时不使用e.wait()。所以我试着检查我想要的所有点,然后按esc键返回到另一个函数,但这不是线程应该工作的方式?如果可能的

我正在努力与我的代码,使其工作! 图形正在运行,只有螺纹未运行

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

当我第一次调用线程时,线程启动,我可以 在图像中创建一些圆圈,但不是我想要的那样。 程序在线程中卡住了,我只能用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]