Getting position of a bounding box
Hi,
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.
How about using the bounding box to extract a submatrix (will return a normal Mat instance) and keep it for later?
what do you need it for ?
@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.
@holger well, I need the XY coordinates since I am looking for boundaries. What you suggested may work though, depending on implementation.
Well so you want to get the bounding box before getting the pixels of the bounding box?
@Raki, but then you need the position of the bounding box, not the pixels , right ?
@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.
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?
Template matching does not provide me the boundaries' locations. It simply draws a rectangle using one point. This is not boundary.
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.