cgvict / roLabelImg

Label Rotated Rect On Images for training
835 stars 223 forks source link

Rotated rectangle implementation [Question] #21

Open edu638s opened 3 years ago

edu638s commented 3 years ago

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 commented 3 years ago

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)