Sub topic 4
Why Image Quality Decides OCR Accuracy
This chapter assumes basic Python, familiarity with images as pixel arrays, and comfort running commands in a terminal. Optical character recognition (OCR) cannot recover information that the image does not capture clearly. Before changing an OCR engine, inspect the image itself.
What the OCR engine sees
A scanned page looks like text to a person. An OCR engine sees numbers representing brightness or color. It must first find likely text regions, then recognize the shapes inside those regions as characters.
Image quality affects both stages:
- Poor contrast can hide text during detection.
- Small characters may contain too few pixels for recognition.
- Noise can look like punctuation or extra strokes.
- Skew can make lines and character shapes harder to analyze.
- Blur can merge nearby characters.
- Uneven lighting can make one part of a page readable and another part invisible.
A useful working model is:
OCR accuracy usually falls before the failure is obvious in the final text. For example, a blurry 8 may become B, while a faint decimal point may disappear completely.
The main quality factors
| Factor | What goes wrong | Typical improvement |
|---|---|---|
| Resolution | Characters contain too few pixels | Capture or scan at a higher resolution |
| Contrast | Text and background have similar brightness | Improve lighting or use contrast adjustment |
| Noise | Speckles resemble character marks | Remove noise carefully |
| Skew | Text lines are rotated | Deskew the image |
| Blur | Character strokes merge or disappear | Use a sharper image or refocus the camera |
| Compression | Block artifacts distort edges | Use a less compressed source |
| Illumination | Shadows or gradients hide text | Use even lighting or correct the background |
Resolution is especially important for small text. Enlarging a low-resolution image does not recreate missing detail. It only creates more pixels around the same uncertain shapes. A scan with sharp, original pixels is better than a heavily enlarged screenshot.
Contrast describes the difference between foreground text and its background. Black text on white paper has strong contrast. Gray text on gray paper has weak contrast. Color contrast can also be misleading: red text may look bright in a color image but become similar in brightness to the background after conversion to grayscale.
Inspecting an image before OCR
Start by checking dimensions and color channels. The following script also creates a grayscale version and an Otsu thresholded version. Otsu thresholding chooses a brightness boundary that separates dark and light pixel groups.
import cv2
image = cv2.imread("receipt.jpg")
if image is None:
raise FileNotFoundError("Could not read receipt.jpg")
height, width = image.shape[:2]
channels = 1 if image.ndim == 2 else image.shape[2]
print(f"size: {width} x {height}")
print(f"channels: {channels}")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(
gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU
)
cv2.imwrite("receipt-gray.png", gray)
cv2.imwrite("receipt-binary.png", binary)Open both output files. If letters vanish in the binary image, thresholding may be too aggressive, or the original contrast may be too weak. If the background becomes filled with speckles, the image contains noise or uneven illumination.
Quality problems from cameras and scans
Camera images add problems that flatbed scans often avoid. The page may be tilted, curved, shadowed, or partly out of focus. Perspective distortion makes the top and bottom of a page appear at different scales. A document camera should face the page directly and use even, diffuse light.
Scans can also fail. A low-quality scanner setting, dirty glass, folded paper, or an incorrect color mode can introduce streaks and shadows. JPEG compression creates block-shaped artifacts around sharp edges. Preserve the original scan when possible and create processed copies for experimentation.
Do not apply every filter automatically. Excessive sharpening creates false edges. Strong denoising removes punctuation. Thresholding can erase thin characters. Compare the processed image with the original and test recognition on representative samples.
A practical quality checklist
Before running Tesseract or another OCR engine, ask:
- Are the characters large enough to read when zoomed to 100%?
- Are text strokes sharp rather than smeared?
- Is the text darker or lighter than its background?
- Are shadows, folds, and reflections covering characters?
- Are text lines horizontal, or should the image be deskewed?
- Does the image contain enough detail for the smallest text?
- Did preprocessing preserve punctuation and thin strokes?
Use a small sample from the real workload when comparing settings. A clean printed page may work well while receipts, labels, and photographs fail because their quality problems differ.
Exercise
Why does enlarging a small, blurry image usually fail to improve OCR accuracy?
Show answer
Enlargement adds pixels but does not restore detail that was missing from the original capture. The OCR engine still receives uncertain character shapes.