ApplicationsHow-to

How to separate touching digits before OCR

Separate touching digits before OCR with fixed-pitch cells, projection valleys, or morphology, then check stroke preservation and image sampling.

LumaSpec Vision9 min read Share on LinkedIn

On this page 10 sections
  1. 01Choose the least destructive method first
  2. 021. Prepare the image and record its geometry
  3. 032. Use fixed-pitch cells when the layout supports them
  4. 043. Find projection valleys near plausible boundaries
  5. 054. Apply opening only to suitable bridges
  6. 065. Validate candidate splits before recognizing digits
  7. 076. Check whether acquisition provides enough pixels
  8. 087. Verify performance and define a rejection path
  9. 09Before you deploy
  10. 10Questions

Touching digits can turn a readable number into a single connected component, leaving a character-by-character OCR pipeline without usable crops. The right fix depends on whether the characters occupy predictable cells, retain visible gaps, or connect through narrow bridges. This workflow helps you choose a splitting method, preserve thin strokes, and reject doubtful boundaries. Keep the original grayscale image throughout: a segmentation mask should help locate characters, not become the only evidence available to the recognizer.

Choose the least destructive method first

Start with known layout constraints, then inspect projection valleys, and use morphology only when the connection geometry supports it. A connected component is not necessarily a character: several digits can share one component, while one damaged digit can produce several components.

Method Choose it when Main limitation First verification
Fixed-pitch cells Character positions follow a stable grid Registration errors cut through characters Overlay cell boundaries across representative images
Vertical projection valleys Neighboring digits have low-ink columns between them Internal character shapes can create false valleys Check each valley against expected spacing
Morphological opening Thin bridges connect substantially thicker strokes Thin strokes and small features may disappear Compare the original and processed masks
Reject and reacquire Characters are blurred, clipped, or poorly sampled Requires acquisition changes Check whether separation exists in the grayscale image

Before splitting characters, isolate the correct text line. The OCR layout segmentation guide addresses that earlier decision; this workflow starts with a cropped numeric reading.

Also check whether your recognizer needs individual characters. A recognizer designed for whole strings may perform better on the intact line than on manually separated crops.

1. Prepare the image and record its geometry

  1. Crop the numeric field while retaining a small background margin around the text.
  2. Correct rotation or perspective using the field geometry, then inspect the resampled strokes.
  3. Convert the image into a binary mask with foreground digits represented consistently.
  4. Measure text height, ordinary stroke thickness, approximate character spacing, and bridge thickness.
  5. Save the original grayscale crop alongside every derived mask.

Measure across several samples rather than choosing parameters from one convenient image. Include narrow characters, rounded characters, weak print, and the largest expected position changes.

Inspect the grayscale crop before blaming segmentation. If two characters are separate there but joined in the binary mask, adjust thresholding or illumination first. A live binary image preview makes this comparison easier during setup.

Avoid adding closing automatically to repair broken strokes. Closing can also connect adjacent digits and make the original problem worse. Any repair operation needs its own before-and-after check.

2. Use fixed-pitch cells when the layout supports them

A fixed grid is often the simplest solution for monospaced printing or a numeric field with known character slots. It does not require touching digits to become separate connected components.

First register the grid to a stable field origin. Do not infer that origin only from the leftmost ink pixel: the ink position can change when the first character changes.

Boundary position = field origin + k × character pitch k = 1, 2, ..., character count − 1

For an illustrative field containing six cells across 144 pixels:

Character pitch = 144 pixels / 6 = 24 pixels Internal boundaries = origin + 24, 48, 72, 96, 120 pixels

Overlay those boundaries on representative images. Check the first and last cells as carefully as the middle cells. A correct pitch with an incorrect origin can damage every crop.

Use slot width as a layout constraint, not as the expected ink width. A narrow digit can occupy the same cell as a wide digit without filling it.

