-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse_event.py
36 lines (28 loc) · 1.31 KB
/
mouse_event.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
# importing the modules
import cv2
import numpy as np
# function to display the coordinates of the points clicked on the image
class PointImage:
def __init__(self, image_name, num_points=12):
self.num_points = num_points
self.points_2d = np.zeros((num_points, 2)) # store coordinates of mouse click on image
self.counter = 0
self.img = cv2.imread(image_name, 1) # reading the image
cv2.imshow('image', self.img) # displaying the image
# setting mouse handler for the image and calling the click_event() function
cv2.setMouseCallback('image', self.click_event)
cv2.waitKey(0) # wait for a key to be pressed to exit
cv2.destroyAllWindows() # close the window
def click_event(self, event, x, y, flags, params):
# checking for left mouse clicks
if event == cv2.EVENT_LBUTTONDOWN:
if self.counter < self.num_points:
self.points_2d[self.counter] = x, y # store coordinates of image into the matrix
self.counter += 1
if '__main__' == __name__:
print(x, ' ', y) # displaying the coordinates on the Shell
# displaying the coordinates on the image window
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(self.img, str(x) + ',' + str(y), (x, y), font, 0.25, (0, 75, 0), 1)
cv2.imshow('image', self.img)
# test = PointImage('correspondence_left.jpg', 18)