kwimage.structs.segmentation module¶
Generic segmentation object that can use either a Mask or (Multi)Polygon backend.
- 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()
- property meta¶
- class kwimage.structs.segmentation.SegmentationList(data, meta=None)[source]¶
Bases:
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
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))