A Brinell hardness result is not read directly from the testing machine. The diameter of the indentation is measured first and then entered into a formula. The diameter is usually read by an operator through a measuring microscope, which takes time and depends on individual judgement. Capturing the indentation with a camera and letting a program assist the measurement is an improvement many labs consider.

This article describes a basic approach to assisted Brinell indentation measurement with Python and OpenCV. It focuses on four questions: how pixels are converted to millimetres, how a diameter is obtained from a contour, how errors carry over into the hardness value, and how the program can tell that its own result is not trustworthy.

Standards referred to

  • ISO 6506-1:2014 Brinell hardness test — Test method (3rd edition; confirmed in 2025 according to the ISO website)
  • ISO 6506-2:2017 — Verification and calibration of testing machines
  • ISO 6506-3:2014 — Calibration of reference blocks

Requirements are quoted from the public sample pages of these standards; use the full text for actual testing.

About the numbers: sections 1, 2 and 4 contain values calculated from the formula. Section 5 reports results of running the code on synthetic, computer-generated images. None of them are measurements of real indentations. Measurements for formal reports or certificates must use a measuring system verified in accordance with ISO 6506-2.

1. How Brinell hardness is calculated

In ISO 6506-1, a hardmetal ball of diameter D is pressed into the test piece with a test force F, and the indentation is measured after the force is removed. Hardness is calculated as follows (F in N, D and d in mm):

HBW = 0.102 × 2F / ( π · D · ( D − √(D² − d²) ) )

The constant 0.102 equals 1/9.80665, the conversion from kgf to N. d is the arithmetic mean of two diameters d1 and d2 measured in approximately perpendicular directions:

d = (d1 + d2) / 2

The test force should be chosen so that d lies between 0.24D and 0.6D. If it does not, ISO 6506-1 requires the ratio d/D to be stated in the test report. The result is rounded to three significant figures.

Calculated example: with D = 10 mm, F = 29 420 N (HBW 10/3000) and d = 4.00 mm, the formula gives 228.8, reported as 229 HBW 10/3000.

2. Why diameter errors matter so much

Differentiating the formula with respect to d shows that, within 0.24D to 0.6D, the relative error in hardness is roughly 2.0 to 2.25 times the relative error in the diameter. Calculated with the example above (unrounded intermediate values):

Diameter d (mm) HBW Change from the first row
4.00 228.8 —
4.01 (+0.25%) 227.6 −0.52%
4.02 (+0.5%) 226.5 −1.04%
4.04 (+1%) 224.1 −2.06%

ISO 6506-2:2017 allows the diameter measuring system a maximum error of 0.5% of the diameter (and requires a resolution that allows the diameter to be estimated to within ±0.5%). From the table, that alone is worth about 1% in hardness. An image system should be designed so that its own error is clearly smaller than this.

3. Measurement workflow

3.1 Image acquisition

  • Keep magnification, focus and illumination fixed. ISO 6506-1 asks for an evenly illuminated field of view; ISO 6506-2 requires the type of illumination to stay unchanged during verification with reference indentations.
  • The whole indentation should be in the field of view, with enough undeformed surface around it to make segmentation easier.
  • Record the test piece, test force, ball diameter and magnification for every image.

3.2 Converting pixels to millimetres (calibration)

At the same magnification, photograph a scale of known length such as a stage micrometer and calculate the length per pixel (mm/px). Points to note:

  • Recalibrate for each magnification and whenever the lens or camera changes. ISO 6506-2 calibrates the measuring system for every objective lens, in two perpendicular axes, at no fewer than four intervals.
  • Lens distortion is larger towards the edges of the field. Keep the indentation near the centre and, if necessary, correct distortion following OpenCV's camera calibration method.
  • The calibration scale has its own uncertainty, which belongs in the result as well.

3.3 Segmentation and diameter

A common sequence is: greyscale → smoothing → thresholding → morphological opening to remove small specks → largest contour → ellipse fit. The two axes of the ellipse are perpendicular and serve as approximations of d1 and d2. The function then checks whether the segmentation is plausible before returning a number.

import cv2
import numpy as np


def measure_indentation(path_or_img, mm_per_px):
    """Return (d1, d2, d) in mm. Assumes the indentation is darker than its surroundings."""
    img = path_or_img if isinstance(path_or_img, np.ndarray) else cv2.imread(path_or_img, cv2.IMREAD_GRAYSCALE)
    if img is None:
        raise FileNotFoundError(path_or_img)
    blur = cv2.GaussianBlur(img, (5, 5), 0)
    _, mask = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, np.ones((5, 5), np.uint8))
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    if not contours:
        raise ValueError("no indentation contour found")
    c = max(contours, key=cv2.contourArea)
    if len(c) < 5:
        raise ValueError("contour too small to fit an ellipse")
    (_, _), (axis1, axis2), _ = cv2.fitEllipse(c)  # full axis lengths in pixels
    problems = plausibility(c, mask.shape, axis1, axis2)
    if problems:
        raise ValueError("segmentation not plausible: " + "; ".join(problems))
    d1, d2 = axis1 * mm_per_px, axis2 * mm_per_px
    return d1, d2, (d1 + d2) / 2


