import cv2 dx0 = 0 dy0 = 0 isdown = False def callback(event, x, y, flags, userdata): global dx0,dy0,isdown color = 0, 255, 0 if flags & cv2.EVENT_FLAG_CTRLKEY: color = 255, 0, 0 elif flags & cv2.EVENT_FLAG_SHIFTKEY: color = 0, 0, 255 elif flags & cv2.EVENT_FLAG_ALTKEY: color = 128, 255, 50 if event == cv2.EVENT_LBUTTONDOWN: dx0 = x dy0 = y isdown = True if event == cv2.EVENT_LBUTTONUP: isdown = False cv2.rectangle(userdata, (dx0, dy0), (x, y), (0, 255, 0), 2) if event == cv2.EVENT_MOUSEMOVE: if isdown: temp = userdata.copy() cv2.rectangle(temp,(dx0,dy0),(x,y),color,2) cv2.imshow("image",temp) image = cv2.imread("x.jpg") cv2.namedWindow("image", cv2.WINDOW_FREERATIO) cv2.setMouseCallback("image", callback,image) cv2.imshow("image", image) while True: k = cv2.waitKey() if k == ord('q'): break cv2.destroyAllWindows()
这样的图片标注