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:
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 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_)
- property dsize¶
- 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'
- corners(*args, **kwargs)[source]¶
Example
>>> import kwimage >>> assert kwimage.Box.random().corners().shape == (4, 2)
- 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))
- 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()
- 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()