A color-difference defect — a red stain on a pale housing, a tint shift on a ceramic surface — is almost always a brightness difference as well, so you can segment it from a grayscale image and skip color processing entirely. The sequence that works: light the part so the stain reads clearly darker or lighter in gray, measure the gray gap between stain and background, and apply one fixed threshold. Bottom-hat morphology and histogram equalization are rescue tools for uneven backgrounds, not the starting point. This walkthrough gives the numbers that decide each stage — the gray-level gap you need, the pixels across the defect, the lens and working distance that deliver them — with the arithmetic shown, and ends with the checks to run before ordering parts.
Why a color defect shows up in gray
Grayscale conversion is a weighted sum of the color channels. The BT.601 luma weights are:
Put numbers on it. A well-lit pale surface reads around (235, 235, 235), which is 235 in gray. A red stain reading (200, 40, 30) gives Y = 0.299 × 200 + 0.587 × 40 + 0.114 × 30 = 59.8 + 23.5 + 3.4 ≈ 87. The gap is 235 − 87 = 148 gray levels — far more than any threshold needs. Saturated stains carry little green and blue against a pale background, and green alone carries 58.7 percent of the luma, so nearly every strongly colored defect drops in gray. That is why a single threshold extracts the whole stained region, pixel by pixel.
The exception is a tint change that holds luma constant — a warm-gray to cool-gray shift, or two stains with the same Y. No gray threshold can separate those; you need a color space, and the standard route is automatic threshold segmentation in Lab color with Otsu's method or HSV segmentation on a saturated background. A monochrome camera with an optical filter widens the gray gap further: a green bandpass pushes a red stain toward black while a white surface stays bright.
The decision: threshold first, morphology only when the background breaks it
Commit to this order: one fixed global threshold on the raw gray image; add a bottom-hat transform when background shading makes that threshold drift; go to color only for equal-luma defects or when the defect's identity must be classified.
| Method | Use it when | It fails when | Cost |
|---|---|---|---|
| Fixed global threshold | Even background, gray gap of 30+ levels | Shading, texture, drifting ambient light | One pass, the fastest |
| Bottom-hat + fixed threshold | Background shifts more than ~20 levels across the field; defect smaller than the structuring element | Defect larger than the element; heavy texture | A closing, then a subtraction |
| Histogram equalization + auto threshold | Defect pixels under ~1 percent of the image; dim scenes for display | Never adds separation; amplifies noise | One lookup-table pass |
| Color-space segmentation | Equal-luma tints; defect colors must be told apart | Needs a color camera and more tuning | Highest |
The bottom-hat (black top-hat) transform is the morphological closing of the image minus the image itself: T_b(f) = f • b − f, where • is closing with structuring element b — see the top-hat transform reference. Closing fills any dark feature smaller than b, so the subtraction leaves a near-zero background with the stain lifted out as a bright blob, and a plain fixed threshold then works even when the raw background drifts. Vision libraries ship this as a bottom-hat operator (for instance gray_bothat) alongside a histogram-equalization operator (equ_histo_image).
Histogram equalization earns its caution: it is a monotonic remap of gray levels, so it cannot create separation that is not already there. A single threshold applied after equalization selects exactly the same pixels as a single threshold at the corresponding level before it. What it changes is where automatic thresholds land when the histogram is dominated by background, and how the image looks on screen. Treat it as a setup and display aid, not a detection method. When the background itself is uneven, shading correction or the bottom-hat route is the real fix.
Lighting that turns the tint into gray levels
The stain only exists in the gray image if the lighting puts it there. Three settings decide that:
- Geometry. On a flat part, a diffused ring light overfilling the field of view by about 1.5 times gives the most even background; ring light selection by angle, size and distance covers the sizing. Curved or shiny surfaces need a dome or cross-polarization, or the specular highlight will swallow the stain — lighting geometry for image contrast maps the choices.
- Light color. Start with white light: it shows every defect type and keeps the image honest for documentation. If the measured gray gap is short of 30 levels, a light color chosen against the stain widens it — green light drives a red stain toward black on a white surface. Part and lighting color to maximize grayscale contrast gives the pairing rules.
- Locked settings. Fix exposure, gain and white balance before tuning anything in software. Auto exposure re-balances the histogram whenever part color changes and quietly moves the threshold; ambient leaks do the same, so close the enclosure — ambient light variation control lists the usual entry points.
For the station built in the worked example below, a diffuse ring light is the starting choice:
- Size
- 100-200 mm
- Type
- Diffused
- Angle
- 30°
- Voltage
- 24 V
Rule-based threshold and blob tools of this kind run on the device on many smart cameras, with no PC in the loop; read AI versus rule-based smart camera inspection before picking the platform.
Give the defect enough pixels
A threshold can only segment a stain the sensor resolved. Commit to at least 4 pixels across the smallest defect you must catch; at 3 pixels the measured blob area swings by roughly a quarter, and below that detection is luck.
Example arithmetic: a 0.1 mm stain at 4 pixels across needs 0.1 / 4 = 0.025 mm per pixel (25 µm). A 60 × 45 mm part with margin needs a 70 × 52.5 mm field, so the sensor must deliver 70 / 0.025 = 2800 pixels across and 52.5 / 0.025 = 2100 vertically: 2800 × 2100 ≈ 5.9 MP. A 12 MP sensor gives comfortable margin at that field.
Worked example: red stain on a 60 mm part
The full set-up, every number shown:
- Field of view: 70 × 52.5 mm (60 × 45 mm part plus mounting tolerance).
- Sensor: a 1/1.7-inch, 12 MP class sensor, 4000 × 3000 pixels, 7.5 mm wide, 1.9 µm pixels.
- Pixel size on the object: 70 mm / 4000 px = 17.5 µm. The 0.1 mm stain spans 0.1 / 0.0175 = 5.7 pixels — above the 4-pixel floor with margin.
- Magnification: m = sensor width / field = 7.5 / 70 = 0.107.
- Lens: at 150 mm working distance, f = WD × m / (1 + m) = 150 × 0.107 / 1.107 = 14.5 mm, so fit a 16 mm C-mount lens. At 16 mm the working distance becomes f × (1 + m) / m = 16 × 1.107 / 0.107 ≈ 165 mm.
- Aperture and depth of field: the part height varies ±2 mm, so you need about 4 mm of depth of field. With c = 2 pixels = 3.8 µm and N = 5.6: DOF = 2 × 5.6 × 0.0038 × 1.107 / 0.107² ≈ 4.1 mm. Set f/5.6; if the part also tilts, f/8 gives 5.9 mm.
- Exposure: on a stationary station, 5 ms with the ring light driven continuously — nothing moves. On a conveyor at 50 mm/s, 5 ms smears the image by 50 × 0.005 = 0.25 mm = 14 pixels, unusable; strobing the light for 100 µs cuts motion to 50 × 0.0001 = 5 µm ≈ 0.3 pixels.
Run the same numbers for your own field with the lens focal length calculator:
For a stationary station, a 12 MP class camera covers this field:
- Sensor format
- 1/1.7"
- Interface
- USB3
- Resolution
- 12 MP
- Shutter
- Rolling
Rolling shutter suits a stationary part; on moving product, pick a global-shutter model from the same finder.
The segmentation procedure, step by step
- Lock the imaging. Fix exposure, gain and white balance, close the enclosure, and capture at least 10 images of known-good parts and 10 with the defect, in production lighting.
- Measure the gray gap. Draw one region of interest on the stain and one on clean background, and read both means. In the example numbers above: stain ≈ 87, background ≈ 235.
- Commit the threshold. Set it at the midpoint: (87 + 235) / 2 = 161. A fixed midpoint beats an automatic threshold whenever both populations are tight, because scene content cannot drag it.
- Threshold and clean. Apply the single global threshold, fill holes, and discard blobs under half the expected defect area — the 5.7-pixel stain covers π × 2.85² ≈ 26 pixels, so the floor is 13 pixels.
- Add bottom-hat when shading wins. If background means shift more than about 20 levels between corners, run the bottom-hat first with a flat disk structuring element 1.5–2 times the stain width (here 5.7 px × 2 ≈ 11 px), then threshold the filtered image. The closing erases the stain from its own local background, so the filtered background sits near zero and one threshold holds everywhere.
- Constrain automatic thresholds. When defect pixels are under about 1 percent of the image, inter-class methods drift toward the dominant background. Keep the fixed midpoint, or run the automatic tool inside a region of interest around the inspection zone.
- Verify stability. Shift the threshold ±15 gray levels; the segmented area must change by less than 10 percent, and all 20 golden images must classify correctly.
- Log the gap. Record the stain-background gap every shift and alarm when it falls under 30 levels. A shrinking gap is a lighting problem arriving days before it becomes a quality escape.
When grayscale is the wrong answer
Escalate away from gray when the tint change holds luma constant, when you must identify which of several defect colors is present, when the background is itself colored with luma near the stain's, or when surface texture makes the stain a smaller variation than the grain. The path is color spaces first — Lab a*/b* and HSV are the workhorses — then a trained classifier for overlapping palettes: Bayesian color image segmentation, and AI defect inspection for natural appearance variability when the product's own variation is the problem. For texture-limited surfaces, local variance filtering targets the stain without modeling the grain; for scratches, dynamic thresholding is the stronger tool.
When you keep the color path, a color camera is the requirement:
- Sensor format
- 1/1.7"
- Lens mount
- C-mount
- Interface
- GigE
- Resolution
- Up to 3 MP
Match the color sensor to the same resolution arithmetic above, filtered by sensor format and shutter in the camera finder.
Checks before you commit parts
- Gray gap of 30 levels or more, measured in production lighting with the enclosure closed.
- At least 4 pixels across the smallest defect at the chosen field of view.
- Lens covers the sensor format at the working distance from the formula, held at the aperture you plan to stop to.
- Ring light overfills the field by about 1.5 times; diffuser fitted for flat parts, dome for curved ones.
- Exposure, gain and white balance locked; strobe duration sized against line speed.
- A named fallback for equal-luma defects — color camera or filtered mono — so a recipe change does not stall the station.
Start the station in the system configurator, browse area scan cameras and ring lights, or send your part and one defect image through the contact page and we will size the set-up with you.
Questions
How do I choose the structuring element size for a bottom-hat transform?
Make the disk's diameter 1.5 to 2 times the widest defect you must keep. Smaller than the defect and the closing only partly fills it, leaving a weak, unstable signal; much larger and runtime grows while the closing starts to flatten background structure you wanted removed. Recompute the size in pixels whenever the field of view or camera changes, because the defect's pixel width scales directly with the pixel size on the object.
Is a fixed midpoint threshold better than Otsu for a red stain?
Use the fixed midpoint between the two measured region means when both populations are tight; it is stable, repeatable and immune to scene content. Switch to an inter-class method such as Otsu when the defect shade varies part to part or the background is broad. One trap: when defect pixels make up under about one percent of the image, Otsu drifts toward the dominant background, so constrain it to a region of interest.
Can a mono camera replace a color camera for color defects?
Yes, whenever the defect's luma differs from the background's, which covers nearly all saturated stains on pale or gray surfaces. A mono sensor with a bandpass filter chosen against the stain color — green for a red stain — widens the gray gap further and saves bandwidth. Keep color only when the recipe must tell defect colors apart or the tint sits near equal luma, where no gray threshold can separate the two.
What gray-level separation survives a production shift?
Thirty levels at 8-bit, with the threshold at the midpoint, leaves 15 levels of headroom on each side — enough for LED aging and small ambient leaks between maintenance cycles. Below a 20-level total gap, expect intermittent false accepts as soon as an enclosure door opens or a light dims; fix the lighting rather than tightening the software. Re-measure the gap at every PM cycle and alarm on the trend, not just the pass limit.
How much does the bottom-hat step slow inspection?
It costs one closing — a dilation followed by an erosion — plus a subtraction, roughly two to three times the runtime of a plain threshold on the same image. On a smart camera or embedded vision sensor that is real budget at several megapixels per cycle, so apply the transform inside the inspection region rather than across the full frame. If the full frame needs it every cycle, the cheaper fix is lighting or shading correction, not more morphology.