Fixed cells become unreliable with proportional fonts, changing character counts, or variable spacing. In those cases, use estimated cell boundaries as search regions rather than compulsory cut locations.

3. Find projection valleys near plausible boundaries

A vertical projection counts foreground pixels in each image column. Gaps and narrow connections often produce lower counts than the bodies of adjacent digits.

B(x, y) = 1 for foreground, 0 for background P(x) = sum of B(x, y) over the text height Normalized projection Q(x) = P(x) / text height

For example, suppose the text height is 32 pixels. A column containing 3 foreground pixels has a normalized projection of 3 / 32 = 0.094. A neighboring column containing 18 foreground pixels gives 18 / 32 = 0.563. The lower column is a candidate boundary, not proof of a valid split.

Search around expected character boundaries rather than taking every local minimum. For the illustrative grid above, you might initially inspect a window extending 4 pixels to either side of each predicted boundary. Adjust that window from measured registration and spacing variation.

A short smoothing filter can reduce isolated fluctuations, but inspect both the original projection and the smoothed curve. Excessive smoothing can hide a narrow gap or move the apparent minimum.

Do not fill every character hole by default. Filling changes the projection and can remove useful shape information. If hole filling helps generate candidates, perform it on a separate copy and validate boundaries against the original mask.

Where no plausible straight boundary exists, reject the simple projection split. A validated curved separation path or whole-string recognition may be more appropriate than forcing a vertical cut.

4. Apply opening only to suitable bridges

Morphological opening applies erosion followed by dilation using the same structuring element. It can remove narrow foreground connections, but it does not restore all detail removed during erosion.

Choose the structuring element from measured bridge and stroke geometry. Its dimensions and orientation matter; there is no universal kernel size for printed digits.

For an illustrative test, ordinary strokes might be 4–5 pixels thick while a connecting bridge is only 1 pixel thick. A 3 × 3 element is worth evaluating, but those measurements do not guarantee success. Curves, junctions, and stroke orientation affect the outcome.

Use one operation as the initial experiment. Compare it with the unchanged mask before increasing kernel size or repeating processing. A larger element may separate the pair while also deleting a thin terminal or dividing another character.

Prefer using the processed mask to propose character locations, then recognize crops from the original grayscale image. If opening provides no stable separation without damage, return to layout constraints or improve acquisition instead of escalating the erosion.

5. Validate candidate splits before recognizing digits

A plausible boundary must satisfy several constraints together. Character count alone is insufficient: an incorrect split can produce the expected count while cutting one digit and merging another.

Check each candidate against:

  • Position: Is the boundary compatible with the field origin and measured spacing?
  • Height: Do the resulting crops contain the expected vertical text extent?
  • Ink distribution: Does a crop contain one plausible character rather than a detached fragment?
  • Stroke continuity: Does the split remove part of a visible stroke in the grayscale image?
  • Format: Is the recognized string compatible with the required length and permitted characters?

Keep more than one candidate when the evidence is ambiguous. Recognize the alternatives and compare their geometry, recognition evidence, and field constraints. Treat recognition confidence as one input, not a guarantee that the boundary is correct.

Never add a missing digit merely to satisfy an expected count. When segmentation succeeds but recognition still omits characters, use the numeric OCR missing-digits troubleshooting guide to separate recognition failures from missing crops.

Record rejection explicitly. A doubtful reading should not silently become accepted production data.

6. Check whether acquisition provides enough pixels

Segmentation cannot recover a boundary that blur, clipping, or inadequate sampling has removed. Review focus, exposure, contrast, and image scale before replacing the algorithm.

For a starting estimate, calculate how many pixels cover the narrowest relevant stroke:

Required horizontal pixels = field width × target pixels per stroke / stroke width

Suppose an illustrative field is 120 mm wide, its narrowest relevant stroke is 0.20 mm, and your initial sampling target is 4 pixels across that stroke:

Required horizontal pixels = 120 × 4 / 0.20 = 2400 pixels

