1

I am pretty new to using openCV and this might be a silly question.enter image description here

I will talk in reference to the picture above. So my problem statement is to draw bounding boxes around all the text elements. Conveniently I can get that information from the json file of the image. So I can get x, y, w and h from json and use cv2.rectangle(img1, (x, y), (x + w, y + h), (255, 255, 255), 2)

The issues is in the json file, for rotated text, it just provides the rotation angle and not the co-ordinates for the rotated rectangle. For example from the image above, for 'yourwebsite.com', x, y, w and h is given and rotation is -90 degrees. I did some basic w, h = h, w to get the bounding box for that. But what should I do when the degrees is like 60 ro 80. Is there any function or method to get the coordinates of rotated bounding boxes?

Example: enter image description here

In this case the flash sale is tilted at an angle of -22 degrees. But the x, y, w and h is provided for the white rectangle on top not covering anything. It needs to be tilted by a particular angle as mentioned in the json file to be over the FLASH SALE area. x and y always represents the top left corner of the bounding box.

Thank you in advance!

Danush Aaditya
  • 73
  • 1
  • 2
  • 12
  • Can you post a real example of some rotation between 0 and 90 but not at the extremes. For that case, what do x,y,w,h represent? Are they still the bounding box which is aligned to the coordinate axes? If not, then to what do they correspond? – fmw42 Jun 08 '21 at 04:24
  • Hey I have edited the original post so you can read it! – Danush Aaditya Jun 08 '21 at 05:04
  • 2
    Why can't you just write your own transformation? It seems pretty straightforward. Your rotation angle is always going to be -ve of that given in the json file, and the pivot is always the top-left corner. So, for e.g. your top-right x2_new = x2_orig * sin(theta) and y2_new = y2_orig * cos(theta). You might also want to check out opencv's RotatedRect. – Knight Forked Jun 08 '21 at 08:11
  • @KnightForked I did something similar to what you said. But I used a different formula. x2_new = int(x0+(x2-x0)*math.cos(theta * math.pi / 180)+(y2-y0)*math.sin(theta * math.pi / 180)) and y2_new = int(y0-(x2-x0)*math.sin(theta * math.pi / 180)+(y2-y0)*math.cos(theta * math.pi / 180)). x0 and y0 is the pivot point. Thank you! – Danush Aaditya Jun 08 '21 at 08:46
  • Does the box need to be rotated to align with the text? Or, the box will be as usual just making sure it is covering the rotated text (I am assuming it is). – hafiz031 Jun 08 '21 at 09:10
  • @hafiz031 The box is just covering the rotated text. Did not figure out how to rotate the box itself – Danush Aaditya Jun 09 '21 at 05:49
  • 1
    @DanushAaditya ok, in this case draw box by drawing lines instead: https://stackoverflow.com/a/18633964/6907424 – hafiz031 Jun 09 '21 at 06:16

1 Answers1

3

I think your question is "how to draw a rotated bounding box when you have only the upper left corner of the box and its dimension and rotation angle". If this is correct, then you can do this from the geometry and trigonometry of the rotated bounding box via Python/OpenCV or any other tool using the following diagram and mathematics.

enter image description here

Given that we know (X1,Y1), width W, height H and angle theta for the rotation. Here we use the convention that the rotation is counter-clockwise positive as in the diagram. Using the right angle subtended at the top, we can compute (X2,Y2) as

X2 = x1 + W*cos(theta)
Y2 = Y1 - W*sin(theta)

We can get (X4,Y4) from the right triangle on the left side as:

X4 = X1 + H*sin(theta)
Y4 = Y1 + H*cos(theta)

And finally we can get (X3,Y3) using (X4,Y4) and the bottom right triangle:

X3 = X4 + W*cos(theta)
Y3 = Y4 - W*sin(theta)
fmw42
  • 46,825
  • 10
  • 62
  • 80