-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
48 lines (43 loc) · 1.5 KB
/
test.py
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
# import the necessary packages
import argparse
import cv2
import numpy as np
# Create a 600x600 px grayscale canvas
canvas = np.ones((600, 600), dtype="uint8") * 255
# Designate a 400x400 px point of interest and set to black
canvas[100:500, 100:500] = 0
# initialize the list of reference points and boolean indicating
# whether cropping is being performed or not
refPt = []
cropping = False
def click_and_crop(event, x, y, flags, param):
# grab references to the global variables
global refPt, cropping
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
refPt.append((x, y))
cropping = False
# draw a rectangle around the region of interest
cv2.rectangle(canvas, refPt[0], refPt[1], (0, 255, 0), 2)
cv2.imshow("image", canvas)
# Create named cv2 window
cv2.namedWindow("Test Canvas")
# Register mouse hook callback from on_mouse_events() function
cv2.setMouseCallback("Test Canvas", click_and_crop)
# key presses and their interpretations
while(True):
cv2.imshow("Canvas", canvas)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'): # q to close window & end script
break
elif key == ord('c'):
canvas[100:500, 100:500] = 0
cv2.destroyAllWindows()