Guide · SAM 3 / 3.1 · precise selection
How to Select Specific Objects with Meta SAM Text finds every match. When you need exactly one, or exactly the right few, use clicks, boxes, candidate masks, filter rules, negative boxes and video object IDs.
Start here
Which Selection Method Should You Use?
Four questions point you to the right technique.
| Situation | Method |
|---|---|
| Editing one photo | Clicks or a box |
| Same choice on 1,000 images | Text + filter rule |
| Similar objects sneak in | Text + negative box |
| “The one holding …” | Text + click, or SAM 3 Agent |
| One object through a video | Click once → object ID |
Method 1
Positive and Negative Clicks
The most precise way to select one object. Green adds, red removes.
- 1Click the centreof the object, not its edge.
- 2Too much?Add a negative click on the part that shouldn’t be there (a held bag, a neighbour).
- 3Too little?Add a positive click on the missing part (a raised arm, a wheel).
- 4Stop at 2–4 clicks.More rarely helps; draw a box instead.
Clicks use SAM 3’s instance (SAM 2-style) mode: one object per selection. Side-by-side examples: prompt examples
Ambiguity
When One Click Means Several Things
A click on a shirt could mean the shirt, the person, or the person with their bag. SAM returns ranked candidates.
- Ask for candidates: leave multimask output on for a single click.
- Keep the best score automatically, or show all three and let the user pick.
- Remove ambiguity: one more click (positive on the whole, negative on the part) usually settles it.
- Multiple clicks? Turn multimask off: the clicks already say what you mean.
Method 2
Draw a Box
One box around one object is the fastest selection for clearly separated things.
- Tight but complete: include the whole object, little background.
- Overlapping objects? Add a click inside the box on the one you mean, or a negative click on the other.
- Box vs exemplar: in instance mode a box selects that object; as a positive exemplar with text it means “find more like this”. See exemplar boxes.
Method 3
Text + a Filter Rule (Automatable)
For pipelines: let text find every candidate, then choose one in code by size, position, score or nearness.
- Largest: the main product in a photo.
- Leftmost / top / bottom: position words like “the person on the left”.
- Nearest to a point: a user tap or a known location.
- Inside a region: one shelf, one lane, one desk.
- Top-k by score: the three most confident matches.
The describe() and pick() helpers are in the code section.
Method 4
Text + Negative Box
When a text prompt grabs look-alikes, box one as negative to exclude that kind.
- Example: “handle” + negative box on the oven handle → only cupboard handles.
- Works on a type, not one item: it removes things that look like the negative example.
- For one specific item use clicks or a rule instead.
- Animated example and code: negative exemplar boxes
Method 5
Select One Object in a Video
Give the chosen object its own ID on one frame; SAM 3 keeps it through the clip.
- Click to select: one click with an
obj_idon a clear frame. - Or text, then remove: prompt “runner”, then remove the IDs you don’t need.
- Drift? Add a click for the same
obj_idon the frame where it slipped. - Full script: video segmentation with Python
Code
Code for Each Selection Method
Pixel coordinates throughout, except where noted.
Transformers · clicks, box, best of 3 candidates
import torch
from PIL import Image
from transformers import Sam3TrackerProcessor, Sam3TrackerModel
model = Sam3TrackerModel.from_pretrained("facebook/sam3", device_map="auto")
processor = Sam3TrackerProcessor.from_pretrained("facebook/sam3")
image = Image.open("street.jpg").convert("RGB")
# A) one positive click + one negative click on the same object (pixel x, y)
inputs = processor(images=image, input_points=[[[[500, 375], [560, 420]]]],
input_labels=[[[1, 0]]], return_tensors="pt").to(model.device)
with torch.no_grad():
out = model(**inputs, multimask_output=False)
mask = processor.post_process_masks(out.pred_masks.cpu(), inputs["original_sizes"])[0][0, 0]
# B) a box around the object: x0, y0, x1, y1
inputs = processor(images=image, input_boxes=[[[420, 300, 640, 520]]], return_tensors="pt").to(model.device)
# C) one ambiguous click → 3 ranked candidates, keep the best
inputs = processor(images=image, input_points=[[[[500, 375]]]], input_labels=[[[1]]], return_tensors="pt").to(model.device)
with torch.no_grad():
out = model(**inputs) # multimask output by default
cands = processor.post_process_masks(out.pred_masks.cpu(), inputs["original_sizes"])[0][0]
best = cands[int(out.iou_scores[0, 0].argmax())]Ultralytics · clicks and box
from ultralytics import SAM
sam = SAM("sam3.pt") # or sam3.1_multiplex.pt (images)
sam("street.jpg", points=[500, 375], labels=[1]) # one click
sam("street.jpg", points=[[[500, 375], [560, 420]]], labels=[[1, 0]]) # + and − on one object
sam("street.jpg", bboxes=[420, 300, 640, 520]) # one boxany interface · text + filter rule
import numpy as np
def describe(masks, scores):
# turn SAM 3 outputs into simple facts you can sort on
objs = []
for i, (m, s) in enumerate(zip(masks, scores)):
ys, xs = np.nonzero(m)
if len(xs):
objs.append(dict(i=i, score=float(s), area=int(m.sum()),
cx=float(xs.mean()), cy=float(ys.mean())))
return objs
def pick(objs, rule="largest", point=None, region=None, k=1):
if region: # keep objects centred inside (x0, y0, x1, y1)
x0, y0, x1, y1 = region
objs = [o for o in objs if x0 <= o["cx"] <= x1 and y0 <= o["cy"] <= y1]
key = {
"largest": lambda o: -o["area"], "smallest": lambda o: o["area"],
"leftmost": lambda o: o["cx"], "rightmost": lambda o: -o["cx"],
"top": lambda o: o["cy"], "bottom": lambda o: -o["cy"],
"score": lambda o: -o["score"],
"nearest": lambda o: (o["cx"] - point[0]) ** 2 + (o["cy"] - point[1]) ** 2,
}[rule]
return sorted(objs, key=key)[:k]
# with the official sam3 processor:
# out = processor.set_text_prompt(state=processor.set_image(image), prompt="apple")
# masks = [m.squeeze().cpu().numpy().astype(bool) for m in out["masks"]]
# chosen = pick(describe(masks, out["scores"]), rule="nearest", point=(380, 115))
# mask = masks[chosen[0]["i"]]video · official sam3 session
# official sam3 video session: select by click, track only that object
predictor.handle_request(dict(type="add_prompt", session_id=sid, frame_index=0,
points=[[0.42, 0.55]], point_labels=[1], obj_id=1)) # check coordinate scale in the video notebook
# or: text first, then drop everyone you don't want
predictor.handle_request(dict(type="add_prompt", session_id=sid, frame_index=0, text="runner"))
for oid in [0, 2, 3]:
predictor.handle_request(dict(type="remove_object", session_id=sid, obj_id=oid))video · Transformers tracker
# Transformers: Sam3TrackerVideoModel, one object by click
processor.add_inputs_to_inference_session(
inference_session=session, frame_idx=0, obj_ids=1,
input_points=[[[[210, 350]]]], input_labels=[[[1]]])
for out in model.propagate_in_video_iterator(session):
... # masks for object 1 onlyTransformers examples follow the official SAM 3 Tracker docs. Setup: install SAM 3 · Python tutorial
No code
Selecting Objects Without Code
In the playground
- Click the object; add negative clicks to trim.
- Or type the object, then click the one you want.
- Zoom in to check edges before exporting.
- Open the playground
In ComfyUI
- Native template: text prompts with
:Nto cap matches, e.g.person:1. - ComfyUI-SAM3 pack: point, box and multi-region collector nodes for clicks.
- ComfyUI setup
Troubleshooting
Selection Problems and Fixes
| Problem | Fix |
|---|---|
| Mask includes a neighbour | Negative click on the neighbour |
| Only part of the object | Positive click on the missing part, or use a box |
| Click selects the wrong level (shirt vs person) | Pick another candidate, or add a second click |
| Text finds many; you need one | Filter rule (largest, leftmost, nearest), or click instead |
| Filter picks the wrong one sometimes | Combine rules (inside region + largest) or add a score threshold |
| Clicks land in the wrong place in code | Check pixel vs normalized coordinates for your interface |
| Selected object drifts in video | Correct with a click on that frame. Tracking loses objects |
Edges look rough after selecting? Improve mask quality · Then cut it out: background removal
Questions
Selecting Objects FAQ
How do I select just one object with SAM 3?
Click it, or draw a box around it. A text prompt returns every match, so to get one, either click instead, or run text and then keep one result by a rule such as largest, leftmost or nearest to a point.
What do negative clicks do?
A negative click marks an area that must not be in the mask. Use it when the mask spills onto a neighbouring object, like a bag held by a person.
Why does one click give three masks?
A single click can be ambiguous (shirt, person, or person plus bag). SAM returns several candidates ranked by a quality score; keep the best one, or add more clicks to remove the ambiguity.
Can I select “the person on the left” with text?
Not reliably with words alone. Prompt “person”, then pick the leftmost result in code. For complex descriptions, use SAM 3 Agent with a multimodal LLM.
How do I keep only one object in a video?
Click the object on one frame so it gets its own object ID, then propagate; or prompt by text and remove the other IDs.
Is a box or a click better?
A box is quicker for clear, separate objects. Clicks are better for oddly shaped or overlapping objects and for fixing mistakes.
Does SAM 3.1 change how selection works?
No. Prompts are the same; SAM 3.1 makes multi-object video tracking faster.