ApplicationsExplainer

Watershed segmentation: how touching objects get split

How watershed segmentation turns each touching object into a distance-transform dome and floods it into one label per part, with the seed-threshold math.

LumaSpec Vision10 min read Share on LinkedIn

On this page 10 sections
  1. 01Why connected-component counting breaks on touching parts
  2. 02The distance transform: one dome per object
  3. 03Flooding the landscape: from domes to labels
  4. 04Worked example: two 10 mm tablets overlapping 2 mm at 30 µm per pixel
  5. 05Where the half-peak rule stops working
  6. 06When watershed is the wrong tool
  7. 07How many pixels the neck needs
  8. 08Where to run it: smart camera or vision controller
  9. 09Pre-flight checks before trusting the count
  10. 10Questions

Two tablets that touch in the image arrive at blob analysis as one connected region, so the counter reports one part where two passed. Watershed segmentation is the standard fix: it turns the distance transform of the thresholded image into a landscape with one dome per object and floods it, so every dome becomes its own labeled basin and the flood walls land exactly where the parts touch. The sections below explain the mechanism, work the numbers for two 10 mm tablets overlapping by 2 mm at a pixel scale of 30 µm, and derive the seed-threshold rule that decides whether you get two labels or one. If you count, sort or measure parts that arrive touching — pills, fasteners, terminals, cut produce — you will be able to predict whether your camera resolves the split.

Why connected-component counting breaks on touching parts

Threshold, then label connected regions: that chain reports one object per connected silhouette, and it is exactly right while the parts stay apart. Two 10 mm tablets overlapping by 2 mm form a single region of 149 mm² where two regions of 78.5 mm² should appear — the 8 mm² lens of shared area is the entire error. Every blob feature inherits the merge: area doubles, orientation points between the two centers, and a bounding box around the pair fits neither part. The same corruption hits any region-based job, including pipelines that count lines inside each region. When parts never touch, plain blob measurement by pixel count is all you need; this page is about the moment silhouettes meet.

The distance transform: one dome per object

The distance transform assigns every foreground pixel its distance to the nearest background pixel. On a round tablet of radius r the value climbs from 0 at the edge to r at the center, so each object becomes a smooth dome with one peak, and the dome height is set by geometry — the inradius — not by brightness. Two touching tablets in one image build two domes joined by a neck, and the height of the col in that neck, the saddle, decides everything that follows.

For two equal circular silhouettes with radius r and overlap o, the center distance is d = 2r − o and the saddle closes in one line:

peak = r saddle = sqrt(o × (r − o/4))

Feed r in millimeters and the saddle comes out in millimeters; divide by the pixel scale to work in pixels. The closed form assumes two equal circles. For any other silhouette, read the saddle straight off the distance map — one profile along the neck is enough.

Flooding the landscape: from domes to labels

The watershed treats the distance map as terrain under rising water. Seed one basin at each dome; as the level climbs, every seed claims the pixels around its dome, and where two claims meet the boundary freezes into a watershed line that runs through the saddle. One dome ends as one labeled region, and the label boundary lands on the crossing points of the two silhouettes — the true touch line, not a rectangle around the pair.

Production code seeds the flood with explicit markers rather than raw local maxima, because a single noisy ridge would spill a second basin. Thresholding the distance map at a level T is the cheapest marker source, and choosing T is the engineering decision this page turns on. The marker-based walk-through with distance-map seeds builds the pipeline step by step; here we settle the number that makes it hold.

Worked example: two 10 mm tablets overlapping 2 mm at 30 µm per pixel

Inputs. Two equal tablets, diameter 10 mm, so r = 5 mm; overlap o = 2 mm along the center line — a 20% overlap, the merge case that makes counters under-report. Camera: the LS-CMR-0166, a 4000 × 3000 sensor on a 1/1.7-inch imager, read over USB3. Framed at 120 mm across, that sensor sets the scale used below.

Rule. Two seeds survive while the threshold T sits above the saddle — the neck is deeper than the cut — and the marker disks, of radius peak − T, stay clear of each other.

