segmentObjects
R2026bDescription
The segmentObjects function segments all objects that
have been added to the sam2VideoObjectSegmenter object on a specified video frame, returning
binary masks and confidence scores for each object.
Call segmentObjects after adding at least one object for
segmentation using addObjectsToSegment. You can call
segmentObjects on any frame in any order and SAM 2 propagates
object identity from prompted frames to the requested frame using temporal processing.
However, processing frames sequentially enables SAM 2 to build temporal context more
effectively.
You can visualize results using the insertObjectMask
function.
Note
This functionality requires the Image Processing Toolbox™ Model for Segment Anything Model 2 add-on.
[
segments all added objects on the video frame specified by
masks,objectIDs,maskScores] = segmentObjects(vidSegmenter,frameId)frameId and returns the binary segmentation masks
masks, the corresponding object identifiers
objectIDs, and per-pixel confidence scores
maskScores.
Examples
Input Arguments
Output Arguments
Tips
If an object is not segmented on a particular frame (for example, it is fully occluded), that object may not appear in the output. Check
objectIDsto determine which objects were successfully segmented.If segmentation quality degrades on frames far from the prompted frames, add additional prompts on those frames using
addObjectsToSegmentand re-runsegmentObjects.Use
maskScoresto identify unreliable predictions. Low average confidence within the mask region suggests the model is uncertain about the object location or boundary.When an object leaves the scene,
segmentObjectsstops returning its ID. Use ID presence or mask area to detect departures rather thanmaskScores, which remain high even during departure.Process frames sequentially to prevent identity drift. Sparse frame processing loses temporal context and increases the risk of confusing visually similar objects.
Version History
Introduced in R2026b




