我试图通过单击鼠标事件绘制两个区域,我想到使用两个线程和一个锁,这是我的代码:
导入numpy为np导入cv2来自线程导入Thread,RLock…
以下脚本可用于使用鼠标选择图像的区域(如.gif中所示)。
import cv2, numpy as np # Mouse callback function global click_list positions, click_list, shapes = [(0,0)], [], [] def callback(event, x, y, flags, param): positions[-1] = (x,y) if event == 1: click_list.append((x,y)) cv2.namedWindow('img') cv2.setMouseCallback('img', callback) # Mainloop - show the image and collect the data while True: # Create a blank image img = np.zeros((600,600,3), np.uint8) # Try to draw the shape being collected for idx in range(len(click_list)-1): cv2.line(img, click_list[idx], click_list[idx+1], (0,255,0), 5) # Draw the stored shapes for shape in shapes: for idx in range(len(shape)): cv2.line(img, shape[idx], shape[idx-1], 255, 5) # Show the image cv2.imshow('img', img) # Wait, and allow the user to quit with the 'esc' key k = cv2.waitKey(1) # If user presses 's', go on to the next shape if k == 115: shapes.append(click_list) click_list = [] # If user presses 'esc' break if k == 27: break # Clean up cv2.destroyAllWindows()