A part that passed at the bench and fails on the line is rarely a camera problem; it is a segmentation that was tuned by eye. This page gives the workflow with numbers: read the gray histogram and cut a threshold a fixed offset below the background peak, clean the binary regions with a morphological opening or closing sized to the defect, then accept or reject each region on measured area and compactness. It is written for engineers configuring smart cameras or writing inspection code for a vision controller. Every step carries a number you can enter directly as a rule: a threshold 25 gray levels under the peak, a structuring-element radius of half the smallest feature to remove, and a compactness limit of 1.10 for round parts.
What are the steps of a threshold segmentation workflow?
Five steps: grab the image, threshold it into regions from the gray histogram, clean and separate those regions with morphology, measure shape features such as area and compactness, and output a pass, fail or position.
Grab with fixed exposure and a strobed or shrouded light so gray values mean the same thing every cycle; an auto mode left on quietly moves the histogram under you. Thresholding turns the 8-bit gray image into binary regions — every pixel below the cut becomes a candidate part or defect. Morphology removes texture, breaks bridges and fills holes without resizing the regions that survive. Feature extraction reduces each region to a handful of numbers, and the decision step compares them against limits set once at commissioning. Each step is one pass over the data, so run time scales with pixels rather than with scene content. A smart camera runs all five on the device; a vision controller runs them for several cameras in one cycle. The automatic threshold methods for grayscale images guide covers step two in depth; the rest of this page walks the whole chain from grab to decision.
How do you choose the threshold from the gray histogram?
Find the most frequent gray value in a background region and cut the threshold 25 gray levels below it for dark defects on a bright background. Run 20 or 30 sample images, widen the offset until background texture disappears, narrow it until no defect edge is lost.
The histogram of a well-lit scene shows a tall background peak and, lower down, a hump for the part or its defects. The peak is the statistics of the surface you rely on, and the defect sits far below it, so a cut at peak − 25 lands inside the valley for most bright-field setups on metal, plastic and paper. Compute the histogram inside a fixed ROI that includes background, never the whole frame, or every part that enters the view drags the peak with it. If background texture in your sample images spans more than about 20 gray levels, raise the offset until it clears the cut. And check the valley itself: where the two modes overlap with no dip between them, no global number will separate them — fix the light first, then the threshold.
Otsu's method picks the valley between two modes automatically and suits images where part and background occupy similar areas; the peak-minus-offset rule wins when the background dominates, because the peak then stays put from image to image.
When should you use a local threshold instead of a global one?
When illumination varies across the field — curved parts, large fields, unshielded stations — threshold each neighborhood against its own mean: a 21 × 21 mean filter with pixels 15 gray levels below it flagged, and a window at least three times the widest feature.
A global threshold assumes one background gray for the whole image. A fall-off of 30 gray levels from center to corner — the everyday case on a large field lit by a bare bright field — either erases the part in the corner or floods the center with false regions. The local mean image subtracts that gradient out: each pixel is judged against the average of its own window, so a 21 × 21 window resolves features up to 7 pixels across (21 ÷ 3), and the 15 gray-level offset starts in the same order as the global rule's 25. The price is a second image in memory and roughly double the segmentation time, so keep the global rule wherever the light is controlled. The dark region extraction with adaptive thresholding article shows the neighborhood method working on uneven part shading.
Which morphological operation fixes which segmentation defect?
Opening — erosion then dilation — removes specks, thin bridges and protrusions narrower than twice the structuring-element radius; closing — dilation then erosion — fills gaps and pinholes narrower than twice the radius. A radius-2 circle removes anything under 4 pixels wide and leaves a 5-pixel speck alone.
Raw erosion shrinks every region by the radius, which is why it is the wrong cleaning tool: a 4-pixel defect disappears, but so does a 2-pixel-thick part wall. Opening erodes and then grows back, so it cleans without resizing what survives. Closing is its mirror image — it joins a region split by a glare streak and removes pinholes without moving the outer boundary.
| Binary-image problem | Operation | Element | Effect |
|---|---|---|---|
| Background texture above the cut | opening | circle, radius 2 | removes features narrower than 4 px |
| One part split by a glare streak | closing | circle, radius 2 | fills gaps up to 4 px |
| Ragged defect outlines | opening | circle, radius 1 | smooths the boundary by 1 px |
| Two touching parts, thin neck | erosion, then dilation | circle, radius 1-2 | breaks the neck, restores size |
Size the element from the geometry: the radius must exceed half the width of what you want gone, so a 3-pixel scratch needs radius 2, not 1. Applying opening twice is not the same as doubling the radius — use the larger element in one pass.
Which shape features should accept or reject a region?
Decide on three numbers: area in pixels, compactness C = P²/(4πA) and convexity — the region area over its convex-hull area. A circle scores 1.00, a square 1.27 and a 30 × 1 pixel scratch about 10, so a compactness limit of 2.0 splits blob-like specks from linear marks.
Area sets the sensitivity floor. After an opening of radius 2, the smallest possible survivor is a 4-pixel disk of about 13 pixels (π × 2² = 12.6), so a filter of area ≥ 12 defines the smallest detectable defect — in pixels, and multiplied by your pixel size, in millimeters. Compactness is scale-independent, which makes it the guard against shape errors: set the pass window on a master part (compactness ≤ 1.10 for a round part, area within ±5 percent) and let anything else fall out. Convexity catches crescents and clusters that area and compactness both miss: a solid blob stays above 0.95, a crescent-shaped burr drops below 0.8. Add one gray feature when darkness itself matters — a mean gray at least 40 levels below the background peak separates a real speck from a shadow edge. The compactness blob filtering article shows compactness removing linear background features, and counting lines per region with blob analysis extends region features to structured parts.
How does the workflow perform on a real part?
On a 6 MP camera over a 120 mm field, one pixel is 120 mm ÷ 3072 = 0.039 mm, so a 0.16 mm speck spans about 4 pixels; thresholding at peak − 25, opening with radius 2 and an area limit of 12 pixels catch that speck and nothing smaller.
Worked example — dark specks on a machined aluminum cover crossing a belt at 100 mm/s:
- Pixel size: field of view ÷ sensor pixels = 120 mm ÷ 3072 = 0.039 mm per pixel.
- Smallest speck in the specification, 0.16 mm: 0.16 ÷ 0.039 = 4.1 pixels across.
- Motion blur, unstrobed at a 1 ms exposure: 100 mm/s × 0.001 s = 0.10 mm, about 2.6 pixels — most of the speck. A 100 µs strobe smears it 0.01 mm = 0.26 pixels, so edges stay sharp and the histogram keeps its shape.
- Threshold: the good-part background peaks at gray 210 with texture from 202 to 218, so the cut is 210 − 25 = 185. Texture survives above 185; specks at gray 90-120 fall below it.
- Opening, circle radius 2: leftovers narrower than 4 pixels vanish; the 4.1-pixel specks stay.
- Features: area ≥ 12 pixels keeps the specks (a 4-pixel disk is about 13 pixels), compactness ≤ 2.0 keeps blob-like specks and drops scratches, which score about 10.
- Decision: any region left means reject; an empty list means pass.
Every step is a single pass over the image, so the chain costs one grab and a few scans, not a per-pixel search repeated per defect. For the strobe step, the LS-CTL-0070 provides four 24 V channels at 200 W with RS-232 control.
- Output
- Constant voltage 24 V
- Channels
- 4 channels
- Control
- Digital
- Feature
- Strobe
Should you run the workflow on a smart camera or a vision controller?
One camera and a rule-based pipeline run on the smart camera itself; a vision controller earns its place at several cameras, custom code, or PCI Express expansion — its models carry five or more LAN ports for exactly that.
A smart camera integrates sensor, processor, lens mount and I/O, and this entire histogram-to-decision chain executes on the device. The MV-SC5060XM-08M-WBN is a 6 MP mono smart camera with an 8 mm M12 lens — the class of camera the worked example above assumes. When the pipeline is your own code rather than a configured tool set, an x86 smart camera runs it where the image is: the LS-SMC-0017 is an x86 platform camera with an 8.8 MP sensor and a C-mount. A vision controller adds LAN ports, memory and built-in light channels: the MV-VC3301H-128G66 pairs an Intel Core i3 with 8 GB of memory and four built-in light outputs, and includes the VM6200Pro license.
- Interface
- GigE
- Resolution
- 6-10 MP
- Sensor format
- 1/2.4"
- Shutter
- Rolling
- Interface
- USB 2.0
- Resolution
- 6-10 MP
- Sensor format
- 1"
- Shutter
- Global
- LAN ports
- 5+ LAN ports
- USB 3 ports
- 3-4 USB 3 ports
- Light outputs
- 4 built-in channels
- Memory
- 8 GB
Browse the smart cameras category or the camera finder for the sensor side, and start the system configurator from the camera step. As the camera count grows, cycle time becomes the constraint — the smart camera image buffers article shows how to match capture rate to processing rate before you size the controller.
Why does a threshold that passed at the bench fail on the line?
Because the histogram moved: auto exposure or gain left on drifts background gray over a shift, ambient light adds to it through an open gantry, and mains flicker beats against the exposure — the peak moves, and a fixed threshold lands in the wrong place.
Run the causes in this order, quickest test first:
- Auto modes on. Read exposure and gain on consecutive images; if either changes, every auto mode comes off and stays off. The brightness drift article walks through turning each auto function off in order.
- Ambient light. Repeat the same part with the hall lights off. A peak shift larger than your 25-level margin means the station needs a shroud or a strobe: a strobe freezes the scene for its flash duration, so ambient contributes only during those microseconds.
- Mains flicker. A dark band rolling through images at certain exposure times is the supply period beating against the shutter; the black bars fix separates flicker from exposure settings.
- Compression artifacts. Phantom 1-2-pixel specks that disappear when the image arrives losslessly are edge artifacts, not defects; the JPEG artifacts and thresholding article shows how they fool an area filter.
Strobing plus recomputing the histogram peak on every image removes the first three causes on most lines, and neither change retunes your other limits — the 25-level offset rides along with the peak. Match the controller to the light in the light controllers category or the light controller finder.
What do you check before ordering the hardware?
Four checks before you order: pixel size against the smallest defect, a lens mount that fits your working distance, enough strobe channels for every light in the station, and one spare LAN port per camera.
- Resolution first: pixel size = field of view ÷ sensor pixels, and the defect must span at least 3 pixels. Work it backwards in the camera resolution calculator — enter the field of view, read the pixel size, divide the defect by 3 and compare.
- Element shape: if scratches run along a known axis, plan a rectangular element rather than a circle, and rotate it with the part.
- Light: bright field for surface specks on flat parts; budget one controller channel per light plus one spare, with strobe support.
- Trigger and cabling: one LAN port per camera plus one spare on the controller, and the trigger chain planned before ordering cable — the camera-to-strobe buffer relay article covers the case where a PLC sits in the trigger path.
Send sample images with the order: free expert support comes with every order, and the contact page is the way in. The rest of the workflow articles live on the applications topic page and the guides hub, including dark speck detection with thresholding and morphology, the closest worked cousin of this pipeline.
Questions
Can this workflow segment color images?
Threshold one channel, not all three. Convert to the channel with the highest contrast between defect and background — often blue for a yellow defect, or the saturation channel for stains — then run the same histogram rule on that channel. Where the color itself defines the region, segment in a color space such as Lab and combine the per-channel regions with a union.
Should the structuring element be a circle or a rectangle?
Circle for specks, pits and any defect without a known direction, because it treats all orientations equally. A long, thin rectangle is for known directions: an opening with a rectangle much taller than wide removes horizontal bridges while sparing vertical scratches, which cleans a brushed surface without erasing the brushing. Rotate the rectangle with the part orientation, or it will miss.
How do I separate parts that touch in the binary image?
Close small gaps first, then erode by 1 or 2 pixels to cut the neck where the two regions join, and let the dilation inside the opening rebuild the survivors. For a wide neck, run a watershed on the distance transform: each part's thickest point seeds its own basin. Check the count afterwards — over-erosion splits one rough part in two.
Do area limits need re-teaching after a lens or working distance change?
Yes. Area counts pixels, and pixels shrink or grow with the field of view, so limits scale with the square of the change: moving from a 120 mm to a 100 mm field multiplies every region's pixel area by (120 ÷ 100)² = 1.44. Multiply the old limits by that squared ratio or re-measure a master part, and re-check compactness limits — they do not scale, but perimeter estimation shifts slightly.
How small a defect can this workflow detect?
Count pixels: the defect must span at least 3 pixels, and 5 or more for a trustworthy measurement. Pixel size = field of view ÷ sensor pixels, so catching a 0.5 mm defect needs pixels of 0.5 ÷ 3 = 0.17 mm or finer, which caps the field of view for a given camera. Below 3 pixels the opening removes the feature entirely.



