1 | initial version |
You could use findContours() to get the contours of your rectangles. If you click somewhere in your image you get the coordinates of the click. With pointPolygonTest() you now can check if you clicked inside one of the contours.
import cv2
import numpy as np
contours = None
def callback(event, x, y, flags, param):
global contours
if (event == cv2.EVENT_LBUTTONDOWN):
for cnt in contours:
res = cv2.pointPolygonTest(cnt, (x,y), True)
if (res > 0):
x,y,w,h = cv2.boundingRect(cnt)
print (x,y), (x+w-1,y+h-1)
cv2.namedWindow('test')
cv2.setMouseCallback('test', callback)
image = np.zeros((400, 400, 3), np.uint8)
cv2.rectangle(image, (10, 10), (100, 100), (255,255,255), 1)
cv2.rectangle(image, (200, 200), (300, 300), (255,255,255), 1)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cnts, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.imshow('test', image)
if cv2.waitKey() == ord('1'):
cv2.destroyAllWindows()
This might be not the best solution, since you check for every contour, but it will work. Best regards