OpenCV的鼠标事件
import numpy as np
import cv2
def drawfunction(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img, (x, y), 20, (255, 255, 255), -1)
img = cv2.imread("cat.jpg")
cv2.namedWindow("image")
cv2.setMouseCallback("image", drawfunction)
while(1):
cv2.imshow("image", img)
key = cv2.waitKey(1)
if key == 27:
break
cv2.destroyAllWindows()
import numpy as np
import cv2
drawing = True
shape = 'r'
def draw_circle(event, x, y, flags, param):
global x1, x2
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
x1, x2 = x, y
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if shape == 'r':
cv2.rectangle(img, (x1, x2), (x, y), (0, 255, 0), -1)
if shape == 'l':
cv2.line(img, (x1, x2), (x, y), (255, 255, 255), 3)
if shape == 'c':
cv2.circle(img, (x, y), 10, (255, 255, 0), -1)
img = cv2.imread("cat.jpg")
cv2.namedWindow("image")
cv2.setMouseCallback("image", draw_circle)
while(1):
cv2.imshow("image", img)
key = cv2.waitKey(1)
if key == ord("1"):
shape = 'r'
if key == ord("2"):
shape = 'l'
if key == ord("3"):
shape = 'c'
if key == 27:
break
cv2.destroyAllWindows()