【发布时间】:2019-07-25 17:19:08
【问题描述】:
我试图通过点击鼠标事件来绘制两个区域,我考虑过使用两个线程和一个锁,这是我的代码:
import numpy as np
import cv2
from threading import Thread, RLock
CANVAS_SIZE = (600,800)
FINAL_LINE_COLOR = (255, 255, 255)
WORKING_LINE_COLOR = (127, 127, 127)
verrou = RLock()
class ZoneDrawer(Thread):
def __init__(self, window_name):
Thread.__init__(self)
self.window_name = window_name
self.done = False
self.current = (0, 0)
self.points = []
def on_mouse(self, event, x, y, buttons, user_param):
if event == cv2.EVENT_MOUSEMOVE:
self.current = (x, y)
elif event == cv2.EVENT_LBUTTONDOWN:
print("Adding point #%d with position(%d,%d)" % (len(self.points), x, y))
self.points.append((x, y))
elif event == cv2.EVENT_RBUTTONDOWN:
self.done = True
def run(self):
cv2.namedWindow(self.window_name)
cv2.imshow(self.window_name, np.zeros(CANVAS_SIZE, np.uint8))
cv2.waitKey(1)
cv2.setMouseCallback(self.window_name, self.on_mouse)
while(not self.done):
canvas = np.zeros(CANVAS_SIZE, np.uint8)
with verrou:
if (len(self.points) > 0):
cv2.polylines(canvas, np.array([self.points]), True, FINAL_LINE_COLOR, 1)
cv2.line(canvas, self.points[-1], self.current, WORKING_LINE_COLOR)
cv2.imshow(self.window_name, canvas)
if cv2.waitKey(50) == 27:
self.done = True
cv2.waitKey()
cv2.destroyWindow(self.window_name)
thread_1 = ZoneDrawer("zone1")
thread_2 = ZoneDrawer("zone2")
thread_1.start()
thread_2.start()
thread_1.join()
thread_2.join()
但此代码仍然无法正常工作。有什么帮助或建议吗?
【问题讨论】:
-
欢迎来到 SO! 这段代码仍然无法正常工作是什么意思?您能否详细说明问题/错误并更具体?
-
我只能画一个区域,但我需要画两个区域
标签: python python-3.x multithreading opencv