System designHow-to

Crack segmentation in images: separating cracks from noise

How to segment thin cracks at any angle when a line-shaped structuring element fails: ridge filters, pruning by geometry and width, with a worked example.

LumaSpec Vision11 min read Share on LinkedIn

On this page 10 sections
  1. 01Why the line structuring element fails
  2. 02What a crack is, as a shape
  3. 03Step-by-step: segmentation that works at any angle
  4. 04Worked example with the arithmetic
  5. 05Choosing the camera for the job
  6. 06Common mistakes
  7. 07Verifying the result
  8. 08When the usual answer is wrong
  9. 09Pre-order checklist
  10. 10Questions

A crack rarely sits at a tidy 0 or 90 degrees, and a fixed line-shaped structuring element preserves only the directions it was built for. The result is familiar: cracks at odd angles get erased along with the noise, or the noise survives and the crack drowns in it. Gray level and gradient alone often cannot separate them either, because many surface marks are as dark and as steep as the crack you want. This guide is for machine-vision and inspection engineers who need to segment line-like defects at any orientation and then judge each candidate with something better than a brightness threshold. You will set up an orientation-independent ridge filter, prune the result with geometry rules that match what a crack actually is, and verify the result with measurements in pixels and millimetres.

Why the line structuring element fails

A line-shaped morphological opening keeps a structure only if the element fits inside it. With a horizontal element of length 9 pixels, a crack at 37 degrees and 1 pixel wide does not contain the element even once, so the opening removes it exactly as it removes a noise blob. Rotating the element through, say, 36 directions and keeping the maximum result works in principle, but it is slow and it still fails on curved cracks, where no single direction fits along the whole length.

The deeper problem is that morphology knows nothing about scale. A filter that preserves anything 1 pixel wide also preserves dust, scratches in the background and print marks. You need a filter whose response depends on shape over a region, not on a local fit.

What a crack is, as a shape

Write down the properties that make a crack a crack in your images. In almost every inspection task the list looks like this:

  • Thin: width from 1 to 10 pixels at working resolution, roughly constant or tapering along its length.
  • Long: length at least 5 to 20 times its width, often 50 times or more.
  • Connected: one continuous path; it does not fall apart into isolated pixels.
  • Locally straight: over any short window it has one dominant direction, even if the global path curves.
  • Darker than the surrounding surface, usually with a strong gradient across its width and a weak gradient along it.

Noise blobs violate most of these at once: they are short, round, isolated, and their gradient points in every direction. Edges of parts violate the thinness and the enclosure: an edge is long but one-sided, with a step profile rather than a dark line between two bright sides. These observations, not gray level alone, are what the filter and the pruning rules should test.

Step-by-step: segmentation that works at any angle

The pipeline below takes a grayscale image to a cleaned binary crack mask. It needs only standard image-processing operations available in most vision libraries and on x86-platform smart cameras.

  1. Fix the geometry of the image. Undistort with your lens calibration so that a pixel is the same size everywhere. Width and length measurements mean nothing on a distorted image.
  2. Remove slow background variation. Apply a large-kernel blur (kernel about 3 to 5 times the crack width) or a morphological closing with a disk larger than the crack width, then subtract it from the original. The crack, being thinner than the kernel, survives subtraction; uneven illumination does not.
  3. Compute a multi-scale ridge response. For each of two or three sigma values around half the crack width, compute the Hessian eigenvalues at every pixel and keep a Frangi-type response that is high when one eigenvalue is near zero and the other is strongly negative (dark line on bright background). Store the maximum response across scales. This is the orientation-independent replacement for the rotating structuring element: it responds to a dark ridge at any angle, including curved paths.
  4. Threshold the ridge response. Pick the threshold from a histogram of the response on a known-good part, not on the defective one. Anything above the good-part noise floor by a margin of 3 to 5 standard deviations is a candidate.
  5. Make it binary and connected. Threshold, then link the candidates with a light morphological closing (a 3 by 3 or 5 by 5 element) so a crack broken by speckle becomes one component. Now label the connected components.
  6. Prune by geometry. For each component compute length (maximum of the skeleton or of the principal axis extent), mean width (area divided by skeleton length), and elongation (length divided by width). Keep only components passing all three rules; typical cut-offs are length at least 10 times width, width at most your maximum crack width, and area at least a few times the minimum crack area you expect.
  7. Prune by position and context. Remove components that always appear in the same place on good parts: edge shadows, engraving, seam lines. A reference-image subtraction or a fixed exclusion mask handles these in one step.
  8. Measure and decide. On the surviving components, compute width, length and area in pixels, convert to millimetres with your pixel scale, and apply the acceptance rule. For how to turn these line results into pass or fail decisions at line speed, see the article on turning crack line results into decisions.
