kwimage.structs.single_box module

class kwimage.structs.single_box.Box(boxes, _check: bool = False)[source]

Bases: NiceRepr

Represents a single Box.

This is a convinience class. For multiple boxes use kwimage.Boxes, which is more efficient.

Currently implemented by storing a Boxes object with one item and indexing into it. This implementation could be done more efficiently.

SeeAlso:

kwimage.structs.boxes.Boxes

Example

>>> from kwimage.structs.single_box import *  # NOQA
>>> box = Box.random()
>>> print(f'box={box}')
>>> #
>>> box.scale(10).quantize().to_slice()
>>> #
>>> sl = (slice(0, 10), slice(0, 30))
>>> box = Box.from_slice(sl)
>>> print(f'box={box}')
property format
property data
classmethod random(**kwargs)[source]
classmethod from_slice(slice_, shape=None, clip=True, endpoint=True, wrap=False)[source]

Example

>>> import kwimage
>>> slice_ = kwimage.Box.random().scale(10).quantize().to_slice()
>>> new = kwimage.Box.from_slice(slice_)
classmethod from_shapely(geom)[source]
classmethod from_dsize(dsize)[source]
classmethod from_data(data, format)[source]
classmethod coerce(data, format=None, **kwargs)[source]
property dsize
translate(*args, **kwargs)[source]
warp(*args, **kwargs)[source]
scale(*args, **kwargs)[source]
clip(*args, **kwargs)[source]
quantize(*args, **kwargs)[source]
copy(*args, **kwargs)[source]
round(*args, **kwargs)[source]
pad(*args, **kwargs)[source]
resize(*args, **kwargs)[source]
intersection(other)[source]

Example

>>> import kwimage
>>> self = kwimage.Box.coerce([0, 0, 10, 10], 'xywh')
>>> other = kwimage.Box.coerce([3, 3, 10, 10], 'xywh')
>>> print(str(self.intersection(other)))
<Box(ltrb, [3, 3, 10, 10])>
union_hull(other)[source]

Example

>>> import kwimage
>>> self = kwimage.Box.coerce([0, 0, 10, 10], 'xywh')
>>> other = kwimage.Box.coerce([3, 3, 10, 10], 'xywh')
>>> print(str(self.union_hull(other)))
<Box(ltrb, [0, 0, 13, 13])>
to_ltrb(*args, **kwargs)[source]

Example

>>> import kwimage
>>> self = kwimage.Box.random().to_ltrb()
>>> assert self.format == 'ltrb'
to_xywh(*args, **kwargs)[source]

Example

>>> import kwimage
>>> self = kwimage.Box.random().to_xywh()
>>> assert self.format == 'xywh'
to_cxywh(*args, **kwargs)[source]

Example

>>> import kwimage
>>> self = kwimage.Box.random().to_cxywh()
>>> assert self.format == 'cxywh'
toformat(*args, **kwargs)[source]
astype(*args, **kwargs)[source]
corners(*args, **kwargs)[source]

Example

>>> import kwimage
>>> assert kwimage.Box.random().corners().shape == (4, 2)
to_boxes()[source]

Returns the underlying “kwimage.Boxes” data structure.

property aspect_ratio

Example:

>>> import kwimage
>>> assert not ub.iterable(kwimage.Box.random().aspect_ratio)
property center

Example:

>>> import kwimage
>>> assert len(kwimage.Box.random().center) == 2
property center_x

Example:

>>> import kwimage
>>> assert not ub.iterable(kwimage.Box.random().center_x)
property center_y

Example:

>>> import kwimage
>>> assert not ub.iterable(kwimage.Box.random().center_y)
property width

Example:

>>> import kwimage
>>> assert not ub.iterable(kwimage.Box.random().width)
property height

Example:

>>> import kwimage
>>> assert not ub.iterable(kwimage.Box.random().height)
property tl_x

Example:

>>> import kwimage
>>> assert not ub.iterable(kwimage.Box.random().tl_x)
property tl_y

Example:

>>> import kwimage
>>> assert not ub.iterable(kwimage.Box.random().tl_y)
property br_x

Example:

>>> import kwimage
>>> assert not ub.iterable(kwimage.Box.random().br_y)
property br_y

Example:

>>> import kwimage
>>> assert not ub.iterable(kwimage.Box.random().br_y)
property dtype
property area
to_slice(endpoint=True)[source]

Example

>>> import kwimage
>>> kwimage.Box.random(rng=0).scale(10).quantize().to_slice()
(slice(5, 8, None), slice(5, 7, None))
to_shapely()[source]

Example

>>> import kwimage
>>> kwimage.Box.random().to_shapely()
to_polygon()[source]

Example

>>> import kwimage
>>> kwimage.Box.random().to_polygon()
to_coco()[source]

Example

>>> import kwimage
>>> kwimage.Box.random().to_coco()
draw_on(image=None, color='blue', alpha=None, label=None, copy=False, thickness=2, label_loc='top_left')[source]

Draws a box directly on an image using OpenCV

Example

>>> import kwimage
>>> self = kwimage.Box.random(scale=256, rng=10, format='ltrb')
>>> canvas = np.zeros((256, 256, 3), dtype=np.uint8)
>>> image = self.draw_on(canvas)
>>> # xdoctest: +REQUIRES(--show)
>>> # xdoctest: +REQUIRES(module:kwplot)
>>> import kwplot
>>> kwplot.figure(fnum=2000, doclf=True)
>>> kwplot.autompl()
>>> kwplot.imshow(image)
>>> kwplot.show_if_requested()
_images/fig_kwimage_structs_single_box_Box_draw_on_002.jpeg
draw(color='blue', alpha=None, label=None, centers=False, fill=False, lw=2, ax=None, setlim=False, **kwargs)[source]

Draws a box directly on an image using OpenCV

Example

>>> # xdoctest: +REQUIRES(module:kwplot)
>>> import kwimage
>>> self = kwimage.Box.random(scale=512.0, rng=0, format='ltrb')
>>> self.translate((-128, -128), inplace=True)
>>> #image = (np.random.rand(256, 256) * 255).astype(np.uint8)
>>> # xdoctest: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> fig = kwplot.figure(fnum=1, doclf=True)
>>> #kwplot.imshow(image)
>>> # xdoctest: +REQUIRES(--show)
>>> self.draw(color='blue', setlim=1.2)
>>> # xdoctest: +REQUIRES(--show)
>>> for o in fig.findobj():  # http://matplotlib.1069221.n5.nabble.com/How-to-turn-off-all-clipping-td1813.html
>>>     o.set_clip_on(False)
>>> kwplot.show_if_requested()
_images/fig_kwimage_structs_single_box_Box_draw_002.jpeg
kwimage.structs.single_box._transfer_docstrings()[source]

Helper to populate Box docstrings from Boxes. In the future we should may want to autogenerate better docstrings statically, but for now lets at least add some introspection.