how to write rectangle (bounding box) by xmax xmin ymax ymin using opencv
Solution 1
Given x, y, width, and height, it should be trivial to get x_max and y_max.
x_max = x + width
y_max = y + height
It is important to remember the coordinate system for images starts with (0, 0)
in the top left, and (image_width, image_height)
in the bottom right. Therefore:
top_left = (x, y)
bottom_right = (x+w, y+h)
The last thing to remember is that there are some cases were the parameter requested is a point (x, y)
, such as the case in the cv2.rectangle function. However, pixels are accessed as the underlying ndarray structure image[row, column]
Check out this question for more info about opencv coordinate systems.
Solution 2
I guess your problem is the reference system.
In a image the point (0,0) is the top left pixel. Judging from your ymin calculation it seems you're considering y "the upper is the higher" but with an origin in top-left point is exactly the opposite.
Ivan Shelonik
Updated on July 22, 2022Comments
-
Ivan Shelonik almost 2 years
I found that I cant easely write bounding box using 4 points (x, y, w, h) using opencv. Where x, y is top left corner and w=width, h=height.
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),15)
But How is it possible to write bounding box using opencv having only xmax xmin ymax ymin points? I need to check that all is allright in my code and bounding boxes used by x, y, w, h is completely equal to bounding boxes that I have under xmax xmin ymax ymin.
I converted x, y, w, h to xmax xmin ymax ymin using these code
bbox_topleft_corner_x = int(prod_data[0]) bbox_topleft_corner_y = int(prod_data[1]) bbox_w = int(prod_data[2]) bbox_h = int(prod_data[3]) ymax = bbox_topleft_corner_y ymin = bbox_topleft_corner_y - bbox_h xmax = bbox_topleft_corner_x + bbox_w xmin = ymin + bbox_w
But I'm not sure that I did all as I wanted. I wanted to convert x, y, w, h to VOOC2007 annotation xml format and their bounding box format
Thanks for any advice
-
Ivan Shelonik about 6 yearsThanks a lot, now I got it. Coordinate system is so strange for me
-
Muneeb Ahmad Khurram over 2 yearsThanks, this is such an underrated question and answer.