SegmentationMask
Examples of functionality
- Segmentation Parser - Run a DeepLabv3+ segmentation model and display the resulting segmentation mask on a video stream.
Reference
class
dai::SegmentationMask
variable
std::optional< ImgTransformation > transformation
function
SegmentationMask()function
SegmentationMask(const std::vector< std::uint8_t > & data, size_t width, size_t height)function
~SegmentationMask()function
void serialize(std::vector< std::uint8_t > & metadata, DatatypeEnum & datatype)inline function
DatatypeEnum getDatatype()function
void setSize(size_t width, size_t height)Sets the size of the segmentation mask.
Parameters
Use with caution as it sets the metadata of the mask without allocating or resizing the underlying data array.
function
std::size_t getWidth()Returns the width of the segmentation mask.
function
std::size_t getHeight()Returns the height of the segmentation mask.
function
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.
function
void setMask(dai::ImgFrame & frame)function
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.
function
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.
function
void setLabels(const std::vector< std::string > & labels)Sets the class labels associated with the segmentation mask. The label at index
Parameters
- labels: Vector of class labels
function
std::vector< std::uint8_t > getMaskData()Returns a copy of the segmentation mask data as a vector of bytes. If mask data is not set, returns an empty vector.
function
dai::ImgFrame getFrame()Returns the segmentation mask as an ImgFrame. If mask data is not set, returns an empty frame with only metadata set.
function
std::optional< uint32_t > getArea(uint8_t index)Returns the area (number of pixels) of the specified instance/class index in the segmentation mask.
Parameters
- index: Instance/Class index
Parameters
If index is not present in the mask, returns std::nullopt.
function
std::optional< dai::Point2f > getCentroid(uint8_t index)Returns the normalized centroid (x,y) coordinates of the specified instance/class index in the segmentation mask.
Parameters
- index: Instance/Class index
Parameters
If index is not present in the mask, returns std::nullopt.
function
std::vector< uint8_t > getUniqueIndices()Returns a list of sorted unique indices present in the segmentation mask.
function
std::vector< std::string > getLabels()Returns all class labels associated with the segmentation mask. If no labels are set, returns an empty vector.
function
std::vector< std::uint8_t > getMaskByIndex(uint8_t index)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.
function
std::vector< std::uint8_t > getMaskByLabel(const std::string & label)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.
function
bool hasValidMask()Returns true if the mask data is not empty and has valid size (width * height).
function
void setCvMask(cv::Mat mask)Parameters
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
Parameters
Throws if mask is not a single channel INT8 type.
function
cv::Mat getCvMask(cv::MatAllocator * allocator)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.
function
cv::Mat getCvMaskByIndex(uint8_t index, cv::MatAllocator * allocator)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.
function
std::vector< std::vector< dai::Point2f > > getContour(uint8_t index)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
function
std::vector< dai::RotatedRect > getBoundingBoxes(uint8_t index, bool calculateRotation)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.
function
DEPTHAI_SERIALIZE(SegmentationMask, Buffer::ts, Buffer::tsDevice, Buffer::sequenceNum, transformation, width, height, labels)Need assistance?
Head over to Discussion Forum for technical support or any other questions you might have.