主要内容

removeObjectsToSegment

R2026b

Remove objects from video segmentation

Since R2026b

    Description

    The removeObjectsToSegment function removes previously added objects from a sam2VideoObjectSegmenter object so they are no longer segmented across video frames. Use this function when an object permanently leaves the scene, becomes irrelevant to your analysis, or was added by mistake.

    After removing objects, call segmentObjects again to update the segmentation results.

    Note

    This functionality requires the Image Processing Toolbox™ Model for Segment Anything Model 2 add-on.

    removeObjectsToSegment(vidSegmenter,objectIDs) removes one or more objects specified by objectIDs from being segmented in all frames. This removes all previously specified object points, background points, and bounding boxes for the objects in objectIDs.

    example

    removeObjectsToSegment(vidSegmenter,objectIDs,frameId) removes all prompts for the specified objects only on the frame frameId. Prompts on other frames remain active.

    Examples

    collapse all

    Remove objects from segmentation after they exit the video frame to improve performance.

    Create a video object segmenter and add two people for segmentation.

    vidSegmenter = sam2VideoObjectSegmenter("pedestrians.mp4");
    objectIDs = ["person1"; "person2"];
    objectPoints = {[100,200]; [300,200]};
    addObjectsToSegment(vidSegmenter, objectIDs, 1, ObjectPoints=objectPoints);

    Segment objects across frames. When an object exits the frame, remove it.

    prevIDs = objectIDs;
    for idx = 1:vidSegmenter.NumFrames
        [masks, ids] = segmentObjects(vidSegmenter, idx);
    
        % Detect departed objects by comparing returned IDs to previous frame.
        departed = setdiff(prevIDs, ids);
    
        if ~isempty(departed)
            removeObjectsToSegment(vidSegmenter, departed);
        end
    
        prevIDs = ids;
    end

    Alternatively, you can monitor mask area across frames to detect objects that are shrinking as they leave the scene and remove them before they fully exit.

    Input Arguments

    collapse all

    Video object segmenter, specified as a sam2VideoObjectSegmenter object.

    Data Types: sam2VideoObjectSegmenter

    Object identifiers to remove, specified as a numeric scalar or vector, or a string scalar or vector. The identifiers must match objects previously added using addObjectsToSegment.

    Data Types: double | string

    Frame index, specified as a positive integer scalar in the range [1, NumFrames]. When specified, only prompts on this frame are removed for the given objects. Prompts on other frames remain active.

    Data Types: double

    Tips

    • Remove objects as soon as they permanently leave the scene to free resources and prevent identity drift when processing frames sparsely.

    • If an object temporarily leaves and re-enters the scene, do not remove it. Instead, use addObjectsToSegment with the same object ID on the re-entry frame to refresh the model memory while preserving identity continuity.

    Version History

    Introduced in R2026b