NVIDIA / retinanet-examples

Fast and accurate object detection with end-to-end GPU optimization
BSD 3-Clause "New" or "Revised" License
887 stars 271 forks source link

how to define theta #183

Closed liwandong closed 4 years ago

liwandong commented 4 years ago

hello, I want to know,how to define the angle,if I have a rotated rectangle and four point coordinates, how can I get a no angle rectangle with four point coordinates and its angle value

james-nvidia commented 4 years ago

First construct the bounding box as [x, y, w, h], then rotate that box by theta radians anti-clockwise from the horizontal axis.

For example, this works if you are specifying your angle between -45 and 45 degrees.

def _corners2rotatedbbox(corners):
    corners = np.array(corners)
    centre = np.mean(np.array(corners), 0)
    theta = calc_bearing(corners[0], corners[1])
    rotation = np.array([[np.cos(theta), -np.sin(theta)],
                         [np.sin(theta), np.cos(theta)]])
    out_points = np.matmul(corners - centre, rotation) + centre
    x, y = list(out_points[0,:])
    w, h = list(out_points[2, :] - out_points[0, :])
    return [x, y, w, h, theta]

def calc_bearing(point1, point2):
    x1, y1 = point1
    x2, y2 = point2
    theta = math.atan2(y2 - y1, x2 - x1)
    theta = nor_theta(theta)
    return theta

def nor_theta(theta):
    if theta > math.radians(45):
        theta -= math.radians(90)
        theta = nor_theta(theta)
    elif theta <= math.radians(-45):
        theta += math.radians(90)
        theta = nor_theta(theta)
    return theta
liwandong commented 4 years ago

thank you very much,This will help me a lot.

ygean commented 4 years ago

@james-nvidia Hi, I met a problem when I used your code to transform a quadrangle into a rotated box, the height get a negative value, it's unnormal, the code shows below:

poly = [191,392,275,395,276,362,193,361]
poly = np.array(poly, dtype=np.float32)
poly = poly.reshape((4,2))
rbox = _corners2rotatedbbox(poly)
rbox
>>>[191.54476516907224,
 393.5165743174451,
 83.87509670416935,
 -33.01466572398152,
 0.03569911267932397]

And I have a question want you to give me an answer that is, why we need to make the angle's value between -45 and 45 degrees, this confuses me so much. Thank you!

james-nvidia commented 4 years ago

@zhouyuangan please see issue #230

chenynCV commented 4 years ago

What's the order of corners?

james-nvidia commented 4 years ago

Unknown. Please post here if you have a good solution. Otherwise you can try various combinations until you get positive width and height

ygean commented 4 years ago

@zhouyuangan please see issue #230

thank you for your reply, I found it's common defination in oriented text detection, so I choose another way to represent angle of rotated box, as you mention, any way could make sense for training neural network to learn the presentation as we make, I refer to RRPN paper that is a paper for arbitrary oriented text detetction, I write a text detetion network based on yolov3 successfully, thanks for your kind!

821029883 commented 3 years ago

@james-nvidia Hi, I would like to ask, if using a rotating box, the w and h of each target [x0,y0,w,h,sita] in json refer to the w1 and h1 of the original horizontal rectangular box, or the rotated w2 And h2? image

piseabhijeet commented 3 years ago

Hi @james-nvidia , @zhouyuangan

I noticed for few objects, the rotated rect drawn from shapely and the one drawn after calling _corners2rotatedbbox() does not have even more than 50% of overlap. What could be the reason?

I am posting my code snippet here: image

image