Technology

Image Segmentation, Explained

Abhay Abhay 4 min read
Image Segmentation, Explained
Photo by Andrew Stutesman on Unsplash

Object detection draws a box around the cat. Image segmentation traces the cat. Every whisker, every paw, the awkward bit where the tail curls behind the sofa. Where a bounding box says “there’s roughly a cat over here,” segmentation answers the harder question: which exact pixels are cat? That shift from “roughly here” to “exactly these” is the whole game, and it’s why segmentation quietly powers everything from tumor detection to the lane-keeping in your car.

Per-pixel labeling, the core idea

At its heart, segmentation is a classification problem run a few million times. Instead of assigning one label to a whole image (“dog”), you assign a label to every single pixel. A 1920×1080 photo becomes roughly two million tiny decisions: road, road, road, pedestrian, sky, sky, traffic light, and so on. The output isn’t a box or a caption; it’s a mask, a same-sized map where each pixel carries a class.

That granularity is the superpower and the headache. You get pixel-perfect understanding, but you also need a model that can label two million things at once without melting your GPU or confidently declaring that the sky is asphalt.

Three flavors: semantic, instance, panoptic

Not all segmentation is created equal, and the difference comes down to one question: do you care which one?

  • Semantic segmentation labels every pixel by class but lumps all objects of the same class together. Five people in a photo? They’re all just “person.” It knows what, not how many. Great when you want to know “where’s the road” but couldn’t care less about counting cars.
  • Instance segmentation separates individual objects of the same class. Now those five people become person #1 through person #5, each with their own mask. It’s semantic segmentation with the ability to count and tell twins apart. Crucial for tracking, counting, and robotic vision.
  • Panoptic segmentation is the overachiever that does both. It handles “stuff” (sky, road, grass, the uncountable background) semantically and “things” (cars, people, the countable foreground) as distinct instances, all in one unified map. Autonomous driving loves panoptic because a car needs to understand both the layout of the road and each individual pedestrian on it.

A handy mental shortcut: semantic asks what, instance asks which, panoptic asks both, please, and hurry.

The architectures, in a line each

Two classics still anchor most conversations:

  • U-Net — a symmetric encoder-decoder shaped like a “U,” with skip connections that ferry fine spatial detail across the network. Born for biomedical imaging, still a semantic-segmentation workhorse.
  • Mask R-CNN — extends the Faster R-CNN object detector with a branch that predicts a mask per detected object, making it the go-to for instance segmentation.

Measuring “close enough”: IoU and Dice

How do you grade a two-million-pixel guess? Two metrics dominate, and both measure overlap between your predicted mask and the ground truth.

  • IoU (Intersection over Union) divides the overlapping area by the combined area. 1.0 is a perfect trace; 0 means you missed entirely.
  • Dice coefficient is the overlap doubled over the sum of both areas — mathematically the F1 score in a lab coat. It tends to be a touch more forgiving on small objects, which is why medical imaging leans on it.

They usually agree on who won and disagree politely on the margin. Here’s IoU in five honest lines:

import numpy as np

def iou(pred_mask, true_mask):
    intersection = np.logical_and(pred_mask, true_mask).sum()
    union = np.logical_or(pred_mask, true_mask).sum()
    return intersection / union if union else 1.0

The “segment anything” moment

For years, segmentation meant training a bespoke model on a painstakingly hand-labeled dataset. Then Meta dropped the Segment Anything Model (SAM), and its 2024 sequel SAM 2, which changed the vibe entirely. SAM 2 does zero-shot segmentation: point at a thing, click a box, or drop a rough scribble, and it masks the object — even objects it was never explicitly trained on. SAM 2 also extends to video, tracking objects across frames through occlusions and fast motion, and runs roughly six times faster than the original on images. It turned segmentation from “train a model for months” into “click and go,” which is a genuinely big deal for anyone labeling data.

Where it actually shows up

This isn’t an academic parlor trick. In medical imaging, segmentation outlines tumors, organs, and vascular leakage so radiologists can measure them precisely — and U-Net variants regularly clear Dice scores north of 0.90 on well-behaved tasks. In autonomous driving, panoptic segmentation tells the car where the drivable road ends and that specific cyclist begins, frame after frame. Add in satellite analysis, manufacturing defect detection, and the background blur on your video calls, and segmentation is quietly everywhere.

Your takeaway

Next time you reach for computer vision, ask the which-one question first. If you only need “where is the road,” use semantic segmentation and grade it with IoU. If you need to count or track individual objects, use instance segmentation (Mask R-CNN is a safe default). If you need both stuff and things in one shot, go panoptic. And before you spend a week hand-labeling masks to train from scratch, throw your images at SAM 2 first — zero-shot might get you 80% of the way there in an afternoon, and your GPU will thank you.

More posts