def plausibility(contour, shape, axis1, axis2, max_axis_diff=0.05, min_fill=0.97):
    """Reasons to distrust a segmentation instead of silently returning a number."""
    problems = []
    x, y, w, h = cv2.boundingRect(contour)
    if x <= 1 or y <= 1 or x + w >= shape[1] - 1 or y + h >= shape[0] - 1:
        problems.append("contour touches the image border")
    if abs(axis1 - axis2) / max(axis1, axis2) > max_axis_diff:
        problems.append(f"axes differ by more than {max_axis_diff:.0%}")
    fill = cv2.contourArea(contour) / (np.pi * axis1 * axis2 / 4)
    if fill < min_fill:
        problems.append(f"contour fills only {fill:.0%} of the fitted ellipse")
    return problems


def brinell_hbw(force_n, ball_d_mm, d_mm):
    """HBW per ISO 6506-1. Returns (hbw, in_range); in_range is False if d is outside 0.24D-0.6D."""
    D, d = ball_d_mm, d_mm
    hbw = 0.102 * 2 * force_n / (np.pi * D * (D - np.sqrt(D**2 - d**2)))
    return hbw, 0.24 * D <= d <= 0.6 * D

The thresholds in plausibility (5% axis difference, 97% fill) are starting points for synthetic images, not values from a standard; set them from your own validation data. Otsu thresholding assumes a clear brightness difference between the indentation and the background. With rough or scratched surfaces or uneven lighting, improve the imaging first, or switch to edge detection, local thresholding or similar methods.

4. Main sources of error

Source Effect Countermeasure
Calibration A bias in mm/px scales every diameter Re-check regularly against a reference scale and record the calibration date
Indentation edge With a raised rim or sink-in, the "edge" itself is hard to define Use one consistent edge rule and compare with manual readings
Illumination and focus Shadows and blur change the segmented area Fix the illumination and check focus before capturing
Threshold and parameters Parameter changes systematically enlarge or shrink the contour Fix the parameters and record their version; do not tune them per sample
Pixel quantisation Reading error of about one pixel per edge Increase magnification so the diameter spans enough pixels

Calculated pixel budget: a 4.00 mm indentation at 0.005 mm/px spans 800 px, so one pixel is 0.125% of d. A 2.4 mm indentation (0.24D with a 10 mm ball) at 0.01 mm/px spans only 240 px, and one pixel is already 0.42%, close to the 0.5% allowed by ISO 6506-2 for the whole measuring system.

5. What the code does on synthetic images

To check that the code above behaves as described, it was run on computer-generated images: a dark ellipse with a blurred edge on a light background with random noise, at an assumed calibration of 0.005 mm/px (script: brinell_synthetic_check.py, OpenCV 5.0.0).

Synthetic indentation (px) Diameter error HBW error
800 × 800 −0.06% +0.13%
800 × 780 −0.06% +0.12%
500 × 500 −0.03% +0.05%
1000 × 990 −0.06% +0.14%
400 × 400 (d = 0.2D, flagged as out of range) −0.03% +0.06%

With a linear brightness gradient across the image, the result stayed within 0.06% up to a gradient of 80 grey levels. At 120 grey levels the Otsu threshold split the image along the gradient; without the plausibility check the function returned a diameter 39% too large. With the check, the result is rejected because the contour touches the image border and fills only 84% of the fitted ellipse.

What this shows, and what it does not:

  • It shows that the pixel-to-millimetre conversion, the ellipse fit and the formula are implemented correctly, and that the plausibility check catches one typical failure.
  • It does not show how accurate the method is on real indentations. Synthetic images have a perfectly defined edge; real indentations have raised rims, sink-in, scratches, inclusions and lens distortion. Accuracy on real samples can only come from the validation in section 6.

6. How to validate the results

Before the program is used for routine measurement, carry out at least these comparisons:

  1. Against manual measurement: have an experienced operator read the same indentations with a measuring microscope, and calculate the mean difference (bias) and its spread.
  2. Reference indentations and blocks: ISO 6506-2 verifies the measuring system by measuring reference indentations (mean diameter within 0.5% of the certified value, at least four indentations per ball size and objective). Hardness reference blocks calibrated in accordance with ISO 6506-3 check the whole procedure, not just the image program.
  3. Repeatability: capture and evaluate the same indentation several times to see how much the result varies.
  4. Failure detection: the program should report failed segmentation, d outside the valid range or an excessive difference between the two axes, instead of silently returning a number.

For more on validation and error evaluation, see the article "Error and validation in research image measurement" on this site.

7. Scope and limitations

  • An image program can assist reading, reduce repetitive work and keep a traceable image record, but it does not replace a measuring system verified in accordance with ISO 6506-2.
  • For materials with poor surface condition or irregular indentations, automatic segmentation may be unreliable and needs human confirmation.
  • Conversions to other hardness scales or to tensile strength are approximate relationships; the method in this article cannot replace the corresponding tests.