kwimage.structs.segmentation
¶
Generic segmentation object that can use either a Mask or (Multi)Polygon backend.
Module Contents¶
Classes¶
Inherit from this class and define |
|
Either holds a MultiPolygon, Polygon, or Mask |
|
Store and manipulate multiple segmentations (masks or polygons), usually |
Functions¶
|
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 fromNiceRepr
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 ...>
- 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’
- class kwimage.structs.segmentation.SegmentationList[source]¶
Bases:
kwimage.structs._generic.ObjectList
Store and manipulate multiple segmentations (masks or polygons), usually within the same image
- 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))