Segmentation
Image Segmentation
Dividing an image into regions by assigning every pixel to a segment, a class, or an object instance, the task behind semantic, instance, panoptic and interactive segmentation.
beginner
Image segmentation divides an image into regions: it decides, for every pixel, which segment, class or object the pixel belongs to. Where image classification gives one label per image and object detection a box per object, segmentation gives the exact pixels, so the shape, area and boundary of every region can be read off the result. This hub defines the task and its variants, outlines how methods evolved, and points to the articles on individual methods, metrics and datasets.
Problem Definition
A digital image is defined on a pixel grid . A segmentation partitions into regions that do not overlap and together cover the image:
Equivalently, it is a label map . What the labels mean defines the variant: arbitrary region ids in classical, unsupervised segmentation, where the algorithm decides ; one of fixed classes in semantic segmentation, where a class region may consist of disconnected pieces; one id per object in instance segmentation.
Haralick and Shapiro’s (1985) survey of classical methods lists desirable properties, among them that regions be uniform in some characteristic, such as gray level or texture, and that adjacent regions differ in it; the two demands can conflict. Shi and Malik (2000) open their normalized cuts paper with Wertheimer’s Gestalt factors of grouping (similarity, proximity, good continuation) and note that there may not be a single correct partition.
Input and Output
The input is an image, sometimes with depth, video frames or user prompts. The output is a label map (one integer per pixel, typically the argmax of per-pixel class scores), a set of binary masks (one per object, with a class and score in instance segmentation), or a boundary map (boundary pixels or per-pixel boundary probabilities, close to edge detection). COCO stores masks as polygons or run-length encoding; PASCAL VOC stores label maps as PNG images with a void label along object boundaries.
Variants
- Semantic segmentation labels every pixel with a class and does not separate objects of the same class.
- Instance segmentation gives each object of the “thing” classes its own mask, as in Mask R-CNN (He et al., 2017).
- Panoptic segmentation (Kirillov et al., 2019) gives every pixel a class and, for countable things (people, cars), an instance id, while amorphous “stuff” (sky, road) gets only a class; segments do not overlap.
- Foreground–background segmentation separates one object or region of interest from the rest.
- Interactive and promptable segmentation returns the mask that user input indicates. Boykov and Jolly (2001) used pixels marked “object” and “background” as hard constraints; in the Segment Anything Model (SAM; Kirillov et al., 2023) a prompt can be points, a rough box or mask, or, in initial experiments, free-form text.
- Video object segmentation follows objects through a video, usually given their first-frame masks, combining segmentation with object tracking.
- Medical image segmentation delineates organs, lesions and cells, often in 3D volumes; U-Net (Ronneberger et al., 2015) was designed for it.
Challenges
- What counts as a segment. Without a task, granularity is undefined: a face, an eye and a pupil are all valid segments. Martin et al. (2001) found that people segment the same image at different levels of detail, yet consistently when refinement is tolerated. When the ADE20K annotator relabeled 61 images six months later, 82.4% of pixels got the same label (Zhou et al., 2017).
- Boundaries. Object boundaries can be weak, while texture and shading create strong edges inside objects; downsampling in networks coarsens boundaries.
- Touching objects of one class form one connected region, as the example below shows; U-Net’s authors upweighted the loss on the thin background gaps between touching cells.
- Small and thin structures such as vessels and wires barely affect pixel-level scores.
- Class imbalance. Road, sky or background dominate pixel counts, and so per-pixel losses and metrics.
- Annotation cost. Pixel-accurate masks take far longer to draw than boxes.
Approaches
The field moved from grouping pixels by low-level similarity to networks that learn class-specific segmentation from labeled masks, and recently to models that segment any object on request.
Classical methods
- Thresholding compares each pixel’s intensity with one or more thresholds; Otsu (1979) chose the threshold that maximizes the between-class variance of the image histogram.
- Region growing and split-and-merge grow seed regions by similar neighbors, or split non-uniform blocks and merge similar neighbors.
- Clustering groups pixels in a feature space. k-means needs the number of clusters; mean shift (Comaniciu & Meer, 2002) seeks the modes of the feature density and, for segmentation, works in a joint spatial–range domain of pixel position and color.
- Watershed floods an image, usually the gradient magnitude, from its minima; boundaries are where floods meet. Vincent and Soille (1991) gave an efficient algorithm by simulating immersion. Every regional minimum starts a basin, so in practice flooding starts from markers (see mathematical morphology).
- Graph-based methods cut a graph whose nodes are pixels and whose edges weigh similarity. Graph cuts (Boykov & Jolly, 2001) find, by a minimum cut, the globally optimal binary segmentation under region and boundary terms and user seeds. Normalized cuts (Shi & Malik, 2000) weigh dissimilarity between groups against similarity within them, solved approximately as a generalized eigenvalue problem. Felzenszwalb and Huttenlocher (2004) merge regions greedily in time for pixels.
- Superpixels oversegment an image into small, compact regions that later stages use instead of pixels. SLIC (Achanta et al., 2012) runs k-means on CIELAB color and position, searching only near each center, so its cost is linear in the number of pixels.
- Active contours evolve a curve to minimize an energy. Snakes (Kass, Witkin & Terzopoulos, 1988) are splines pulled toward edges and lines by image forces. Level-set methods, building on Osher and Sethian (1988), represent the curve as the zero level set of a function, so it can split and merge.
None of these methods knows object categories: they group pixels that look alike, not pixels of the same object. Arbeláez et al. (2011) combined a learned contour detector with an oriented watershed transform into a hierarchy of regions, one answer to the granularity problem.
Deep learning
Fully convolutional networks. Long, Shelhamer and Darrell (2015) turned classification CNNs into fully convolutional networks (FCNs), which map an input of any size to a correspondingly sized map of class scores, trained end to end, with skips adding fine, shallow features to coarse, deep ones. They reported 62.2% mean IoU on PASCAL VOC 2012, a 20% relative improvement on the previous best.
Encoder–decoders. U-Net (Ronneberger et al., 2015) pairs a contracting path for context with a symmetric expanding path for localization, linked by skip connections, and trains from very few images with heavy augmentation.
Dilated convolutions. Dilated (atrous) convolutions enlarge the receptive field without losing resolution (Yu & Koltun, 2016). DeepLab used them from its first version in 2015; its journal version (Chen et al., 2018) adds atrous spatial pyramid pooling, sampling features at several rates for objects at several scales, and refines boundaries with a fully connected conditional random field.
Instance and panoptic segmentation. Mask R-CNN (He et al., 2017) adds a mask branch to the Faster R-CNN detector, in parallel with box recognition. Kirillov et al. (2019) defined panoptic segmentation with a baseline that combined separate instance and semantic segmenters heuristically.
Mask classification. MaskFormer (Cheng, Schwing & Kirillov, 2021) predicts a set of binary masks, each with one class label, with a transformer decoder as in DETR, so one model, loss and training procedure serve semantic and panoptic segmentation. Mask2Former (Cheng et al., 2022) restricts cross-attention to predicted mask regions; with one architecture and Swin-L backbones it reported 57.8 PQ (panoptic) and 50.1 AP (instance) on COCO val2017, and 57.7 mIoU on ADE20K val with a FaPN pixel decoder and multi-scale inference.
Promptable models. SAM (Kirillov et al., 2023) pairs a large image encoder with a prompt encoder and a lightweight mask decoder, trained on SA-1B (over 1 billion masks on 11 million images). It returns the region a prompt indicates, without a class label, and several masks for an ambiguous prompt, such as a point that could mean a shirt or the person wearing it.
| Family | Representative methods | Year | Key idea | Article |
|---|---|---|---|---|
| Thresholding | Otsu | 1979 | Split the intensity histogram | Thresholding |
| Region-based | Region growing, split-and-merge | — | Grow or split regions under a homogeneity test | — |
| Clustering | k-means, mean shift | 2002 (mean shift) | Group pixels in color (and position) space | — |
| Morphological | Watershed | 1991 | Flood the gradient image from its minima | Watershed |
| Graph-based | Graph cuts; normalized cuts; Felzenszwalb–Huttenlocher | 2000–2004 | Cut a pixel-similarity graph | Graph cuts; normalized cuts |
| Superpixels | SLIC | 2012 | Compact oversegmentation for later stages | Superpixels |
| Active contours | Snakes, level sets | 1988 | Evolve a curve to minimize an energy | — |
| Fully convolutional | FCN | 2015 | Dense class scores from a CNN, end to end | FCN |
| Encoder–decoder | U-Net | 2015 | Contracting and expanding paths with skips | U-Net; encoder–decoder |
| Dilated convolutions | DeepLab | 2015–2018 | Larger receptive field without downsampling | DeepLab |
| Detect then segment | Mask R-CNN | 2017 | A mask per detected object | Mask R-CNN; instance segmentation |
| Mask classification | MaskFormer, Mask2Former | 2021–2022 | Predict a set of masks, each with a class | Panoptic segmentation |
| Promptable | SAM | 2023 | Any object on request, with no class label | SAM |
Example: three classical segmentations of a synthetic image
The NumPy and SciPy code builds a image with a striped background that brightens from left to right, two touching bright disks, a dim square and Gaussian noise, keeping the ground-truth class and instance maps. It segments the image three ways, scored with per-class IoU and pixel accuracy: a global Otsu threshold; connected components of the thresholded mask, as instances; and k-means on intensity with three and four clusters.
import numpy as np
from scipy import ndimage
# A 64 x 64 test image: a background that brightens from left to right, with
# faint stripes; two touching bright disks (class 2) and a dim square (class 1).
rng = np.random.default_rng(0)
H = W = 64
yy, xx = np.mgrid[0:H, 0:W]
img = 0.15 + 0.25 * xx / (W - 1) + 0.04 * np.sin(2 * np.pi * yy / 8)
inst = np.zeros((H, W), int) # ground-truth instance ids
inst[(yy - 20)**2 + (xx - 38)**2 <= 8**2] = 1 # disk 1
inst[(yy - 20)**2 + (xx - 53)**2 <= 8**2] = 2 # disk 2, 15 px away: touching
inst[40:56, 10:26] = 3 # square
cls = np.choose(inst, [0, 2, 2, 1]) # 0 background, 1 dim, 2 bright
img[cls == 2] = 0.90
img[cls == 1] = 0.60
img += rng.normal(0, 0.05, img.shape)
def scores(pred, gt, k): # per-class IoU and pixel accuracy
iou = [((pred == c) & (gt == c)).sum() / ((pred == c) | (gt == c)).sum() for c in range(k)]
return np.round(iou, 3), round(np.mean(iou), 3), round((pred == gt).mean(), 3)
# 1. Global threshold chosen by Otsu's criterion on a 256-bin histogram.
hist, edges = np.histogram(img, bins=256)
p = hist / hist.sum()
w0 = np.cumsum(p) # class-0 weight for each cut
mu = np.cumsum(p * (edges[:-1] + edges[1:]) / 2) # first cumulative moment
with np.errstate(divide="ignore", invalid="ignore"):
between = (mu[-1] * w0 - mu)**2 / (w0 * (1 - w0)) # between-class variance
t = edges[np.nanargmax(between) + 1]
fg = img > t
iou, miou, acc = scores(fg.astype(int), (cls > 0).astype(int), 2)
print(f"Otsu threshold {t:.3f}: IoU [background, object] {iou}, pixel accuracy {acc}")
# 2. Connected components (4-connectivity) turn the foreground mask into instances.
lab, n = ndimage.label(fg)
sizes = sorted(np.bincount(lab.ravel())[1:].tolist(), reverse=True)
print(f"{n} components of sizes {sizes}; true object sizes {[int((inst == i).sum()) for i in (1, 2, 3)]}")
for i in (1, 2, 3): # best-matching component per object
best = max(((lab == j) & (inst == i)).sum() / ((lab == j) | (inst == i)).sum() for j in range(1, n + 1))
print(f" object {i}: best component IoU {best:.3f}")
# 3. k-means on intensity, initialized at evenly spaced percentiles; clusters are
# sorted by center and mapped to classes by the rule given in `to_class`.
def kmeans(x, k, iters=50):
c = np.percentile(x, np.linspace(10, 90, k))
for _ in range(iters):
lbl = np.argmin(np.abs(x[:, None] - c[None, :]), axis=1)
c = np.array([x[lbl == j].mean() for j in range(k)])
rank = np.argsort(np.argsort(c)) # 0 = darkest cluster
return rank[lbl].reshape(H, W), np.sort(c)
for k, to_class in [(3, [0, 1, 2]), (4, [0, 0, 1, 2])]: # k = 4: two darkest -> background
seg, c = kmeans(img.ravel(), k)
km = np.array(to_class)[seg]
iou, miou, acc = scores(km, cls, 3)
print(f"k-means k={k}, centers {np.round(c, 3)}: IoU [bg, dim, bright] {iou}, mIoU {miou}, "
f"pixel accuracy {acc}")
print(f" background labeled dim: {((km == 1) & (cls == 0)).sum()} px, "
f"{((km == 1) & (cls == 0) & (xx >= W // 2)).sum()} of them in the right half")
Output:
Otsu threshold 0.531: IoU [background, object] [0.993 0.963], pixel accuracy 0.994
5 components of sizes [392, 235, 1, 1, 1]; true object sizes [195, 197, 256]
object 1: best component IoU 0.497
object 2: best component IoU 0.503
object 3: best component IoU 0.918
k-means k=3, centers [0.207 0.382 0.842]: IoU [bg, dim, bright] [0.584 0.084 0.775], mIoU 0.481, pixel accuracy 0.622
background labeled dim: 1433 px, 1229 of them in the right half
k-means k=4, centers [0.19 0.341 0.58 0.898]: IoU [bg, dim, bright] [0.982 0.802 1. ], mIoU 0.928, pixel accuracy 0.985
background labeled dim: 62 px, 62 of them in the right half
The threshold, 0.531, falls between the brightest background (about 0.44 before noise) and the square (0.60); 21 of the 24 errors are square pixels that noise pushes below it. Connected components find five regions, not three: the largest, 392 pixels, is exactly the two disks (195 + 197) joined where they touch, so each disk is matched with IoU of only about 0.5; the square is a second component and three background noise pixels are the rest. Separating touching objects takes shape cues, as in watershed splitting, or a model that predicts instances.
With three clusters, k-means spends a cluster on the background gradient: it splits the background into dark and bright halves (centers 0.207 and 0.382) and divides the square between the disks’ cluster (114 pixels) and the bright-background one (142), so the “dim” class gets IoU 0.084. With four clusters, mean IoU is 0.928, but only because the code maps the two darkest clusters to background, a choice made by looking at the ground truth: clusters are not classes, and naming segments is what learned, semantic methods add. Over noise seeds 0–7 the outcomes were the same: object IoU 0.948–0.972 for the threshold, the disks always merged, and k-means mean IoU 0.48–0.53 with three clusters and 0.905–0.950 with four.
Datasets and Benchmarks
- Berkeley Segmentation Dataset: several human segmentations per natural image (Martin et al., 2001), without class labels. BSDS300 has 200 training and 100 test images; BSDS500 (Arbeláez et al., 2011) adds 200 test images.
- PASCAL VOC (Everingham et al., 2010): semantic segmentation of 20 classes plus background.
- COCO (Lin et al., 2014): instance masks for 80 thing categories, later extended with stuff and panoptic labels.
- Cityscapes (Cordts et al., 2016): street scenes from 50 cities, 5,000 finely and 20,000 coarsely annotated images.
- ADE20K (Zhou et al., 2017): exhaustively annotated scenes (20,210 training, 2,000 validation, 3,000 test images), with a scene-parsing benchmark of 150 object and stuff classes.
Evaluation Metrics
- Pixel accuracy, the fraction of correctly labeled pixels, is dominated by large classes (see accuracy).
- Mean IoU, the per-class intersection over union averaged over classes, is the standard semantic segmentation score; the Dice coefficient, a mask’s F1 score, is common in medical imaging.
- Mask AP scores instance segmentation with detection’s average precision, computing IoU on masks.
- Panoptic quality (PQ) (Kirillov et al., 2019) matches segments at IoU above 0.5, which makes matches unique, and divides the summed IoU of matched pairs by .
- Boundary F-measure. Martin, Fowlkes and Malik (2004) matched predicted boundary pixels to human ones, tolerating small localization errors, and summarized precision and recall by their harmonic mean. Arbeláez et al. (2011) report it at one threshold per dataset (ODS) and per image (OIS), alongside region measures such as segmentation covering.
Applications
Medical imaging measures organs, tumors and cells; driving and robotics segment road, vehicles and people; photo and video editing replaces backgrounds and removes objects; remote sensing maps land cover; inspection segments defects. Promptable models also speed up dataset annotation.
Open Problems
- Granularity and ambiguity. The wanted partition depends on the task; benchmarks with one ground truth per image count other valid answers as errors.
- Open vocabularies. Promptable models segment almost anything but do not name it.
- Annotation cost and domain shift. Masks are expensive, and models degrade on new domains, sensors and cities.
- Video and 3D. Consistency over time and across views, at low compute cost, remains harder than single-image segmentation.
Related
- Semantic Segmentation
Labeling every pixel of an image with one of a fixed set of classes, such as road, sky, car or person, without separating individual objects; the dense-prediction counterpart of image classification, also called scene parsing.
- Instance Segmentation
Detecting every object of a fixed set of "thing" classes and giving each one its own pixel mask, class label and confidence score, so that two touching cars come out as two cars rather than one car-colored region.
- Thresholding
Segmenting an image by comparing each pixel's intensity with a threshold, chosen by hand, from the histogram as in Otsu's method, or from each pixel's neighborhood as in Sauvola's method.
- Graph Cuts
Energy minimization by minimum s–t cut, which labels the pixels of an image optimally for binary energies with submodular pairwise terms and approximately, through expansion and swap moves, for many labels.
- Superpixels
An oversegmentation of an image into a few hundred compact, roughly uniform regions that follow image boundaries, used in place of pixels to make later processing cheaper.
- Dice Coefficient
Twice the overlap of a predicted and a ground-truth mask divided by their total size, equal to the F1 score over pixels; the standard overlap score of medical image segmentation and, in soft form, a training loss.
References
- Haralick, R. M. & Shapiro, L. G. (1985). Image Segmentation Techniques. Computer Vision, Graphics, and Image Processing, 29(1), 100–132.
- Otsu, N. (1979). A Threshold Selection Method from Gray-Level Histograms. IEEE Transactions on Systems, Man, and Cybernetics, 9(1), 62–66.
- Comaniciu, D. & Meer, P. (2002). Mean Shift: A Robust Approach Toward Feature Space Analysis. IEEE Transactions on Pattern Analysis and Machine Intelligence, 24(5), 603–619.
- Vincent, L. & Soille, P. (1991). Watersheds in Digital Spaces: An Efficient Algorithm Based on Immersion Simulations. IEEE Transactions on Pattern Analysis and Machine Intelligence, 13(6), 583–598.
- Boykov, Y. Y. & Jolly, M.-P. (2001). Interactive Graph Cuts for Optimal Boundary & Region Segmentation of Objects in N-D Images. IEEE International Conference on Computer Vision (ICCV), 1, 105–112.
- Shi, J. & Malik, J. (2000). Normalized Cuts and Image Segmentation. IEEE Transactions on Pattern Analysis and Machine Intelligence, 22(8), 888–905.
- Felzenszwalb, P. F. & Huttenlocher, D. P. (2004). Efficient Graph-Based Image Segmentation. International Journal of Computer Vision, 59(2), 167–181.
- Achanta, R., Shaji, A., Smith, K., Lucchi, A., Fua, P. & Süsstrunk, S. (2012). SLIC Superpixels Compared to State-of-the-Art Superpixel Methods. IEEE Transactions on Pattern Analysis and Machine Intelligence, 34(11), 2274–2282.
- Kass, M., Witkin, A. & Terzopoulos, D. (1988). Snakes: Active Contour Models. International Journal of Computer Vision, 1(4), 321–331.
- Osher, S. & Sethian, J. A. (1988). Fronts Propagating with Curvature-Dependent Speed: Algorithms Based on Hamilton–Jacobi Formulations. Journal of Computational Physics, 79(1), 12–49.
- Long, J., Shelhamer, E. & Darrell, T. (2015). Fully Convolutional Networks for Semantic Segmentation. IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 3431–3440.
- Ronneberger, O., Fischer, P. & Brox, T. (2015). U-Net: Convolutional Networks for Biomedical Image Segmentation. Medical Image Computing and Computer-Assisted Intervention (MICCAI), Lecture Notes in Computer Science, 234–241.
- Yu, F. & Koltun, V. (2016). Multi-Scale Context Aggregation by Dilated Convolutions. International Conference on Learning Representations (ICLR). arXiv:1511.07122.
- Chen, L.-C., Papandreou, G., Kokkinos, I., Murphy, K. & Yuille, A. L. (2018). DeepLab: Semantic Image Segmentation with Deep Convolutional Nets, Atrous Convolution, and Fully Connected CRFs. IEEE Transactions on Pattern Analysis and Machine Intelligence, 40(4), 834–848.
- He, K., Gkioxari, G., Dollár, P. & Girshick, R. (2017). Mask R-CNN. IEEE International Conference on Computer Vision (ICCV), 2980–2988.
- Kirillov, A., He, K., Girshick, R., Rother, C. & Dollár, P. (2019). Panoptic Segmentation. IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), 9396–9405.
- Cheng, B., Schwing, A. G. & Kirillov, A. (2021). Per-Pixel Classification is Not All You Need for Semantic Segmentation. Advances in Neural Information Processing Systems 34 (NeurIPS).
- Cheng, B., Misra, I., Schwing, A. G., Kirillov, A. & Girdhar, R. (2022). Masked-Attention Mask Transformer for Universal Image Segmentation. IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), 1280–1289.
- Kirillov, A., Mintun, E., Ravi, N., Mao, H., Rolland, C., Gustafson, L., Xiao, T., Whitehead, S., Berg, A. C., Lo, W.-Y., Dollár, P. & Girshick, R. (2023). Segment Anything. IEEE/CVF International Conference on Computer Vision (ICCV), 3992–4003.
- Martin, D., Fowlkes, C., Tal, D. & Malik, J. (2001). A Database of Human Segmented Natural Images and its Application to Evaluating Segmentation Algorithms and Measuring Ecological Statistics. IEEE International Conference on Computer Vision (ICCV), 2, 416–423.
- Martin, D. R., Fowlkes, C. C. & Malik, J. (2004). Learning to Detect Natural Image Boundaries Using Local Brightness, Color, and Texture Cues. IEEE Transactions on Pattern Analysis and Machine Intelligence, 26(5), 530–549.
- Arbeláez, P., Maire, M., Fowlkes, C. & Malik, J. (2011). Contour Detection and Hierarchical Image Segmentation. IEEE Transactions on Pattern Analysis and Machine Intelligence, 33(5), 898–916.
- Everingham, M., Van Gool, L., Williams, C. K. I., Winn, J. & Zisserman, A. (2010). The Pascal Visual Object Classes (VOC) Challenge. International Journal of Computer Vision, 88(2), 303–338.
- Lin, T.-Y., Maire, M., Belongie, S., Hays, J., Perona, P., Ramanan, D., Dollár, P. & Zitnick, C. L. (2014). Microsoft COCO: Common Objects in Context. European Conference on Computer Vision (ECCV), Lecture Notes in Computer Science, 740–755.
- Cordts, M., Omran, M., Ramos, S., Rehfeld, T., Enzweiler, M., Benenson, R., Franke, U., Roth, S. & Schiele, B. (2016). The Cityscapes Dataset for Semantic Urban Scene Understanding. IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 3213–3223.
- Zhou, B., Zhao, H., Puig, X., Fidler, S., Barriuso, A. & Torralba, A. (2017). Scene Parsing through ADE20K Dataset. IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 5122–5130.