# How do you detect cracks in eggs with image processing?

> Black-hat and top-hat filters are mathematical morphology operations that highlight thin lines darker or lighter than their surroundings. Applied to each egg's shell, they bring out cracks as elongated lines. In Vigiovo, a line only counts as a crack if it is elongated, long and deep enough to run into the shell, and the egg is graded cracked when the total length reaches 0.8 of the radius.

Updated: 2026-10-09 · URL: https://vigiovo.com/en/resources/crack-detection-with-black-hat · Português: https://vigiovo.com/recursos/deteccao-de-trincas-black-hat.md

## Key takeaways

- Black-hat highlights lines darker than the surrounding shell; top-hat highlights lines that are lighter.
- The filters only bring out lines. The shape rules applied to each line decide whether it is a crack.
- In Vigiovo, a line counts as a crack when it is elongated (at least 5 times longer than wide), long and deep.
- Hairline cracks, invisible under normal light, are outside the reach of this method.

## What are black-hat and top-hat filters?

They are two [mathematical morphology](https://vigiovo.com/en/resources/glossary#mathematical-morphology) operations that compare each pixel with its neighbourhood. The [black-hat](https://vigiovo.com/en/resources/glossary#black-hat-top-hat) filter highlights lines that are darker than the background around them. The top-hat filter does the opposite and highlights lighter lines. Both use a reference shape, called a structuring element, which sets the thickness of the lines they pick out.

The idea is simple: a thin line is narrower than the reference shape, so it survives in the result, while the smooth areas around it are removed. For the mathematical background in more depth, see [mathematical morphology in inspection](https://vigiovo.com/en/resources/mathematical-morphology-in-inspection).

## Why do these filters find cracks in the shell?

A crack appears on the shell as a thin line, darker or lighter than the rest of the shell. Because the line is narrow, the filter picks it out without needing labelled examples: what matters is the shape of the defect, not how one particular egg looks.

The problem is that the shell also has other thin lines and shapes that are not cracks, such as round stains and the natural shading near the rim. That is why Vigiovo does not stop at the filter: it applies shape rules to the lines it finds. To understand how each egg's light level is estimated, see the [shading surface](https://vigiovo.com/en/resources/glossary#shading-surface).

## Which rules separate a crack from a stain?

A line only counts as a crack when it meets all three conditions below. Measurements are taken against the egg's [reference radius](https://vigiovo.com/en/resources/glossary#reference-radius), called r:

- **Elongated**: at least 5 times longer than wide.
- **Long**: at least 0.35 r.
- **Deep**: it reaches at least 0.4 r into the shell, instead of following the rim.

These rules separate cracks from round stains, which are not elongated, and from the natural shading at the rim, which does not run into the shell. A line that fails any of the three conditions does not count toward the egg.

## How does the total length become the egg's grade?

Vigiovo adds up the length of the lines that pass the rules. If the total reaches 0.8 r, the egg is graded cracked. The limits are fixed: the same image always gives the same result, and each decision can be checked against the measurements behind it.

**Vigiovo crack thresholds and their practical role (practical assessment)**

| Rule | Threshold in Vigiovo | Practical role |
| --- | --- | --- |
| Elongation | At least 5 times longer than wide | Separates cracks from round stains |
| Length | At least 0.35 r | Drops short scratches |
| Depth | At least 0.4 r into the shell | Drops shading at the rim |
| Total length | 0.8 r or more | Grades the egg as cracked |

These values are configuration settings, tuned during the calibration of each line with labelled eggs. They are not a legal standard or a regulatory requirement: they mark the point where the line decides a crack is large enough to change the egg's class.

## What can this approach not see?

The analysis only sees what is in the camera image. Hairline cracks, invisible under normal light, do not appear as lines and are outside the reach of the method. For those cases, [candling](https://vigiovo.com/en/resources/glossary#candling) or [acoustic crack testing](https://vigiovo.com/en/resources/acoustic-crack-testing) are the appropriate methods.

Lighting matters too. Strong glare can look like lines, and shadows can change the contrast of the shell. Diffuse, even lighting reduces that risk more than any parameter setting. Details on how Vigiovo measures the shell are on [how it works](https://vigiovo.com/en/how-it-works).

## Frequently asked questions

### Are black-hat and top-hat filters machine learning?

No. They are mathematical morphology operations with fixed rules and no training. Applied to the same image, they always produce the same result. In Vigiovo, the thresholds for length and depth set in the configuration decide whether an egg is cracked, not a trained model.

### Can these filters find hairline cracks?

Not reliably. Hairline cracks are too fine to show under normal light, so a surface camera does not see them. For those, candling or acoustic testing are the suitable methods.

### Is the 0.8 radius limit a legal standard?

No. The total crack length limit is a Vigiovo configuration value, tuned during the calibration of each line with labelled eggs. It is not a law or a regulation, and it should be checked against each establishment's own routine.

## 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

- [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.
- [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.
