The idea is to click-and-drag a bounding box around a region of interest to obtain the coordinates. To do this, we must capture the event actions of a mouse click and record the starting and ending coordinates of the ROI. OpenCV allows us to do this by processing mouse click events. Anytime a mouse click event is triggered, OpenCV will relay the information to our extract_coordinates
callback function. In order to handle the event, OpenCV requires various arguments:
- event: Event that took place (left/right pressed or released mouse click)
- x: The x-coordinate of event
- y: The y-coordinate of event
- flags: Relevant flags passed by OpenCV
- Parameters: Extra parameters passed by OpenCV
A pressed left click records the top left coordinates while a released left click records the bottom right coordinates. We then draw a bounding box around the ROI and print the coordinates of the top left and bottom right rectangular region to the console. A right click will reset the image.
Extract bounding box coordinates widget:
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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…