imgaug icon indicating copy to clipboard operation
imgaug copied to clipboard

Bounding boxes from center x, center y, center height, center width

Open let-me-cook opened this issue 4 years ago • 7 comments

Might be a good feature if we can write something like this

bb = BoundingBox(center_x=center_x, center_y=center_y, center_height=center_height, center_width=center_width)

rather than

bb = BoundingBox(
    x1=center_x - center_width / 2,
    y1=center_y + center_height / 2,
    x2=center_x + center_width / 2,
    y2=center_y - center_height / 2, 
 )

let-me-cook avatar Oct 31 '20 13:10 let-me-cook

I guess that if you have to do that for a lot of boxes it would be worth it to write a subclass

import imgaug as ia

class CWHBoundingBox(ia.BoundingBox):
    def __init__(self, center_x, center_y, 
                 width, height, label = None):

        super().__init__(
            x1=center_x - width / 2,
            y1=center_y + height / 2,
            x2=center_x + width / 2,
            y2=center_y - height / 2,
            label = label
        )

my_bb = ia.BoundingBox(x1=7, y1=8, x2=13, y2=12, label=None)
my_box = CWHBoundingBox(center_x = 10, center_y = 10, width = 6, height = 4)

print(my_box)
# BoundingBox(x1=7.0000, y1=8.0000, x2=13.0000, y2=12.0000, label=None)

my_box.width
# 6.0

jspaezp avatar Nov 05 '20 08:11 jspaezp

Going back to this issue and wondering if there could be a more elegant way to do this i found that you could add a class method to do this (so you don't actually make a new class and just add the functionality to the BoundingBox class)

snippet showing ow it could be done:

import imgaug as ia

def bb_from_cw(cls, center_x, center_y, width, height, label=None):

    obj = cls(
        x1=center_x - width / 2,
        y1=center_y + height / 2,
        x2=center_x + width / 2,
        y2=center_y - height / 2,
        label=label,
    )
    return obj


setattr(ia.BoundingBox, "from_cw", classmethod(bb_from_cw))

my_box = ia.BoundingBox.from_cw(center_x=10, center_y=10, width=6, height=4)
print(my_box)
# BoundingBox(x1=7.0000, y1=8.0000, x2=13.0000, y2=12.0000, label=None)

Hope it helps and I think the class method could be added as a in a PR. Up to @aleju if you think it would be useful

jspaezp avatar Nov 08 '20 20:11 jspaezp

I think that this feature will be very nice to have, especially if you work with YOLO models. @aleju any updates on this feature?

victor1cea avatar Feb 02 '22 17:02 victor1cea

@victor1cea The project has not been active for a while now. @aleju has not been active for a while and I am not aware of any co-maintainer ... i think it is stable enough to be used as is but to be completely honest I would not expect any new updates anytime soon.... sorry

I don't know if it would be time to fork it...

jspaezp avatar Feb 23 '22 17:02 jspaezp

Going back to this issue and wondering if there could be a more elegant way to do this i found that you could add a class method to do this (so you don't actually make a new class and just add the functionality to the BoundingBox class)

snippet showing ow it could be done:

import imgaug as ia

def bb_from_cw(cls, center_x, center_y, width, height, label=None):

    obj = cls(
        x1=center_x - width / 2,
        y1=center_y + height / 2,
        x2=center_x + width / 2,
        y2=center_y - height / 2,
        label=label,
    )
    return obj


setattr(ia.BoundingBox, "from_cw", classmethod(bb_from_cw))

my_box = ia.BoundingBox.from_cw(center_x=10, center_y=10, width=6, height=4)
print(my_box)
# BoundingBox(x1=7.0000, y1=8.0000, x2=13.0000, y2=12.0000, label=None)

Hope it helps and I think the class method could be added as a in a PR. Up to @aleju if you think it would be useful

As bounding boxes are top-left and bottom-right corner coordinates. y1 and y2 should be changed in bb_from_cw method, right ?

Digital2Slave avatar Feb 16 '23 07:02 Digital2Slave

@Digital2Slave I think it doe snot really matter ... :P having said that ... since this project is currently in an orphan state, I think it would be a good call to start using something else for the time being.

jspaezp avatar Feb 16 '23 19:02 jspaezp

@Digital2Slave I think it doe snot really matter ... :P having said that ... since this project is currently in an orphan state, I think it would be a good call to start using something else for the time being.

Okay, that's right. The author do not active for a long time. :disappointed:

Digital2Slave avatar Feb 17 '23 07:02 Digital2Slave