Ask Your Question
0

Obtaining rect dimension when clicked anywhere in the box ?

asked 2016-06-22 01:49:14 -0600

Can someone please give an idea as to how to get the dimensions of a rectangle when clicked anywhere inside the rectangle box ?

edit retag flag offensive close merge delete

Comments

Please make clear what you want to achieve! The dimensions of a box drawn on an image or what?

Missing gravatar imageMissing ( 2016-06-22 02:42:25 -0600 )edit

I have an image in which I have many bounding boxes. I want that I should get the dimensions of a specific box provided I click anywhere inside that box.

Ashutosh Mishra gravatar imageAshutosh Mishra ( 2016-06-22 03:44:41 -0600 )edit
1

You should think your problem differently. Try to use findcontour and connectedcomponenent. When you will click inside a box you find label and hence box (if it's a box)

LBerger gravatar imageLBerger ( 2016-06-22 04:39:55 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-06-22 04:49:06 -0600

Missing gravatar image

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

edit flag offensive delete link more

Comments

If I have rectangles already present in my image, will this code return the dimension of that box if I click inside that box ?

Ashutosh Mishra gravatar imageAshutosh Mishra ( 2016-06-22 07:19:01 -0600 )edit
2

I just added the two rectangles for demo purposes. If you have the edges in your images it will also work, provided that your input image to findContours() is in grayscale and your rectangles are represented white on black background. The code returns the top left and bottom right coordinates of the rectangle you clicked. You easily can compute the dimension of it from hat.

Missing gravatar imageMissing ( 2016-06-22 07:23:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-22 01:49:14 -0600

Seen: 558 times

Last updated: Jun 22 '16