kwimage.structs.segmentation

Generic segmentation object that can use either a Mask or (Multi)Polygon backend.

Module Contents

Classes

_WrapperObject

Inherit from this class and define __nice__ to “nicely” print your

Segmentation

Either holds a MultiPolygon, Polygon, or Mask

SegmentationList

Store and manipulate multiple segmentations (masks or polygons), usually

Functions

_coerce_coco_segmentation(data, dims=None)

Attempts to auto-inspect the format of segmentation data

class kwimage.structs.segmentation._WrapperObject[source]

Bases: ubelt.NiceRepr

Inherit from this class and define __nice__ to “nicely” print your objects.

Defines __str__ and __repr__ in terms of __nice__ function Classes that inherit from NiceRepr should redefine __nice__. If the inheriting class has a __len__, method then the default __nice__ method will return its length.

Example

>>> import ubelt as ub
>>> class Foo(ub.NiceRepr):
...    def __nice__(self):
...        return 'info'
>>> foo = Foo()
>>> assert str(foo) == '<Foo(info)>'
>>> assert repr(foo).startswith('<Foo(info) at ')

Example

>>> import ubelt as ub
>>> class Bar(ub.NiceRepr):
...    pass
>>> bar = Bar()
>>> import pytest
>>> with pytest.warns(RuntimeWarning) as record:
>>>     assert 'object at' in str(bar)
>>>     assert 'object at' in repr(bar)

Example

>>> import ubelt as ub
>>> class Baz(ub.NiceRepr):
...    def __len__(self):
...        return 5
>>> baz = Baz()
>>> assert str(baz) == '<Baz(5)>'

Example

>>> import ubelt as ub
>>> # If your nice message has a bug, it shouldn't bring down the house
>>> class Foo(ub.NiceRepr):
...    def __nice__(self):
...        assert False
>>> foo = Foo()
>>> import pytest
>>> with pytest.warns(RuntimeWarning) as record:
>>>     print('foo = {!r}'.format(foo))
foo = <...Foo ...>
__nice__(self)[source]
draw(self, *args, **kw)[source]
draw_on(self, *args, **kw)[source]
warp(self, *args, **kw)[source]
translate(self, *args, **kw)[source]
scale(self, *args, **kw)[source]
to_coco(self, *args, **kw)[source]
numpy(self, *args, **kw)[source]
tensor(self, *args, **kw)[source]
class kwimage.structs.segmentation.Segmentation(data, format=None)[source]

Bases: _WrapperObject

Either holds a MultiPolygon, Polygon, or Mask

Parameters
  • data (object) – the underlying object

  • format (str) – either ‘mask’, ‘polygon’, or ‘multipolygon’

classmethod random(cls, rng=None)[source]

Example

>>> self = Segmentation.random()
>>> print('self = {!r}'.format(self))
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> kwplot.figure(fnum=1, doclf=True)
>>> self.draw()
>>> kwplot.show_if_requested()
to_multi_polygon(self)[source]
to_mask(self, dims=None)[source]
property meta(self)[source]
classmethod coerce(cls, data, dims=None)[source]
class kwimage.structs.segmentation.SegmentationList[source]

Bases: kwimage.structs._generic.ObjectList

Store and manipulate multiple segmentations (masks or polygons), usually within the same image

to_polygon_list(self)[source]

Converts all mask objects to multi-polygon objects

to_mask_list(self, dims=None)[source]

Converts all mask objects to multi-polygon objects

to_segmentation_list(self)[source]
classmethod coerce(cls, data)[source]

Interpret data as a list of Segmentations

kwimage.structs.segmentation._coerce_coco_segmentation(data, dims=None)[source]

Attempts to auto-inspect the format of segmentation data

Parameters
  • data – the data to coerce

    2D-C-ndarray -> C_MASK 2D-F-ndarray -> F_MASK

    Dict(counts=bytes) -> BYTES_RLE Dict(counts=ndarray) -> ARRAY_RLE

    Dict(exterior=ndarray) -> ARRAY_RLE

    # List[List[int]] -> Polygon List[int] -> Polygon List[Dict] -> MultPolygon

  • dims (Tuple) – required for certain formats like polygons height / width of the source image

Returns

Mask | Polygon | MultiPolygon - depending on which is appropriate

Example

>>> segmentation = {'size': [5, 9], 'counts': ';?1B10O30O4'}
>>> dims = (9, 5)
>>> raw_mask = (np.random.rand(32, 32) > .5).astype(np.uint8)
>>> _coerce_coco_segmentation(segmentation)
>>> _coerce_coco_segmentation(raw_mask)
>>> coco_polygon = [
>>>     np.array([[3, 0],[2, 1],[2, 4],[4, 4],[4, 3],[7, 0]]),
>>>     np.array([[2, 1],[2, 2],[4, 2],[4, 1]]),
>>> ]
>>> self = _coerce_coco_segmentation(coco_polygon, dims)
>>> print('self = {!r}'.format(self))
>>> coco_polygon = [
>>>     np.array([[3, 0],[2, 1],[2, 4],[4, 4],[4, 3],[7, 0]]),
>>> ]
>>> self = _coerce_coco_segmentation(coco_polygon, dims)
>>> print('self = {!r}'.format(self))