Measuring Brinell indentation diameters from images: checking the code on synthetic images
Sample code that measures Brinell indentation diameters with OpenCV and computes HBW, checked on synthetic images with known true values. This is a technical demonstration, not a measurement of real indentations.
Last re-run: 2026-11-05
Background
Brinell hardness requires reading two diameters of the indentation. Automatic image measurement can save reading time, but only if the code converts pixels to millimetres correctly, fits the contour and computes hardness correctly, and refuses to return a number when the image is unsuitable instead of silently giving a wrong one.
Input
Synthetic greyscale images generated by the script (1200 × 1200 pixels): a dark elliptical “indentation” on a light background with a blurred edge and Gaussian noise. Assumed calibration 0.005 mm/pixel; assumed test conditions F = 29420 N, D = 10 mm. The images and results can be regenerated with the script below.
Process
- Gaussian blur, automatic Otsu thresholding, then morphological opening to remove small specks.
- Fit an ellipse to the largest contour to get the two axis lengths in pixels, and convert to millimetres with the calibration.
- Check plausibility: does the contour touch the image border, do the axes differ by more than 5%, does the contour fill 97% of the fitted ellipse? If not, no value is returned.
- Compute HBW with the defining formula of ISO 6506-1 and check that the mean diameter d lies within 0.24D–0.6D.
Output
Known true values versus measured values
| True d1 / d2 (px) | Measured d1 / d2 (px) | d true → measured (mm) | HBW true → measured | Within valid range |
|---|---|---|---|---|
| 800 / 800 | 799.5 / 799.5 | 4.0000 → 3.9975 (-0.06%) | 228.8 → 229.1 (+0.13%) | Yes |
| 800 / 780 | 799.6 / 779.5 | 3.9500 → 3.9478 (-0.06%) | 234.9 → 235.2 (+0.12%) | Yes |
| 500 / 500 | 499.9 / 499.8 | 2.5000 → 2.4993 (-0.03%) | 601.6 → 601.9 (+0.05%) | Yes |
| 1000 / 990 | 999.4 / 989.4 | 4.9750 → 4.9719 (-0.06%) | 144.1 → 144.3 (+0.14%) | Yes |
| 400 / 400 | 399.9 / 399.9 | 2.0000 → 1.9995 (-0.03%) | 945.5 → 946.1 (+0.06%) | No |
Uneven illumination
- Grey-level difference 0: d = 3.9975 mm (-0.06%)
- Grey-level difference 40: d = 3.9978 mm (-0.05%)
- Grey-level difference 80: d = 3.9978 mm (-0.06%)
- Grey-level difference 120: Rejected: the contour touches the image border and fills only 84% of the fitted ellipse
How it was checked
Because the true values of synthetic images are known, they can be compared directly: across five sizes the diameter error was -0.06% to -0.03% and the HBW error +0.05% to +0.14%. The 400 / 400 pixel case gives d = 2.0 mm, below 0.24D = 2.4 mm, and the code correctly flags it as out of range. With a linear brightness gradient, grey-level differences of 0, 40 and 80 barely changed the result; at 120 the segmentation was no longer plausible and the code refused to return a value. Re-run on 5 November 2026 with OpenCV 5.0.0; the output matched the record in the repository line by line.
Limits
- Synthetic images lack the raised rims, scratches, reflections and lens distortion of real indentations; the results show that the code logic is correct, not how accurate it is on real samples.
- The calibration is assumed. Real measurements need the pixel size from a calibration scale and an uncertainty evaluation of the measuring system.
- The 5% and 97% plausibility thresholds are example values for synthetic images and must be re-set before use on real images.
- The measuring system was not verified according to ISO 6506-2, so this is not a compliant measurement method.