This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
Understand when to use bounding boxes versus pixel-level masks for image
analysis.
What’s in this recipe:
- Run object detection to get bounding boxes and labels
- Run panoptic segmentation to get pixel-level masks
- Visualize and compare outputs side-by-side
Problem
You need to analyze objects in images, but there are two approaches:
Which should you use? Detection is faster but approximate. Segmentation
is slower but precise.
Solution
Run both approaches on the same images using DETR models and compare the
results.
Setup
Load images
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘detection_vs_seg’.
<pixeltable.catalog.dir.Dir at 0x145b43f90>
Created table ‘images’.
Inserted 2 rows with 0 errors in 0.22 s (9.21 rows/s)
2 rows inserted.
Run object detection
The detr_for_object_detection function returns bounding boxes, labels,
and confidence scores.
Parameters:
model_id: DETR variant (facebook/detr-resnet-50 or
facebook/detr-resnet-101)
threshold: Confidence threshold (0.0-1.0). Higher = fewer but more
confident detections
Output:
Added 2 column values with 0 errors in 4.09 s (0.49 rows/s)
2 rows updated.
Visualize detections with bounding boxes
Use bboxes_draw to overlay the detection results on the original
image.
Added 2 column values with 0 errors in 0.03 s (58.89 rows/s)
2 rows updated.
Run panoptic segmentation
The detr_for_segmentation function returns pixel-level masks and
segment metadata.
Parameters:
model_id: Segmentation model (facebook/detr-resnet-50-panoptic)
threshold: Confidence threshold for filtering segments
Output:
Note: The full segmentation output contains a numpy array that
can’t be stored as JSON. We store just the segments_info metadata
and compute the pixel-level visualization inline.
Visualize segmentation with colored overlay
Use overlay_segmentation to visualize the pixel masks with colored
regions and contours.
Compare side-by-side
Count objects per image
Explanation
Detection gives fast, approximate locations. Segmentation gives slower
but precise boundaries.
Capability comparison
When to use each
Choose detection when:
- You need to know what objects are present and where
(approximately)
- Speed matters (detection is 2x faster)
- You need search, filtering, or counting
- Bounding boxes suffice for visualization
Choose segmentation when:
- You need exact object boundaries (pixel-perfect masks)
- You’re doing image editing, compositing, or AR
- You need to measure actual object area/coverage
- You want scene composition analysis (what % is sky vs buildings)
See also