From 77a8ec445f9843c04f8d7d02f8cddf2f8a78e046 Mon Sep 17 00:00:00 2001 From: mattmcw Date: Wed, 9 Oct 2024 00:37:58 -0400 Subject: [PATCH] Commit example for bounding box selection --- notes/bounding_box.py | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 notes/bounding_box.py diff --git a/notes/bounding_box.py b/notes/bounding_box.py new file mode 100644 index 0000000..d1b0210 --- /dev/null +++ b/notes/bounding_box.py @@ -0,0 +1,45 @@ +import cv2 + +class BoundingBoxWidget(object): + def __init__(self): + self.original_image = cv2.imread('1.jpg') + self.clone = self.original_image.copy() + + cv2.namedWindow('image') + cv2.setMouseCallback('image', self.extract_coordinates) + + # Bounding box reference points + self.image_coordinates = [] + + def extract_coordinates(self, event, x, y, flags, parameters): + # Record starting (x,y) coordinates on left mouse button click + if event == cv2.EVENT_LBUTTONDOWN: + self.image_coordinates = [(x,y)] + + # Record ending (x,y) coordintes on left mouse button release + elif event == cv2.EVENT_LBUTTONUP: + self.image_coordinates.append((x,y)) + print('top left: {}, bottom right: {}'.format(self.image_coordinates[0], self.image_coordinates[1])) + print('x,y,w,h : ({}, {}, {}, {})'.format(self.image_coordinates[0][0], self.image_coordinates[0][1], self.image_coordinates[1][0] - self.image_coordinates[0][0], self.image_coordinates[1][1] - self.image_coordinates[0][1])) + + # Draw rectangle + cv2.rectangle(self.clone, self.image_coordinates[0], self.image_coordinates[1], (36,255,12), 2) + cv2.imshow("image", self.clone) + + # Clear drawing boxes on right mouse button click + elif event == cv2.EVENT_RBUTTONDOWN: + self.clone = self.original_image.copy() + + def show_image(self): + return self.clone + +if __name__ == '__main__': + boundingbox_widget = BoundingBoxWidget() + while True: + cv2.imshow('image', boundingbox_widget.show_image()) + key = cv2.waitKey(1) + + # Close program with keyboard 'q' + if key == ord('q'): + cv2.destroyAllWindows() + exit(1) \ No newline at end of file