kwimage.structs.segmentation module

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

class kwimage.structs.segmentation._WrapperObject[source]

Bases: NiceRepr

draw(*args, **kw)[source]
draw_on(*args, **kw)[source]

See help(self.data.draw_on)

warp(*args, **kw)[source]
translate(*args, **kw)[source]
scale(*args, **kw)[source]
to_coco(*args, **kw)[source]
numpy(*args, **kw)[source]
tensor(*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(rng=None)[source]

Example

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

Bases: ObjectList

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

to_polygon_list()[source]

Converts all mask objects to multi-polygon objects

to_mask_list(dims=None, pixels_are='points')[source]

Converts all mask objects to multi-polygon objects

to_segmentation_list()[source]
classmethod coerce(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

Todo

  • [ ] Handle WKT

Returns:

Mask | Polygon | MultiPolygon | Segmentation - 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))