Arithmetic.

  1. Pixel scale: 120 mm ÷ 4000 px = 0.030 mm/px.
  2. Dome peak: 5 mm ÷ 0.030 mm/px = 167 px.
  3. Center distance: d = 2 × 5 mm − 2 mm = 8 mm.
  4. Saddle: sqrt(2 mm × (5 mm − 0.5 mm)) = sqrt(9 mm²) = 3.0 mm = 100 px.
  5. Classic rule, T = 0.5 × 167 px ≈ 83 px: the saddle (100 px) lies above it, so both marker disks merge into one seed and the flood returns one basin — one tablet counted where two passed.
  6. Saddle rule, T = saddle + 10% of the neck depth = 100 px + 0.10 × 67 px ≈ 107 px: marker disks of radius 167 − 107 = 60 px = 1.8 mm, so two disks on the center line span 3.6 mm against 8 mm of center distance — 4.4 mm of margin, and two seeds survive.

Result. Two basins, two labels, two tablets; the watershed line lands on the two points where the edges cross.

Decision. At 20% overlap the half-peak rule under-counts by one part per touching pair. Read the saddle off the distance map (100 px here), set T about 10% of the neck depth above it (107 px here), and the split holds. Past 50% overlap the neck depth falls to 23 px in this example and edge blur moves the crossing point between frames — that is an imaging problem, not a threshold problem.

Where the half-peak rule stops working

Solving saddle = 0.5 × peak for the overlap gives o = r × (2 − √3) ≈ 0.27 r: equal round parts separate on the textbook rule while overlap stays under about 13% of the diameter. Past that line, seed from the saddle. The table recomputes the example tablets — peak 167 px, half-peak 83 px — across the overlap range:

Overlap Saddle Half-peak rule (83 px) Seed threshold to set
0.5 mm (5%) 52 px splits cleanly keep 83 px
1 mm (10%) 73 px splits cleanly keep 83 px
2 mm (20%) 100 px seeds merge, one part counted 107 px
4 mm (40%) 133 px seeds merge 136 px, neck depth down to 34 px

Same tablets, same 0.030 mm/px scale; other shapes and scales recompute from saddle = sqrt(o × (r − o/4)). The pattern to remember: the saddle climbs fast — at 40% overlap it reaches 80% of the peak, and the split hangs on a 34 px sliver.

When watershed is the wrong tool

A single elongated part — a key, a banana, a connector housing — can carry two distance-transform peaks, and each peak floods its own basin: one part split into two labels. The bench test costs nothing: image one part alone, and if it yields more than one seed, smooth the distance map with a small Gaussian, merge basins whose common wall is short against the part size, or seed from known geometry. Rough edges raise the same false peaks, and the fixes are identical.

Contact without overlap is the easy opposite: two tablets kissing at a tangent have a saddle near 0 px and separate at almost any threshold, so the depth cost comes from overlap area, not from contact. And when the neck is only a pixel or two deep because the scale is too coarse, no threshold rescues it — the optics need the next section. Characters suffer the same failure, and the touching-letter segmentation fix applies the same neck logic.

How many pixels the neck needs

At 20% overlap the saddle sits at 0.60 × peak, since sqrt(0.4 × 0.9) = 0.60, so the peak sets everything. Keep the object radius at 50 px or more: the neck is then 30 px deep, an order of magnitude above the 1–2 px of edge blur a focused lens leaves, and seeds stop flickering between frames. For the 10 mm tablets that sets the coarsest useful scale at 5 mm ÷ 50 px = 0.10 mm/px — a 100 px part — and the example's 0.030 mm/px sits three times inside the budget. Working backward from your field of view, the pixel count follows from those two numbers:

Camera resolution for the smallest feature
Units
Pixels needed
–
Sensor size
– MP
Smallest sensor that fits
–
Pixel on the part
– mm
Pixels across the feature, that sensor
–

Pixels needed = field / feature × pixels per feature on each axis (field incl. margin). The lens must resolve the pixels too: Nyquist = 1000 / (2 × pixel µm) lp/mm.

