sam2VideoObjectSegmenter
R2026bDescription
The sam2VideoObjectSegmenter function creates a video
object segmenter that segments and tracks objects across video frames using the Segment
Anything Model 2 (SAM 2).
Use this object to perform prompt-driven video object segmentation by specifying
objects on one or more frames using point or bounding box prompts. When you call
segmentObjects on each frame, the segmenter propagates object
identity from the prompted frames to the requested frame using temporal context. The
typical workflow is:
Create a
sam2VideoObjectSegmenterobject from a video file or image datastore.Add objects to segment using
addObjectsToSegmentwith point or bounding box prompts.Loop through the video frames and call
segmentObjectson each frame to segment the specified objects.Remove objects that have permanently left the scene using
removeObjectsToSegmentto free resources and prevent the segmenter from searching for them. If an object temporarily leaves and re-enters, add a new prompt on the re-entry frame usingaddObjectsToSegmentwith the same object ID.Release GPU resources with
releaseGPUMemorywhen segmentation is complete.
Unlike frame-by-frame approaches such as segmentAnythingModel or imsegsam,
sam2VideoObjectSegmenter maintains consistent object identities
across frames by propagating segmentation results. This reduces the need to re-prompt on
every frame.
Note
This functionality requires the Image Processing Toolbox™ Model for Segment Anything Model 2 add-on.
Creation
Syntax
Description
creates a video object segmenter vidSegmenter = sam2VideoObjectSegmenter(videoName)vidSegmenter from the
video file specified by videoName. The function loads the
SAM 2 model and writes extracted video frames to disk for processing. By
default, the object uses the "sam2-large" model
variant.
creates a video object segmenter vidSegmenter = sam2VideoObjectSegmenter(ds)vidSegmenter from the
file-backed datastore ds, which represents an image
sequence. The function processes the images in read order and writes those
frames to disk in the format required by SAM 2.
specifies additional options using one or more name-value arguments. For
example,
vidSegmenter = sam2VideoObjectSegmenter(___,Name=Value)sam2VideoObjectSegmenter("video.mp4",ModelName="sam2-tiny")
uses the smallest and fastest SAM 2 model variant,
sam2-tiny.
Note
During creation, the sam2VideoObjectSegmenter object
converts the video or images in the datastore into consecutively numbered JPEG
files and writes them to disk. For long videos or large image sequences, this
process can take some time and requires sufficient disk space.
Input Arguments
Name-Value Arguments
Properties
Object Functions
Examples
Tips
For best results, mark each object on at least two frames that are far apart in time from each other (for example, frame 1 and frame 200). This helps SAM 2 maintain consistent object identity across the video.
If SAM 2 segments the wrong object or includes background regions, add
BackgroundPointsinaddObjectsToSegmentto refine the segmentation boundary.Process frames sequentially to prevent identity drift. Sequential processing builds temporal context that allows the segmenter to distinguish departures from new arrivals. Remove permanently departed objects using
removeObjectsToSegmentto free resources.When an object leaves the scene,
segmentObjectsstops returning its ID. Use ID presence or mask area to detect departures. Do not rely onmaskScoresas they may remain high even during departure.If an object temporarily leaves the frame and re-enters, do not remove it. Instead, add a new prompt on the re-entry frame using
addObjectsToSegmentwith the same object ID. This refreshes the model memory without losing identity continuity.When two visually similar objects cross paths, the
sam2VideoObjectSegmenterobject might confuse their identities because it relies on visual cues without physics-based motion prediction. CombinesegmentObjectsoutput with a multi-object tracker such asvideoTracker(Sensor Fusion and Tracking Toolbox) for more robust identity association.
Version History
Introduced in R2026b




