Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Prompting SAM with points, boxes and text

You know conceptually what a prompt does. This page puts all three types into practice: text prompts on a UC Berkeley satellite image, and point prompts on a close-up truck photo, building the practical vocabulary the main building-extraction workflow uses two pages from now.


1. Motivation

Choosing the right prompt type for a task saves real effort. A text prompt can find every building in a scene in one call; a point prompt gives you surgical control over a single object. Knowing which to reach for, rather than defaulting to one out of habit, is a practical skill this page builds directly.


2. Core idea

Text, point, and box prompts trade breadth against precision. Text prompts are fast and broad, describing a category rather than a location. Point prompts give precise control over a single object, at the cost of needing to know where to click. Box prompts guide the model spatially, useful when you can draw a region but do not want to click an exact point.


3. Workflow

A. Text prompts

A single word or short phrase segments every matching object in the scene at once, no coordinates required. This is the broadest, lowest-effort prompt type, well suited to a first look at a scene or a common, visually generic category.

unique=True assigns a distinct integer value to each segmented object, the same instance-identifier idea from L08. save_scores writes a separate raster storing each pixel’s confidence value, letting you filter or inspect low-confidence detections later without discarding the whole result.

B. Box prompts

A box drawn around one object tells SAM to use that object as a reference and search for visually similar objects elsewhere in the image, a box around one building often finds the other buildings in the scene too. Boxes are specified in [xmin, ymin, xmax, ymax] format; box_crs tells samgeo which coordinate system your coordinates use, so geographic coordinates work directly without manual conversion.

C. Point prompts

enable_inst_interactivity=True turns on point- and box-based instance segmentation mode. A single foreground point (label=1) selects whichever object is most likely at that pixel location. Adding more foreground points on the same object improves coverage for large or irregular shapes:

A background point (label=0) excludes a region, useful for refining an ambiguous boundary:

The foreground point still selects the object; the background point carves the excluded region out of the resulting mask, giving you fine-grained control an initial single-point prompt could not.

D. Choosing among the three


4. Python reactivation

Point and box coordinates are both passed as lists of lists ([[x1, y1], [x2, y2]] for points, [[xmin, ymin, xmax, ymax]] for boxes), the same nested-list pattern you have used for coordinate data throughout this course. point_labels and box_labels are separate, parallel lists, matched by position to the coordinates list, so the first label always corresponds to the first point or box, exactly the “parallel lists” pattern you used with zip() in earlier lessons.


5. Common pitfalls


6. Mini task

You want to segment exactly one specific building in a satellite scene, but a text prompt of “building” also picks up several other buildings you do not want included. What prompt strategy would isolate just the one building you care about?

Sample solution

A point prompt placed on the specific building of interest, or a box drawn tightly around just that building, would isolate it directly, since both give SAM a spatial anchor rather than a category description that matches every instance. If the point-only result still includes unwanted neighboring structures, adding a background point on the unwanted area would refine the mask further, the same foreground-plus-background refinement demonstrated in step C.


7. Key takeaways