sigma ≈ 0.5 × crack width (pixels) mean width = component area / skeleton length elongation = length / mean width (keep if ≥ 10)

Worked example with the arithmetic

Suppose the narrowest crack you must catch is 0.2 mm wide, on a field of view 40 mm across imaged by a 6 MP camera with a 2448 by 2048 sensor, giving a pixel scale of about 40 / 2448 = 0.0163 mm per pixel. The crack is then 0.2 / 0.0163 ≈ 12 pixels wide, so you run the ridge filter at sigma values of 3, 6 and 9 pixels.

A candidate component covers 3,800 pixels with a skeleton length of 950 pixels. Its mean width is 3,800 / 950 = 4 pixels, or 4 × 0.0163 ≈ 0.065 mm. Its elongation is 950 / 4 ≈ 238, far above the cut-off of 10, so it passes on shape. A nearby blob of 60 pixels with a skeleton length of 10 gives a mean width of 6 pixels and an elongation under 2, so it is removed. A 0.1 mm minimum-width rule accepts the 0.065 mm candidate or rejects it depending on your spec, which is exactly the decision the measurement was for.

Choosing the camera for the job

The filter itself costs little compute; the pixels are what make it work. Target at least 3 pixels across the narrowest crack, and 5 or 6 if you need trustworthy width numbers. Work backwards from the field of view and that pixel count to the sensor resolution, using the camera resolution calculator.

For a bench setup where the parts come one at a time, a compact vision sensor is often enough, because threshold-and-prune pipelines run on the device. For multi-scale ridge filtering at line speed, or when the same image also feeds measurement or code reading, a GigE smart camera with more headroom is the safer pick.

Smart cameras & vision sensorsHikrobot MV-SC5060XM-08M-WBN SC5000X series Smart Camera, 6 MP, Mono, 8 mm focal length, M12-mountMV-SC5060XM-08M-WBN
Interface
GigE
Resolution
6-10 MP
$1,629.00View part
Smart cameras & vision sensorsHikrobot MV-SC3050XC-16M-ATN-H 5 MP color vision sensorMV-SC3050XC-16M-ATN-H
Interface
Fast Ethernet
Resolution
5 MP
$1,809.00View part

Both cameras above give you the resolution this pipeline needs; the mono sensor is the usual choice for crack work because color adds nothing to a dark-line filter and costs sensitivity. Where the inspection runs from a fixed station with simple logic, an all-in-one unit with its own light source shortens the build:

Smart cameras & vision sensors1.6 MP smart camera, 60 fps, 16 mm lens, white light (polarized, transparent light) (LS-SMC-0063)LS-SMC-0063
Interface
GigE
Resolution
Up to 3 MP
$1,110.00View part

If you are still sizing the optics, the lens choice follows the field of view and working distance; the lens focal length calculator gives the first estimate, and the smart cameras finder filters by interface, resolution and shutter so you can shortlist quickly. For a full walk-through of matching sensor, lens and part, see camera angle of view from calibration.

Common mistakes

  • Thresholding on the defective image. The threshold must come from good parts; on a defective image the crack itself sets the scale and the noise floor is guessed.
  • One sigma only. Cracks vary in width along their path; a single-scale filter fades where the crack widens and breaks the connection. Two or three scales fix this at modest cost.
  • Closing too aggressively. A large closing bridges the gap between a crack and a nearby mark, merging two rejections into one acceptance. Keep the closing element at about the crack width or smaller.
  • Judging by gray level alone. As you have probably seen, dark blobs score the same as dark cracks on brightness. Elongation and connectivity are the discriminators, not intensity.
  • Ignoring the lighting. If contrast between crack and surface is weak, the filter output is weak everywhere. The guide to fixing lighting before thresholding on glass shows how often the software was never the problem. For shiny surfaces, the article on dome, diffuse and polarizer lighting for metal pins covers the setups that give the crack a chance.

