Skip to content

Instantly share code, notes, and snippets.

@jjone36
Last active March 10, 2019 01:16
Show Gist options
  • Save jjone36/43a8b93e5f62da8fd8dfd82bb103592e to your computer and use it in GitHub Desktop.
Save jjone36/43a8b93e5f62da8fd8dfd82bb103592e to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
# Step 1. Define callback function
drawing = False
ix = -1
iy = -1
def draw_rectangle(event, x, y, flags, params):
global ix, iy, drawing
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix, iy = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
cv2.rectangle(img, pt1 = (ix, iy), pt2 = (x, y),
color = (87, 184, 237), thickness = -1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
cv2.rectangle(img, pt1 = (ix, iy), pt2 = (x, y),
color = (87, 184, 237), thickness = -1)
# Step 2. Call the window
img = cv2.imread('map.png')
cv2.namedWindow(winname = 'my_drawing')
cv2.setMouseCallback('my_drawing', draw_rectangle)
# Step 3. Execution
while True:
cv2.imshow('my_drawing', img)
if cv2.waitKey(10) & 0xFF == 27:
break
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment