Code readingTroubleshooting

Industrial OCR character recognition: segmenting and training fonts

Fix industrial OCR character recognition: isolate one region per character, tune the threshold on line lighting, train a font beyond A-Z and 0-9.

LumaSpec Vision12 min read Share on LinkedIn

On this page 10 sections
  1. 01Where industrial OCR actually fails
  2. 02Cause 1 - merged and clipped regions: filter by size, not by area
  3. 03Cause 2 - the threshold was tuned on a bench image
  4. 04Cause 3 - characters below 20 pixels tall
  5. 05Worked example - 2.5 mm etched characters at 150 mm working distance
  6. 06Cause 4 - glyphs the shipped alphabet has never seen
  7. 07Isolate the failing stage in one afternoon
  8. 08Prevention checklist
  9. 09The decision, case by case
  10. 10Questions

The crops look right on screen: ten clean character regions for ten printed characters. Then recognition returns 58O?3 where the part reads 58063, and the project stalls. For an engineer adding OCR to a production line this is the most common failure there is, and it is rarely the font file's fault. The causes stack in a predictable order: merged or clipped character regions, a threshold tuned on bench images, too few pixels per character, and glyphs the shipped alphabet never contained. Work through them in that order with the tests below; the read rate usually recovers within a day, and you will know exactly which stage to blame.

Where industrial OCR actually fails

Recognition returns confident nonsense — an 8 where a 6 is printed, punctuation where nothing is — while extraction still looks fine on screen. Rank the suspects by how often each turns out to be the cause:

  1. Merged or clipped character regions — two characters in one crop, or half a character in each.
  2. Contrast that moves with the day — a threshold tuned on a single bench image.
  3. Pixel starvation — characters under 20 pixels tall.
  4. An alphabet the shipped font never contained.

One more check takes a minute: confirm the classifier is actually running the font file you think it is. A read function left on a default alphabet reads only A-Z, 0-9 and basic punctuation, and it maps every other glyph to the nearest shape it knows. And if the task is checking that fixed text is present and correctly formed, rather than reading variable serials, character verification needs no font training at all — the OCR vs OCV comparison for date and lot codes draws that line.

Cause 1 - merged and clipped regions: filter by size, not by area

The threshold worked and connected-component analysis found regions — then someone selected the largest region and got one character plus its frame, or nothing but the frame. Selection by area rank is the single most common segmentation error. The second is morphology with kernels larger than the features they are meant to clean: a closing disk wider than the gap between characters welds neighbours into one region, and an opening disk wider than a stroke erases the character it was meant to despeckle. On a 25-pixel-tall sans-serif character the stroke is 2-3 pixels wide, so an opening disk of radius 3 pixels or more eats it.

Filter by geometry instead. Keep regions whose height falls within ±20% of the nominal character height and whose width sits between 0.3 and 1.1 times that height. Digits run about 0.5-0.6 in width-to-height ratio; letters range from the narrowest glyphs near 0.3 to a wide M near 1.0. Across one text line, region heights should agree within about 15%, because font size and camera distance are fixed — a region twice as tall is a stacked pair or a border, and it must be removed before the classifier sees it.

The test is that folder. You are checking that the crop count equals the character count and that each crop holds exactly one character. Half characters, doubled characters and frame fragments are segmentation output, not classifier output, so no font change fixes them. The segmentation fix for touching letters shows the size-filter pattern in detail; reading randomly rotated digits with OCR covers the case where per-character segmentation only works after deskew. When the text block itself moves in the image, find the block first and segment inside it — the two-stage OCR pipeline of detection then recognition is built for exactly that.

Cause 2 - the threshold was tuned on a bench image

A fixed threshold of 150 on a 0-255 grey scale is set from one clean image. On the line it meets laser etching that varies with focus, inkjet that varies with speed, specular streaks on curved metal, and daylight through an open door. Read rate then drifts between shifts even though nothing in the recipe changed.

Run the numbers instead of arguing. Take 30 production images captured across a full day, find the best threshold for each, and watch the spread: if the per-image optimum moves more than about 25 grey levels, no fixed value can hold. The illumination is part of the variation, and software is compensating for physics it cannot see.

Set the threshold automatically inside the character ROI — Otsu's method is the standard default and costs almost nothing per frame — or remove the variation at the source. Diffuse or dark-field lighting makes an etched or stamped mark the extreme of the histogram instead of a mid-grey ripple; controlling ambient light variation in pick-and-place vision walks through that choice. Check polarity too: if the pipeline only works after complementing the thresholded region, the threshold is selecting the background, so threshold the marks directly. Mains-powered LED lights also flicker at 100 or 120 Hz, and a short exposure can land between pulses — the flicker-versus-exposure check behind black bars in camera images covers that signature.

The strongest fix is a strobe: a flash of 100 µs to 1 ms freezes motion and contributes an exposure the room cannot influence, so contrast stops drifting with the hour. A digital controller with a strobe feature is the piece that locks this down — the LS-CTL-0070 drives 4 channels at 200 W with RS232 control, one channel per light and one recipe per product.

Light controllersDigital control box, 4 ch, 200 W, RS232 (LS-CTL-0070)LS-CTL-0070
Output
Constant voltage 24 V
Channels
4 channels
Control
Digital
Feature
Strobe
$549.00View partBuild a system with it

Cause 3 - characters below 20 pixels tall

Below about 20 pixels of character height, recognition degrades steeply and no training rescues it. The geometry is simple: a sans-serif stroke is roughly one tenth of the character height, so 20 pixels leaves about 2 pixels across each stroke after antialiasing — the minimum the threshold stage needs to keep strokes unbroken. Plan on 25-35 pixels: 25 for a fixed station, 35 where characters sit near the edge of the field of view or print depth varies. The minimum font size for OCR expands on that floor, and why OCR confuses similar characters shows what 8/B and 0/D confusion at low resolution or contrast looks like. Reader documentation uses the same arithmetic; this guide to choosing a valid character image size for OCR walks it from the decoder side.

pixels per character = character height (mm) × px/mm px/mm ≥ 25 ÷ character height (mm) m = sensor width / field of view f = WD × m / (1 + m)

Size the optics from the character, not the part. Required px/mm fixes the maximum field of view for a given sensor; the two-line lens formula then gives focal length from working distance. If the arithmetic lands under 20 pixels per character, change focal length, working distance or sensor — training harder on a starved image is the expensive path. The calculator below runs the pixel check directly.

Camera resolution for the smallest feature
Units
Pixels needed
–
Sensor size
– MP
Smallest sensor that fits
–
Pixel on the part
– mm
Pixels across the feature, that sensor
–

Pixels needed = field / feature × pixels per feature on each axis (field incl. margin). The lens must resolve the pixels too: Nyquist = 1000 / (2 × pixel µm) lp/mm.

Find cameras with enough pixels Open the full calculator

Worked example - 2.5 mm etched characters at 150 mm working distance

An aluminium housing carries laser-etched serial text at 2.5 mm character height. The camera is a 1440 × 1080 mono sensor of 1/3-inch format, about 4.8 mm wide, mounted 150 mm from the part. Target: 25 pixels per character.

  1. Required scale: 25 px ÷ 2.5 mm = 10 px/mm.
  2. Maximum field of view: 1440 px ÷ 10 px/mm = 144 mm.
  3. Magnification: m = 4.8 mm ÷ 144 mm = 0.033.
  4. Focal length: f = 150 mm × 0.033 ÷ (1 + 0.033) ≈ 4.8 mm, so order a 5 mm lens. The lens focal length calculator runs this step directly.
  5. Check with the real lens: m = 5 ÷ (150 − 5) = 0.0345, so FOV = 4.8 ÷ 0.0345 ≈ 139 mm and scale = 1440 ÷ 139 ≈ 10.4 px/mm. Each character lands at 2.5 × 10.4 ≈ 26 px — inside the 25-35 target.
  6. Motion: one pixel is 139 ÷ 1440 ≈ 0.1 mm, so at a line speed of 100 mm/s an exposure of 0.1 ÷ 100 = 1 ms keeps blur within one pixel.
  7. At 1 ms, room light will not expose the image — this is the strobe case from Cause 2.

Now the decision the example is built to teach. If the same housing carried 1 mm characters, the 139 mm field of view would give 10.4 px per character — half the floor. Shrinking the field of view to about 55 mm restores it (1440 ÷ 55 ≈ 26 px/mm), and the lens formula follows: m = 4.8 ÷ 55 ≈ 0.087, f ≈ 150 × 0.087 ÷ 1.087 ≈ 12 mm. The fix is a 12 mm lens at the same station, not a better font.

Cause 4 - glyphs the shipped alphabet has never seen

The font files that ship with vision libraries cover A-Z, 0-9 and basic punctuation, and nothing else. Accented letters, national scripts, logo glyphs and heavily stylised typefaces fall outside, and a classifier without those classes maps every unknown glyph to the nearest shape it knows — which produces exactly the garbage you are seeing. The giveaway: digits read fine while exactly the non-standard characters fail, or every character fails on one product family only.

Train a font of your own, and train it on line images, not renders. Collect roughly 100 samples per character class from production — 30 is too few and degraded prints want several hundred — spread across rotation up to ±10-15°, the contrast spread from Cause 2, and the print-depth range the process really produces. Hold out 20% for validation and require near-99% per-character accuracy on the hold-out before the font touches the line. Then set the rejection threshold, starting near 0.6 and moving it from the confusion report, so a low-confidence character rejects instead of guessing.

Training an OCR character set for printed sans-serif fonts shows the set-building workflow, and building your own OCR training data from cropped character images covers the crops themselves. For character sets beyond Latin — per-country license-plate fonts are the classic case — the per-country license plate training data guide plans the classes before the first sample is cropped. Retrain whenever the marking process changes: a font trained on laser-etched digits does not transfer to inkjet.

Isolate the failing stage in one afternoon

Work down the list and stop at the first test that fails:

  1. Crops. Export the regions that reach the classifier. A count mismatch, or any crop holding two characters, half a character or a frame fragment, is Cause 1.
  2. Region statistics. Heights across one line should agree within ±15% and widths inside the 0.3-1.1 aspect window. Outliers mark merged or clipped regions — Cause 1 again.
  3. Threshold spread. Best threshold across 30 line images moving more than about 25 grey levels is Cause 2. Fix the light, not the constant.
  4. Pixel height. Characters under 20 px tall in the raw image are Cause 3. Resize the optics.
  5. Confusion pattern. Consistent swaps like 8/B, 5/S and 0/D point at resolution or contrast. Failures confined to glyphs outside A-Z 0-9 point at the alphabet — Cause 4. Random failure on clean single-character crops points at a font loaded wrong or trained on bench images only.

Each test takes minutes and names its own fix, and the font file is the last thing you touch.

Prevention checklist

Carry these into the next station design:

  • Optics: design to 25-35 pixels of character height at the worst-case part position, not 20 at the best.
  • Light: strobe with diffuse or dark-field geometry matched to the surface; set exposure by flash duration, not by the room.
  • Segmentation: store character height and width filter limits in the product recipe, never select by area rank alone, and keep morphology kernels below the stroke width.
  • Threshold: auto-threshold per character ROI, verified on 30 images spanning a full day of ambient light.
  • Font: one trained font per marking process, a 20% hold-out, and a rejection threshold set from the confusion report.
  • Acceptance: write the ambient-light condition into the acceptance test so a sunny afternoon cannot fail the line silently — the ambient light rejection specification for vision acceptance tests gives the wording.

The decision, case by case

Crops broken: fix segmentation with size filters and small kernels. The font is innocent.

Crops clean, reads drift with the hour: auto-threshold inside the ROI and strobe the light. Contrast that moves is a lighting problem, not a training problem.

Under 20 pixels per character: change focal length, working distance or sensor first, following the worked example. When the pixel math clears the target and the read must run on the device without a PC, a smart camera does the job — the LS-SMC-0063 pairs a 16 mm lens with a 1.6 MP sensor, a 60 fps readout over GigE and a polarized white light built in.

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

Digits fine, other characters wrong: train the font from line crops at about 100 samples per class.

The mark is a symbology, not text: stop tuning OCR. Decoding libraries beat character classifiers on codes by a wide margin, and a purpose-built reader keeps up with line rate — the LS-CRD-0028 reads 60 codes/s at 30 fps through an 8 mm lens with its 2560 x 2048 sensor.

Fixed code readers2560 x 2048 fixed code reader, 30 fps, 60 codes/s, 8 mm lens (LS-CRD-0028)LS-CRD-0028
Interface
Fast Ethernet
Resolution
5 MP
Sensor format
1/1.7"
Shutter
Global
$920.00View part

Once the failing stage is named, the fix is usually a parameter, a light or a lens — in that order of cost. To size a new station, the camera finder filters by sensor, shutter and resolution, the light controller finder matches channels and strobe features to your lights, and the code-reading topic page collects the character-reading guides. The vision configurator steps through camera, lens, light and controller in one pass.

Questions

How many pixels tall does a character need for reliable OCR?

Plan on 20 pixels of character height as the floor and 25-35 pixels as the comfortable zone. The rule comes from stroke geometry: a sans-serif stroke is roughly one tenth of the character height, so 20 pixels leaves about two pixels across each stroke. Below about 16 pixels, strokes merge during binarisation and no classifier recovers them. Upscaling after the fact does not help either — the detail was never captured.

Why does my OCR work on test images but fail on the production line?

Because the threshold, the morphology and the font were chosen on a handful of clean, flat, evenly lit samples. Production adds specular spots, curvature, dirt and varying print depth, so the optimum threshold moves and characters weld together. Re-tune against 30 or more images captured on the line under worst-case lighting, and keep a held-out set for every font iteration.

Which characters require a trained OCR font?

Everything outside the shipped A-Z, 0-9 and basic punctuation set: accented letters, national scripts, and stylised or etched typefaces that depart from standard alphabet shapes. Expect to gather around 100 samples per character from the line, covering rotation, contrast and print-quality spread, and retrain when the marking method changes. Dot-peened or laser-etched digits often deserve their own trained font too.

Can one camera read barcodes and OCR characters at the same station?

Plan for two stations. Code readers are built around symbology finders and decode libraries, while OCR needs character-height pixels and clean segmentation; optics and exposure that suit one degrade the other. If the part carries both a DataMatrix code and a human-readable serial, read them in two shots — or in one image only if the character height still clears 25 pixels.

How do I tell whether the failure is in segmentation or in the classifier?

Export the exact regions that reach the classifier as image files. If a crop contains two characters, half a character or a piece of the border, the failure is segmentation and no font change will help. If every crop holds exactly one clean character and the reads are still wrong, the problem is contrast or the font itself — check which classes are confused before retraining.