Class SegmentationMask

Inheritance Relationships

Base Types

Class Documentation

class SegmentationMask : public dai::Buffer, public dai::ProtoSerializable

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).

Public Functions

SegmentationMask()
SegmentationMask(const std::vector<std::uint8_t> &data, size_t width, size_t height)
virtual ~SegmentationMask()
virtual void serialize(std::vector<std::uint8_t> &metadata, DatatypeEnum &datatype) const override
inline virtual DatatypeEnum getDatatype() const override

Get the datatype of this specific message.

Returns:

DatatypeEnum

void setSize(size_t width, size_t height)

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() const

Returns the width of the segmentation mask.

std::size_t getHeight() const

Returns the height of the segmentation mask.

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

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)

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)

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)

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)

Sets the class labels associated with the segmentation mask. The label at index i in the labels vector corresponds to the value i in the segmentation mask data array.

Parameters:

labels – Vector of class labels

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

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() const

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) const

Returns the area (number of pixels) of the specified instance/class index in the segmentation mask.

Note

If index is not present in the mask, returns std::nullopt.

Parameters:

index – Instance/Class index

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

Returns the normalized centroid (x,y) coordinates of the specified instance/class index in the segmentation mask.

Note

If index is not present in the mask, returns std::nullopt.

Parameters:

index – Instance/Class index

std::vector<uint8_t> getUniqueIndices() const

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

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

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) const

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) const

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() const

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

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

Public Members

std::optional<ImgTransformation> transformation