Verifying the result

Run the pipeline on a set of at least 20 known-good parts and count false positives per part. Then run it on parts with cracks you have marked by hand and check that every marked crack survives with its measured width within one or two pixels of the manual value. If false positives cluster in one image region, look there physically: a reflection, a seam or a texture change usually explains it, and an exclusion mask is the honest fix. To watch the binary mask live while you tune thresholds, the setup in live binary image preview for defect inspection saves hours of guessing.

When the usual answer is wrong

Some defects should not be segmented as cracks at all. Sub-pixel cracks narrower than about 1.5 pixels carry no reliable shape information, and the honest options are more resolution or a lighting change such as a low-angle or dark-field setup, which makes a shallow crack scatter light toward the camera; the industrial lights finder lets you filter for the right lighting type. Cracks under paint or coating may be invisible in visible light altogether, which is a wavelength question rather than a filtering one; the comparison of SWIR, NIR and visible imaging explains when another band helps. And if the defect is a scratch rather than a crack, the pipeline barely changes but the acceptance logic does; the article on scratch detection with dynamic thresholding covers that variant.

Pre-order checklist

  • Crack width in millimetres at the surface, and the narrowest value you must detect.
  • Required width-measurement accuracy, which sets the pixels-per-crack target.
  • Field of view and working distance, hence pixel scale and sensor resolution.
  • Part presentation: stationary, indexed or continuous motion, and the time budget per part.
  • Sample parts, good and defective, that the pipeline has never been tuned on.
  • Exclusion list: features that always appear and must be masked from the start.

If you want a second pair of eyes on the pixel budget before you order, send the field of view, crack width and cycle time through the quote form at business quotes or ask the team via the contact page. Every order includes free expert support, so the configuration can be checked before anything ships. To explore the camera side first, start at the machine vision cameras finder or jump into the system configurator.

Questions

Why does a line structuring element miss cracks at other angles?

A line-shaped structuring element responds only to structures aligned with its own direction. A crack at 37 degrees survives an erosion with a horizontal element just as a blob would, so the operation neither preserves the crack nor removes round noise. You would need one element per angle, typically 8 to 36 directions, which is exactly what a Hessian-based ridge or Frangi filter computes in one pass.

What sigma should I use for a ridge filter on cracks?

Match the sigma to the crack width: for a Gaussian-based Hessian, a sigma of roughly half the crack width in pixels gives the strongest ridge response. If cracks are 3 to 6 pixels wide, run the filter at sigma values of 1.5 and 3 and keep the maximum response. Multi-scale filtering costs two to four times the compute of a single scale.

How do I tell a real crack from a scratched or printed line after segmentation?

Geometry alone cannot always separate them, because a scratch is also long, thin and connected. Add context: check whether the line crosses a known feature, compare its gray level to a reference image of a good part, or measure its width profile, since scratches often have constant width while cracks taper. When geometry fails, fix the lighting rather than the algorithm; contrast problems are rarely solved in software.

What resolution do I need to measure crack width reliably?

Aim for at least 3 pixels across the narrowest crack you must detect, and 5 to 6 pixels if you need width accuracy better than about 20 percent. With a known pixel size, width uncertainty is roughly one pixel, so a 3-pixel-wide crack has about 30 percent measurement error. More pixels per crack beat any filtering trick.

Can this run on a smart camera without a PC?

Simple threshold plus geometry pruning runs on most smart cameras. Multi-scale ridge filtering is heavier: it fits within a PC-based or x86-platform smart camera pipeline, but a compact vision sensor will usually not sustain it at full frame rate. Estimate the budget: a 6 MP image at 60 nm-scale sigma over three scales is tens of operations per pixel, so check the camera's processor class before committing.