Ask Your Question

Getting position of a bounding box

asked 2018-08-09 04:44:57 -0500

Raki gravatar image

updated 2018-08-09 05:56:26 -0500


I am conducting template matching over an image to get the bounding boxes, and it works.

   # Apply template Matching
    # loop over the list of templates and draw bounding boxes around them in the image
    for i in range(len(templates)):
        w, h = templates[i].shape[::-1]
        res = cv2.matchTemplate(backgroundImage_Gray, templates[i], cv2.TM_SQDIFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        top_left = min_loc
        bottom_right = (top_left[0] + w, top_left[1] + h)
        cv2.rectangle(backgroundImage, top_left, bottom_right, 255, 2)
        # get the pixels of the rectangle in a variable that can hold them all conveniently

I'd like to keep the location of each bounding box in a list though, so that I can, later on, access them. Does anyone know how to keep the position in a suitable container?

I'd appreciate any suggestion.

edit retag flag offensive close merge delete


How about using the bounding box to extract a submatrix (will return a normal Mat instance) and keep it for later?

holger gravatar imageholger ( 2018-08-09 04:56:40 -0500 )edit

what do you need it for ?

berak gravatar imageberak ( 2018-08-09 05:10:45 -0500 )edit

@berak I am making a game in which a space shuttle moves in the solar system and once enters a planet (its bounding box) something shall happen. So, I need to know if the shuttle crosses the box or not.

Raki gravatar imageRaki ( 2018-08-09 05:12:13 -0500 )edit

@holger well, I need the XY coordinates since I am looking for boundaries. What you suggested may work though, depending on implementation.

Raki gravatar imageRaki ( 2018-08-09 05:13:14 -0500 )edit

Well so you want to get the bounding box before getting the pixels of the bounding box?

holger gravatar imageholger ( 2018-08-09 05:51:52 -0500 )edit

@Raki, but then you need the position of the bounding box, not the pixels , right ?

berak gravatar imageberak ( 2018-08-09 05:54:16 -0500 )edit

@berak Yes, I want to know where the bounding box is, so that If the shuttle crosses I can trigger something. @holger Sorry for the confusion, position of the box is what I wanted to mean.

Raki gravatar imageRaki ( 2018-08-09 05:55:54 -0500 )edit

Ok - so your question is - how to get the bounding box using template matching? The code you are posting is doing this? Is it not working?

holger gravatar imageholger ( 2018-08-09 05:57:49 -0500 )edit

Template matching does not provide me the boundaries' locations. It simply draws a rectangle using one point. This is not boundary.

Raki gravatar imageRaki ( 2018-08-09 06:02:41 -0500 )edit

Sorry i dont get it - if you are able to draw a rectangle around the object you want to detect - you have the bounding box. The rectangle IS the bounding box. Is has top left and bottom right points.

holger gravatar imageholger ( 2018-08-09 06:10:19 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-08-09 06:45:37 -0500

berak gravatar image


def ship_hits_rect(ship, tl, br):
    return ship[0] <= br[0] and ship[0] >= tl[0] and ship[1] <= br[1] and ship[1] >= tl[1]
edit flag offensive delete link more


Yes you are checking if ship is in the bounding box of the matched template. I think the author has a different understanding of a bounding box than me - Anyway - correct answer :-)

holger gravatar imageholger ( 2018-08-09 07:41:00 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-08-09 04:44:57 -0500

Seen: 167 times

Last updated: Aug 09 '18

Related questions