This is a sampling estimate, not an OCR accuracy guarantee. Lens contrast, focus, print variation, motion blur, and resampling still affect usable detail. Use the camera resolution selection guide for the underlying sampling trade-offs.

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

For a host-processing workflow, review area scan cameras and filter candidates in the camera finder. The LS-CMR-0166 is one acquisition candidate to evaluate against your image requirements.

Area scan cameras12 MP USB3 area scan camera, IMX226, 1/1.7" rolling shutter, 30 fps, 4000 × 3000 resolution (LS-CMR-0166)LS-CMR-0166
Sensor format
1/1.7"
Interface
USB3
Resolution
12 MP
Shutter
Rolling
$560.00View partBuild a system with it

For an on-device approach, review smart cameras and vision sensors. The LS-SMC-0063 is another candidate to evaluate; confirm available OCR tools, crop handling, and diagnostic image access before choosing that workflow.

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

These parts are acquisition candidates, not evidence that a particular segmentation operation is supported. Verify software capabilities and test your actual print samples before committing to either architecture.

7. Verify performance and define a rejection path

Build a labeled test set containing both touching and already separated digits. Include different character pairs, position shifts, weak print, and the extremes of acceptable focus and contrast.

Measure whole-field exact matches as well as segmentation quality. A correct-looking boundary is not useful if the final reading is wrong.

Track at least these outcomes:

  • Correct character count and correct complete reading.
  • Correct character count but incorrect reading.
  • False splits within individual digits.
  • Unseparated character pairs.
  • Rejected fields and their causes.

As an illustrative check, 12 segmentation failures among 300 labeled fields give 12 / 300 = 4%. Inspect those failures by character pair and image condition before changing parameters. Keep tuning samples separate from the final validation set.

Check sensitivity to small changes in threshold, registration, and blur. A method that works only at one exact threshold is a poor production starting point.

Store rejected crops with proposed boundaries and preprocessing settings. That evidence makes it easier to distinguish print defects, acquisition changes, and segmentation mistakes later.

Before you deploy

  • Confirm that individual-character segmentation is actually required by the recognizer.
  • Register fixed cells to field geometry rather than changing ink edges.
  • Search projection valleys only within plausible boundary regions.
  • Test morphology against the thinnest valid strokes, not just the thickest bridge.
  • Preserve grayscale crops for recognition and diagnostics.
  • Validate complete readings on samples excluded from parameter tuning.
  • Define what happens when no trustworthy split exists.

For acquisition selection, start with the camera configurator. If the split remains uncertain, send representative grayscale crops, expected field geometry, and measured stroke widths through application support before choosing hardware or adding more preprocessing.

Questions

Do touching digits need to become separate connected components?

Not necessarily. If the numeric field has stable character slots, you can crop those slots even when ink connects across their boundaries. First confirm that the recognizer requires individual characters; some recognition workflows accept an entire text line without explicit character separation.

What kernel size should I use for morphological opening?

There is no kernel size that is safe for every font and image scale. Measure the narrowest valid strokes and the connections you want to remove, then compare candidate operations against unchanged images. Reject any setting that separates pairs by deleting legitimate character features.

Why does a vertical projection split a digit in the middle?

A digit can contain low-ink columns within its own shape, especially when printing is damaged or thresholding removes faint strokes. Restrict candidate valleys using expected spacing, then check the original grayscale image and the geometry of both resulting crops before accepting a boundary.

When should I improve the image instead of changing segmentation?

Improve acquisition when the grayscale image lacks a stable visible distinction between neighboring characters, or when valid strokes occupy too few pixels for repeatable processing. Check focus, motion blur, lighting contrast, and sampling before assuming that additional morphological operations will recover missing detail.

Can I assume a smart camera supports these segmentation methods?

No. General product specifications do not establish which OCR or preprocessing operations are available. Confirm the device's documented toolset, image export options, and handling of rejected readings. Test representative touching-digit samples before selecting an on-device inspection workflow.