# What is mathematical morphology in visual inspection of eggs?

> Mathematical morphology is a set of operations that change the shape of regions in an image, such as erosion, dilation, opening and closing. It involves no training: it applies fixed rules to a mask of pixels. In Vigiovo, opening removes noise and thin protrusions, and closing fills small holes in the mask.

Updated: 2026-10-09 · URL: https://vigiovo.com/en/resources/mathematical-morphology-in-inspection · Português: https://vigiovo.com/recursos/morfologia-matematica-na-inspecao.md

## Key takeaways

- Mathematical morphology is a set of shape operations with fixed rules, applied to a mask of egg or non-egg pixels.
- Opening (erosion followed by dilation) removes noise and thin protrusions; closing (dilation followed by erosion) fills small holes.
- Vigiovo opens the mask with a disk of about 1 r to remove thin appendages and clips each egg to its fitted ellipse.
- Morphology cleans shapes but does not separate touching eggs or measure colour; those tasks belong to watershed and the Lab colour space.

## What are erosion and dilation?

Erosion shrinks a region: a pixel stays part of it only if its whole neighbourhood, inside a small disk, is also part of it. Thin lines and isolated dots disappear first. Dilation does the opposite: each pixel of the region spreads into its neighbourhood, and the shape grows.

The size and shape of the disk, called the structuring element, decide what each operation removes. A larger disk affects larger structures, and a small disk only tiny details. That is the central idea of [mathematical morphology](https://vigiovo.com/en/resources/glossary#mathematical-morphology): the operation acts on shapes the size of the disk.

## What do opening and closing do?

Opening is an erosion followed by a dilation. It removes small specks and thin protrusions and keeps the main body of the region almost intact. Closing is the reverse: a dilation followed by an erosion. It fills small holes and gaps inside the region without changing the outer contour much.

The table shows the general effects of each operation and where they appear in Vigiovo's inspection.

**General effects of the operations and their use in Vigiovo**

| Operation | General effect on shape | Where it appears in Vigiovo |
| --- | --- | --- |
| Erosion | Shrinks the region and removes thin lines and dots | Not described separately on the technical page |
| Dilation | Expands the region and fills small gaps | Not described separately on the technical page |
| Opening | Removes noise, small specks and thin protrusions | Mask cleanup and cutting out each egg, with a disk of about 1 r |
| Closing | Fills small holes and gaps | Colour mask cleanup, with noise removal |

In practice, Vigiovo uses opening and closing on the colour mask and then opening on each egg. The table describes the general effect of each operation; the last column only reflects what the system's technical page states.

## How does Vigiovo clean the mask of each egg?

Cleaning happens in two layers. First, the whole mask goes through opening and closing to remove noise and close small holes. Components with fewer than 150 pixels are discarded. Then each egg is treated on its own, in three steps:

- the holes in the egg are filled;
- the region is opened with a disk of about 1 r, which removes thin appendages;
- the region is clipped to the ellipse fitted to the egg, which removes shadows between neighbouring eggs.

The order matters. The global cleanup removes noise before each egg is isolated, and the per-egg cleanup trims what is left between them. For what happens once each egg is isolated, see [separating touching eggs](https://vigiovo.com/en/resources/watershed-for-touching-eggs).

## Why is the disk measured in multiples of the egg radius?

Because the egg's size in pixels depends on the camera and the distance. A disk of about 1 r keeps the same relationship to the egg with any camera. That is why the same configuration works with cameras of different resolutions. The [reference radius](https://vigiovo.com/en/resources/glossary#reference-radius) is the ruler that makes this possible.

The same principle applies to cracks. A line only counts as a crack if it is at least five times longer than it is wide and at least 0.35 r long. Lines below these limits are not counted, whatever the camera.

## Does morphology also help find cracks?

Yes, through the [black-hat and top-hat](https://vigiovo.com/en/resources/glossary#black-hat-top-hat) filters, which are built on morphological operations. Black-hat highlights lines darker than their surroundings, and top-hat highlights lines lighter than them. A crack then appears as a highlighted line, not as part of the natural shading of the shell.

Morphology does not solve everything. It works with shape and contrast, so on its own it cannot tell dirt from natural colour variation; for that Vigiovo measures colour in the [Lab space](https://vigiovo.com/en/resources/glossary#lab-colour-space). Nor does it separate touching eggs: that task belongs to [watershed](https://vigiovo.com/en/resources/watershed-for-touching-eggs).

## Frequently asked questions

### Is mathematical morphology artificial intelligence?

No. It is a set of mathematical operations with fixed rules on shapes, with no model trained on images. The same image and the same configuration always give the same result, and each step can be described and checked.

### What is the difference between opening and closing?

Opening, which is erosion followed by dilation, removes noise, small specks and thin protrusions. Closing, which is dilation followed by erosion, fills small holes and gaps inside a region. Both use the same kind of disk, but in opposite orders.

### Can morphology separate two touching eggs?

Not reliably. It cleans shapes, but it does not create a boundary between two objects that touch. That separation is done by watershed, starting from one peak per egg found with the distance transform.

## Sources

- [Morphological Transformations](https://docs.opencv.org/4.x/d9/d61/tutorial_py_morphological_ops.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.
- [Lab colour space for detecting shell stains](https://vigiovo.com/en/resources/lab-colour-space-for-shell-stains.md) (Explainer, 2026-10-09): How the Lab colour space separates lightness from colour to find stains of a different colour on the shell, and why Vigiovo excludes glare from that measurement.
- [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.
