1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
import cv2 import numpy as np
curshape = 0 startpos = (0, 0)
img = np.zeros((480, 640, 3), np.uint8)
def mouse_callback(event, x, y, flags, userdata): global curshape, startpos
if (event & cv2.EVENT_LBUTTONDOWN == cv2.EVENT_LBUTTONDOWN): startpos = (x, y) elif (event & cv2.EVENT_LBUTTONUP == cv2.EVENT_LBUTTONUP): if curshape == 0: cv2.line(img, startpos, (x,y), (0,0,255)) elif curshape == 1: cv2.rectangle(img, startpos, (x,y), (0,0,255)) elif curshape == 2: a = (x - startpos[0]) b = (y - startpos[1]) r = int((a**2+b**2)**0.5) cv2.circle(img, startpos, r, (0,0,255))
cv2.namedWindow('drawShape', cv2.WINDOW_NORMAL) cv2.resizeWindow('drawShape', 640, 360)
cv2.setMouseCallback('drawShape', mouse_callback, "123")
while True: cv2.imshow('drawShape', img) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break elif key == ord('l'): curshape = 0 elif key == ord('r'): curshape = 1 elif key == ord('c'): curshape = 2
cv2.destroyAllWindows()
|