Find cameras with enough pixels Open the full calculator

Where to run it: smart camera or vision controller

One camera, one stopped part, one count: the whole chain runs on the device. A smart camera carries threshold, distance transform, watershed and blob logic in its housing, which is the entire cell for 5 MP class counting jobs:

Smart cameras & vision sensorsHikrobot MV-SC3050XC-16M-ATN-H 5 MP color vision sensorMV-SC3050XC-16M-ATN-H
Interface
Fast Ethernet
Resolution
5 MP
Sensor format
1/1.45"
Shutter
Global
$1,809.00View part

When the count drives a reject gate across several cameras, move the flood to a vision controller that keeps camera connections, lighting channels and decision logic in one deterministic host.

Vision controllersHikrobot MV-VC5525-256G70-NN VC5000 Vision Controller, 12th-I5, 32GB+256G SSD, Supported PCIEMV-VC5525-256G70-NN
LAN ports
5+ LAN ports
USB 3 ports
3-4 USB 3 ports
PoE
PoE camera ports
Light outputs
8 built-in channels
$4,479.00 · quotedView and quoteBuild a system with it

The imager side of the example stays modest: the LS-CMR-0166 in the area scan camera range reads 4000 × 3000 at 30 fps over USB3.

Area scan cameras12 MP USB3 area scan camera, IMX226, 1/1.7" rolling shutter, 30 fps, 4000 × 3000 resolution (LS-CMR-0166)LS-CMR-0166
Sensor format
1/1.7"
Interface
USB3
Resolution
12 MP
Shutter
Rolling
$560.00View partBuild a system with it

Pre-flight checks before trusting the count

  • Lock the threshold first: watershed inherits every threshold error, so fix exposure and choose a segmentation that survives part variation — Otsu in Lab color or local mean adaptive thresholding.
  • Measure the saddle once on your worst touching pair and set T = saddle + 10% of the neck depth.
  • Run the seed-count sanity check: a single part alone must give exactly one seed.
  • Check the scale: object radius at 50 px or more for 20%-class overlaps.
  • Keep a golden set of images with known part counts and re-run it after any lighting or optics change.

That is the whole decision: threshold cleanly, keep the neck tens of pixels deep, seed above the saddle, and n touching parts come out as n labels. To size the imager to your field of view, start at the camera finder or the camera configurator; the applications topic page gathers more worked inspection write-ups, and the contact page reaches the applications team when a neck refuses to resolve.

Questions

How much overlap between two parts can watershed segmentation separate?

For equal round parts, the flood splits them while the saddle value at the neck stays below your seed threshold; the classic half-peak seed rule covers overlap up to about 13% of the diameter, and a saddle-based threshold pushes the usable range toward half the diameter. Unequal sizes shift the saddle toward the smaller part, so measure the saddle from the distance map instead of computing it. Past roughly 50% overlap the neck is shallower than the edge blur and no threshold recovers it — change the imaging, not the code.

Why did watershed split one single part into two labels?

Elongated or dumbbell-shaped parts carry two or more distance-transform peaks, and every peak floods a basin of its own — the classic over-segmentation failure. Image a single part alone as the bench test: more than one seed means you need smoothing of the distance map, a merge of basins whose common wall is short against the part size, or seeding from known geometry. Fix the seeds, not the flood.

Can a smart camera run watershed, or is a vision controller required?

A single-camera cell that stops the part and counts runs the entire chain — threshold, distance transform, watershed, blob logic — on the device; catalog smart cameras reach 60 fps in this class. Move to a vision controller when several cameras feed one decision, when 12 MP images must clear a fixed cycle time, or when you want camera connections, lighting channels and logic in one deterministic host.

Does watershed need a binary image, or does it work on grayscale too?

The distance-transform recipe in this guide runs on a binary image, the standard form for solid parts on a conveyor. The same flooding works on any grayscale landscape: for gray images such as cells or fruit, invert the intensity so each object forms its own dome or valley, then seed the same way. Binary or gray, one marker per object with the threshold set from the saddle decides the split.