Home > Mobile >  Get bounding box coordinates (xywh) from quad coordinates and normalize them
Get bounding box coordinates (xywh) from quad coordinates and normalize them

Time:11-14

I have photos and coordinates of objects within those photos.

For example:

        {
          "shape_attributes": {
            "name": "polygon",
            "all_points_x": [613, 2007, 1769, 581],
            "all_points_y": [1767, 1674, 2260, 2395]
          },
          "region_attributes": {"field_name": "doc_quad"}
        }

I need to convert them to 4 points:

 x_center y_center width height

to get a bounding box. After that I need to normalize them following this instructions:

Box coordinates must be in normalized xywh format (from 0 - 1). If your boxes are in pixels, divide x_center and width by image width, and y_center and height by image height.

How to convert 8 pointed polygon coordinates into normalized form (with 4 points)?

This question might require some geometry knowledge and you can send a link for a related article, If you find this question too easy. The thing is I don't know how to ask Google a question to find an answer, because How to convert 8 point polygon coordinates into 4 point rectangle coordinates didn't give me results I want (majority of articles I found assume that I don't know actual coordinates and use some detection functions from opencv, but I do know actual coordinates) that's why I'm asking here.

CodePudding user response:

Since the polygon is not rectangular, one way is to take a bounding box that covers all points inside, or a tight rectangle that is covered by all points. Below is the first method.

"all_points_x": [613, 2007, 1769, 581]
"all_points_y": [1767, 1674, 2260, 2395]

Though the graph (0,0) is at bottom left and image (0,0) point starts at top left the idea is same. Only top and bottom horizontal lines in my graph reference are flipped for image.

The graph is here, enter image description here

Take the max of all_points_x to get the right most vertical line and take minimum of all_points_x to get left most vertical line. Similarly take max and min of all_points_y to get top and bottom horizontal line.

p2 = max([613,2007,1769,581]) = 2007
p1 = min([613,2007,1769,581]) = 581
q2 = max([1767,1674,2260,2395]) = 2395
q1 = min([1767,1674,2260,2395]) = 1674

Get center point (cx,cy) by,

cx = (p1   p2) / 2 = 1294
cy = (q1   q2) / 2 = 2034.5

Get width, height, (w,h) by,

w = p2 - p1 = 1426
h = q2 - q1 = 721

Now if the image is (im_w, im_h) then the normalized format will be,

cx_new = cx / im_w
w_new = w / im_w
cy_new = cy / im_h 
h_new = h / im_h

So the 4 points are,

cx_new cy_new w_new h_new 

This format x_center y_center width height is used by YOLO. This link should be helpful.

CodePudding user response:

is it okay to find minimum covered regtangle from

--> min(x),min(y) max(x),max(y)

  • Related