Created
August 24, 2022 09:17
-
-
Save kwcooper/8eef6ac3692b17365a8d4ee60110d96b to your computer and use it in GitHub Desktop.
A simple interface to return clicked coordinates of an image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
class PointGrabber: | |
def __init__(self, imgPath, printCords=True): | |
self.points = [] | |
self.img = cv2.imread(imgPath, 1) | |
self.printCords = printCords | |
def select_point(self, event, x, y, flags, param): | |
''' display the coordinates of points clicked on an image ''' | |
if event == cv2.EVENT_LBUTTONDOWN: | |
if self.printCords: print(x, y) | |
self.points.append((x,y)) | |
# displaying the coordinates | |
# on the image window | |
cv2.circle(self.img, (x,y), 5, (255, 0, 0), 3) | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
cv2.putText(self.img, str(x) + ',' + | |
str(y), (x,y), font, | |
1, (255, 0, 0), 2) | |
cv2.imshow('image', self.img) | |
def get_clicked_cords(self): | |
''' A small driver program to return clicked points from an image ''' | |
cv2.imshow('image', self.img) | |
# setting mouse handler for the image | |
cv2.setMouseCallback('image', self.select_point) | |
# wait for any key to be pressed and exit | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
cv2.waitKey(1) | |
return self.points | |
if __name__=="__main__": | |
imgPath = 'image/file/path/here.jpg' | |
PG = PointGrabber(imgPath) | |
points = PG.get_clicked_cords() | |
print(points) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment