Explainer · Inspection technology

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 · 4 min read

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: 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
OperationGeneral effect on shapeWhere it appears in Vigiovo
ErosionShrinks the region and removes thin lines and dotsNot described separately on the technical page
DilationExpands the region and fills small gapsNot described separately on the technical page
OpeningRemoves noise, small specks and thin protrusionsMask cleanup and cutting out each egg, with a disk of about 1 r
ClosingFills small holes and gapsColour 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.

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 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 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. Nor does it separate touching eggs: that task belongs to watershed.

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

  1. Morphological Transformations, OpenCV documentation
  2. Como o Vigiovo classifica ovos sem IA / How Vigiovo grades eggs without AI, Vigiovo

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.

See how it works