addObjectsToSegment
R2026bAdd objects for video segmentation using point or bounding box prompts
Since R2026b
Syntax
Description
The addObjectsToSegment function specifies which objects
to segment across video frames by providing point or bounding box prompts on specific
frames.
Call this function after creating a sam2VideoObjectSegmenter object and before calling segmentObjects. You can call this function multiple times to add objects
on different frames or to provide additional prompts for existing objects to improve
segmentation accuracy.
Note
This functionality requires the Image Processing Toolbox™ Model for Segment Anything Model 2 add-on.
addObjectsToSegment(
adds one or more objects specified by vidSegmenter,objectIDs,frameIDs,ObjectPoints=objectPoints)objectIDs for
segmentation on the video frames specified by frameIds, using
the point prompts in objectPoints to identify each object
location. Each point indicates a location on the object you want to segment.
addObjectsToSegment(
adds one or more objects for segmentation using bounding box prompts in
vidSegmenter,objectIDs,frameIDs,ObjectBoundingBox=bboxes)bboxes to identify each object region. Use bounding boxes
when you have detector output or when a box better captures the object extent than
individual points.
addObjectsToSegment(___,BackgroundPoints=
additionally specifies background points near the object for better disambiguation
using any of the previous syntaxes. For example,
bgPoints)addObjectsToSegment(vidSegmenter,"person",1,ObjectPoints=[100,200],BackgroundPoints=[50,60])
adds a point prompt identifying a person at pixel location
[100,200] and a background point at
[50,60] to exclude that region from the segmentation
mask.
Examples
Input Arguments
More About
Tips
If the segmentation includes unwanted background regions, add
BackgroundPointson those regions to refine the object boundary.For more accurate segmentation across long videos, add prompts for the same object on multiple frames that are far apart in time. SAM 2 uses all provided prompts to maintain consistent identity.
You can call this function at any point during the segmentation workflow even after calling
segmentObjectson some frames. Calling this function for the same object on a frame it has already been added to will replace the existing prompts on that frame. Add new prompts to correct errors or introduce new objects that appear later in the video.When using an object detector to generate prompts, pass the detected bounding boxes directly using
ObjectBoundingBox.
Version History
Introduced in R2026b




