Day 63: Using SAM/SAM2 for person cutout
A clean person cutout in the pipeline
Fold SAM into the pipeline: detection gives the person box, SAM turns it into a precise mask, and you apply the mask to extract a clean cutout (Stage 0's morphology cleanup from Day 10 still helps tidy mask edges). This cutout sharpens measurement — the silhouette edge is far more accurate than a rectangle — and produces the exact person shape Stage 4 will composite garments onto.
import cv2, numpy as np
mask = sam(img, bboxes=[person_box])[0].masks.data[0].cpu().numpy().astype(np.uint8)
# tidy edges with morphology (Stage 0 Day 10)
kernel = np.ones((5, 5), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
cutout = cv2.bitwise_and(img, img, mask=mask) # person on black
b, g, r = cv2.split(cutout)
rgba = cv2.merge([b, g, r, mask * 255]) # transparent backgroundThe mask refines the measurements too
With a precise silhouette you can cross-check pose-derived widths against the mask's actual extent at shoulder/hip height — another redundancy (Day 58's theme) that catches bad keypoints. A measurement that pose and silhouette agree on is one you can report with real confidence.
Key terms
- Person cutout
- The person extracted from the background via a segmentation mask, often with a transparent background.
- Mask post-processing
- Cleaning a raw mask (morphology, hole-filling) before use — the same tools as Stage 0 classical CV.
Beyond a nicer cutout, how does the precise person mask improve measurement reliability?