Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotated rectangle implementation [Question] #21

Open
edu638s opened this issue Feb 3, 2021 · 1 comment
Open

Rotated rectangle implementation [Question] #21

edu638s opened this issue Feb 3, 2021 · 1 comment

Comments

@edu638s
Copy link

edu638s commented Feb 3, 2021

I'm trying to get some parameters from a rotated rectangle in the roLabelImg generated XML and print the rectangle with matplotlib, which has a very similar representation, with variables "center coordinates, widht/height and angle":
Matplotlib: Rectangle((cx, cy), width=w, height=h, angle=theta).

So long I haven't been able to do that. I've even tried to do transformations like: cx' = cx - l*math.cos(theta+alfa)
I've considered the center points maybe could be different, because in matplotlib they represent xmin, ymin, so it can explain the transformation above. But didn't work.
So I'd like to know what is your implementation, or if you know how can i transform your coordinates to matplotlib style. Well, i would appreciate some help. Thanks!

@fahol28
Copy link

fahol28 commented Nov 25, 2021

Hey I had the same problem. In the source code I found the way how the author calculate the boxes.
With these two functions you should be able to show the boxes:

from PIL import Image, ImageDraw
import math

 def rotatePoint( xc,yc, xp,yp, theta):   
        xoff = xp-xc;`
        yoff = yp-yc;
        cosTheta = math.cos(theta)
        sinTheta = math.sin(theta)
        pResx = cosTheta * xoff + sinTheta * yoff
        pResy = - sinTheta * xoff + cosTheta * yoff
        # pRes = (xc + pResx, yc + pResy)
        return xc+pResx,yc+pResy 

def addRotatedShape(cx,cy,w,h,angle):
#         cx = float(robndbox.find('cx').text)
#         cy = float(robndbox.find('cy').text)
#         w = float(robndbox.find('w').text)
#         h = float(robndbox.find('h').text)
#         angle = float(robndbox.find('angle').text)

        p0x,p0y = rotatePoint(cx,cy, cx - w/2, cy - h/2, -angle)
        p1x,p1y = rotatePoint(cx,cy, cx + w/2, cy - h/2, -angle)
        p2x,p2y = rotatePoint(cx,cy, cx + w/2, cy + h/2, -angle)
        p3x,p3y = rotatePoint(cx,cy, cx - w/2, cy + h/2, -angle)
        return [p0x, p0y, p1x, p1y, p2x, p2y, p3x, p3y]

How you can print the image with an outline:

points= addRotatedShape(cx,cy,w,h,angle)
img = Image.open(fullImgPath)
ImageDraw.Draw(img).polygon(points, outline="blue")         
display(img)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants