# SegmentationMask

SegmentationParser node outputs this message. It contains a single-channel segmentation mask, where each pixel value corresponds
to the class label of the segmented object at that location. The mask is represented as a byte array where the value 255 is
reserved for the pixels that do not belong to any class, and the values from 0 to 254 are used for the actual class labels. For
example, in a binary segmentation task, the "background" class would be represented by 255 and the "object" class would be
represented by 0.

## Remapping

`SegmentationMask` is transformable, which means it can be remapped into another frame's coordinate system. See [`Remapping
transformable messages`](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages.md) for details.

## Examples of functionality

 * [Segmentation Parser](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/segmentation_parser.md) - Run a
   DeepLabv3+ segmentation model and display the resulting segmentation mask on a video stream.

## Reference

### dai::SegmentationMask

Kind: class

SegmentationMask message. Segmentation mask of an image is stored as a single-channel UINT8 array, where each value represents a
class or instance index. The value 255 is treated as background pixels (no class/instance).

#### int64_t sequenceNum

Kind: variable

#### Timestamp ts

Kind: variable

#### Timestamp tsDevice

Kind: variable

#### std::optional< ImgTransformation > transformation

Kind: variable

#### SegmentationMask()

Kind: function

#### SegmentationMask(const std::vector< std::uint8_t > & data, size_t width, size_t height)

Kind: function

#### ~SegmentationMask()

Kind: function

#### void serialize(std::vector< std::uint8_t > & metadata, DatatypeEnum & datatype)

Kind: function

#### DatatypeEnum getDatatype()

Kind: function

Get the datatype of this specific message.

return: DatatypeEnum

#### SegmentationMask transformTo(const ImgTransformation & target)

Kind: function

Returns a copy of this segmentation mask. Segmentation mask remapping is not implemented. For optimal performance, segmentation
masks should be generated from already aligned source messages instead of being transformed after the fact. Use ImageAlign on the
source inputs to align segmentation masks. parameters: target: Target image transformation.

#### void setSize(size_t width, size_t height)

Kind: function

Sets the size of the segmentation mask. note: Use with caution as it sets the metadata of the mask without allocating or resizing
the underlying data array.

#### std::size_t getWidth()

Kind: function

Returns the width of the segmentation mask.

#### std::size_t getHeight()

Kind: function

Returns the height of the segmentation mask.

#### void setMask(const std::vector< std::uint8_t > & mask, size_t width, size_t height)

Kind: function

Sets the segmentation mask from a vector of bytes. The size of the vector must be equal to width * height.

#### void setMask(dai::ImgFrame & frame)

Kind: function

Sets the segmentation mask from an ImgFrame . parameters: frame: Frame must be of type GRAY8

#### void setMask(span< const std::uint8_t > mask, size_t width, size_t height)

Kind: function

Sets the segmentation mask from a byte span without an extra temporary vector. The span size must be equal to width * height.

#### span< std::uint8_t > prepareMask(size_t width, size_t height)

Kind: function

Prepares internal storage for writing and returns a mutable view to it. The caller must fill exactly width * height bytes.

#### void setLabels(const std::vector< std::string > & labels)

Kind: function

Sets the class labels associated with the segmentation mask. The label at index parameters: labels: Vector of class labels

#### std::vector< std::uint8_t > getMaskData()

Kind: function

Returns a copy of the segmentation mask data as a vector of bytes. If mask data is not set, returns an empty vector.

#### dai::ImgFrame getFrame()

Kind: function

Returns the segmentation mask as an ImgFrame . If mask data is not set, returns an empty frame with only metadata set.

#### std::optional< uint32_t > getArea(uint8_t index)

Kind: function

Returns the area (number of pixels) of the specified instance/class index in the segmentation mask. parameters: index:
Instance/Class index note: If index is not present in the mask, returns std::nullopt.

#### std::optional< dai::Point2f > getCentroid(uint8_t index)

Kind: function

Returns the normalized centroid (x,y) coordinates of the specified instance/class index in the segmentation mask. parameters:
index: Instance/Class index note: If index is not present in the mask, returns std::nullopt.

#### std::vector< uint8_t > getUniqueIndices()

Kind: function

Returns a list of sorted unique indices present in the segmentation mask.

#### std::vector< std::string > getLabels()

Kind: function

Returns all class labels associated with the segmentation mask. If no labels are set, returns an empty vector.

#### std::vector< std::uint8_t > getMaskByIndex(uint8_t index)

Kind: function

Returns a binary mask where pixels belonging to the specified instance/class index are set to 1, others to 0. If mask data is not
set, returns an empty vector.

#### std::vector< std::uint8_t > getMaskByLabel(const std::string & label)

Kind: function

Returns a binary mask where pixels belonging to the specified class label are set to 1, others to 0. If labels are not set or
label not found, returns an empty vector.

#### bool hasValidMask()

Kind: function

Returns true if the mask data is not empty and has valid size (width * height).

#### void setCvMask(cv::Mat mask)

Kind: function

note: This API only available if OpenCV support is enabled Copies cv::Mat data to Segmentation Mask buffer parameters: frame:
Input cv::Mat frame from which to copy the data note: Throws if mask is not a single channel INT8 type.

#### cv::Mat getCvMask(cv::MatAllocator * allocator)

Kind: function

Retrieves mask data as a cv::Mat copy with specified width and height. If mask data is not set, returns an empty matrix.
parameters: allocator: Allows callers to supply a custom cv::MatAllocator for zero-copy/custom memory management; nullptr uses
OpenCV’s default.

#### cv::Mat getCvMaskByIndex(uint8_t index, cv::MatAllocator * allocator)

Kind: function

Returns a binary mask where pixels belonging to the instance index are set to 1, others to 0. If mask data is not set, returns an
empty matrix. parameters: index: Instance index; allocator: Allows callers to supply a custom cv::MatAllocator for
zero-copy/custom memory management; nullptr uses OpenCV’s default.

#### std::vector< std::vector< dai::Point2f > > getContour(uint8_t index)

Kind: function

Calls the opencv findContours function and filters the results based on the provided index. Returns filtered contour as a vector
of vectors of non-normalized points. If mask data is not set, returns an empty vector. parameters: index: class index

#### std::vector< dai::RotatedRect > getBoundingBoxes(uint8_t index, bool calculateRotation)

Kind: function

Returns a bounding box for each continuous region with the specified index. parameters: index: class index; calculateRotation: If
true, returns rotated bounding boxes, otherwise returns the outer, axis-aligned bounding boxes.

#### DEPTHAI_SERIALIZE(SegmentationMask, ts, tsDevice, sequenceNum, transformation, width, height, labels)

Kind: function

#### int64_t getSequenceNum()

Kind: function

Retrieves image sequence number

#### std::chrono::time_point< std::chrono::steady_clock, std::chrono::steady_clock::duration > getTimestamp()

Kind: function

Retrieves timestamp related to dai::Clock::now()

#### std::chrono::time_point< std::chrono::steady_clock, std::chrono::steady_clock::duration > getTimestampDevice()

Kind: function

Retrieves timestamp directly captured from device's monotonic clock, not synchronized to host time. Used mostly for debugging

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
