# What is the distance transform, and what is it used for in egg inspection?

> The distance transform is a map in which each pixel of a shape holds its distance to the nearest edge. The centre of a single egg is the highest point, and two touching eggs form two peaks joined by a shallow valley. Vigiovo uses this map to find one peak per egg before running watershed.

Updated: 2026-10-09 · URL: https://vigiovo.com/en/resources/distance-transform-explained · Português: https://vigiovo.com/recursos/transformada-de-distancia.md

## Key takeaways

- Each pixel of the mask holds its distance to the nearest edge: edge pixels are near zero and central pixels are higher.
- The centre of an egg becomes the highest point of the map, and the split starts from there.
- Two touching eggs form two peaks joined by a shallow valley, which lets them be separated.
- The map is only as good as the mask: holes and ragged edges move the peaks.

## What is the distance transform?

The distance transform is an image derived from a binary mask, in which each pixel of the egg stores its distance to the nearest background pixel. Pixels on the edge are close to zero, and pixels further inside are higher. Picture the egg from above as an island: the further from the shore, the higher the land.

For Vigiovo, the transform does not measure dirt or cracks. It measures shape: where the middle of each region in the mask lies. That is the information [watershed](https://vigiovo.com/en/resources/watershed-for-touching-eggs) needs to separate touching eggs. For the short definition, see [distance transform in the glossary](https://vigiovo.com/en/resources/glossary#distance-transform).

## Why does the centre of an egg become a peak?

In a single egg, the point farthest from the edge lies near the centre, so that is the highest point on the map. Because an egg is elongated, the top is not a sharp tip: the map slopes gently towards the two ends.

This matters because the peak is a stable position. A few pixels of noise on the edge barely move where the centre lies, which is why the peak works as a seed for watershed. The peak only appears if the mask has the shape of an egg, though: large holes or a ragged outline create false peaks.

**Reading the distance map (practical judgement, not a rule)**

| Region of the map | Value | What it means |
| --- | --- | --- |
| Egg edge | close to zero | pixels touching the background |
| Centre of a single egg | the highest value of that egg | the point farthest from the edge |
| Meeting point of two eggs | lower than the two peaks | the neck between the two eggs |

The exact value of each pixel depends on the camera's resolution. That is why Vigiovo expresses its limits as multiples of the egg's [reference radius](https://vigiovo.com/en/resources/glossary#reference-radius) rather than as fixed pixel counts.

## What happens when two eggs touch?

Two touching eggs form a single blob shaped like a figure eight. On the distance map, each egg gets its own peak, and the two peaks are joined by a valley, the neck where the eggs meet. The neck between two eggs is shallow, which is why the split is stable on Vigiovo.

Not every blob goes through this step. Vigiovo computes the transform only on blobs larger than one egg; a single egg skips the split. The peaks are picked with [h-maxima](https://vigiovo.com/en/resources/glossary#h-maxima), which ignore small bumps so that one egg is not split in two. The details of watershed are in [watershed for touching eggs](https://vigiovo.com/en/resources/watershed-for-touching-eggs).

## How does Vigiovo use the distance transform?

Vigiovo computes the transform on the mask of each large blob, finds one peak per egg with h-maxima obtained by morphological reconstruction, and then runs watershed from those peaks. The mask comes from the [HSV colour segmentation](https://vigiovo.com/en/resources/hsv-colour-segmentation-for-eggs) step, which separates the shell from the conveyor rollers.

Before the transform is computed, the mask has already been cleaned by opening and closing, and small blobs have been dropped. The order matters: a mask with holes creates peaks that do not correspond to eggs. The logic of the cleaning is in [mathematical morphology in inspection](https://vigiovo.com/en/resources/mathematical-morphology-in-inspection).

## What are the limits of the distance transform?

The transform sees only the shape of the mask, not the shell itself. If the mask is poor, because of shadows, glare or pieces of belt that got into it, the map and its peaks are wrong. A badly deformed egg can also have more than one peak, or no clear peak at all.

That is why Vigiovo marks an egg as unverified, and gives it no grade, when its shape is still concave after the split, instead of grading an uncertain shape. That path is described in [watershed for touching eggs](https://vigiovo.com/en/resources/watershed-for-touching-eggs). For what a camera sees and what it misses, see [candling or computer vision](https://vigiovo.com/en/resources/candling-vs-computer-vision).

## Frequently asked questions

### Does the distance transform measure the size of an egg?

Not directly. It measures the distance to the edge in pixels, which depends on the camera's resolution. For that reason Vigiovo expresses its limits as multiples of the reference radius of the egg, estimated from isolated blobs on the first frame.

### Why does watershed need the distance transform?

Watershed needs one seed in each egg. The transform gives a peak at the centre of each egg, and those peaks serve as seeds. Without them, watershed does not know where each egg starts, and two touching eggs can be treated as one.

### Does the distance transform find cracks?

No. It works on the shape of the mask, not on the fine lines of the shell. Cracks are found with black-hat and top-hat filters, which highlight lines darker or lighter than their surroundings. A line only counts as a crack if it is elongated, long and deep.

## Sources

- [Image Segmentation with Watershed Algorithm](https://docs.opencv.org/4.x/d3/db4/tutorial_py_watershed.html), OpenCV documentation
- [Como o Vigiovo classifica ovos sem IA / How Vigiovo grades eggs without AI](https://vigiovo.com/como-funciona), Vigiovo

## Read next

- [Watershed: separating touching eggs in an image](https://vigiovo.com/en/resources/watershed-for-touching-eggs.md) (Explainer, 2026-10-09): How the watershed algorithm separates touching eggs in an image, using the distance transform, one seed per egg and the boundary between each pair, as Vigiovo does.
- [Mathematical morphology in visual inspection](https://vigiovo.com/en/resources/mathematical-morphology-in-inspection.md) (Explainer, 2026-10-09): What mathematical morphology is, how erosion, dilation, opening and closing clean an egg mask, and how Vigiovo uses these operations in inspection.
- [Candling vs computer vision: what each one detects](https://vigiovo.com/en/resources/candling-vs-computer-vision.md) (Comparison, 2026-10-09): Candling compared with camera inspection in egg grading: which defects each detects, speed, record keeping and why the two methods work best together.

## How Vigiovo helps

Vigiovo grades eggs on the conveyor as clean, dirty, cracked or unverified, in real time, with deterministic computer vision: every decision comes with the measurements behind it, on an ordinary CPU. https://vigiovo.com/en/how-it-works.md

4 min read.
