Thresholding for Mobile OCR: An Introduction – Part 1

Project Description

Thresholding for Mobile OCR: An Introduction – Part 1

Code examples for OpenCV in C++ with resulting images are provided – and for the math lovers among you, there will also be a more detailed explanation.

What is Thresholding?

Thresholding is a basic method for image segmentation, used to separate regions of objects from the background. The simplest form of thresholding compares each pixel intensity value with a determined threshold value. Each pixel below a specific threshold limit is assigned a value like “black” or “white” respectively, if the pixel intensity is above the threshold limit. The output of a thresholding operation is a binary image as seen on the example images below.

What is Thresholding good for?

Image analysis systems usually include several tasks, one of them is thresholding. Thresholding has a huge influence on the performance of the following processes, because these are based on the data output coming from the thresholding operation. Improper thresholding can cause blotches, streaks or erasures on the document as well as confounding segmentation and recognition tasks. If someone performs optical character recognition (OCR), merges, fractures and other deformations on character shapes are the main reason of performance loss.

Thresholding is applied in various image analysis systems, including:
• Document image analysis (extracting characters, graphical content, musical scores, etc.)
• Quality inspection of materials (displaying damages materials)
• Medical image analysis (segment CT, ultrasonic or endoscopic images for diagnostic purposes, etc.)

When to use Thresholding

Thresholding is a relatively simple process in terms of computing power resources and therefore a perfect use case for e.g. mobile phones. However, whenever they achieve a performance similar to more sophisticated methods like Bayesian schemes or Markov models, it should be opted for two-class segmentation problems.

Thresholding Methods

SIMPLE THRESHOLDING

This method only defines one global threshold value for all pixels in the image. Every pixel in the image is compared with this fixed threshold and either replaced with a white or black value, depending on the intensity value which is either higher or lower compared to the threshold.

OpenCV offers a method threshold() which provides five distinct types of thresholding.

double cv::threshold(
  cv::InputArray    src	              // Input image (8 bit, single channel)
  cv::OutputArray   dst	              // Result image (same size/type as src)
  double	        thresh	          // Threshold value
  double	        maxVal	          // the maximal (non-zero) value that can be assigned to output
  int	            thresholdType	  // Threshold type to use (see Table 1)
);

Given the following inputs:

Simple Thresholding Simple Thresholding
input signal input image

1. BINARY

THRESH_BINARY Binary Thresholding Binary Thresholding
threshold type rule output signal

If the intensity of the pixel src(x,y) is greater than thresh, then the dst pixel intensity is set to maxVal, otherwise the pixel value is set to 0

C++ Code

using namespace cv; 

// Read image
Mat src = imread("threshold.png", IMREAD_GRAYSCALE);
Mat dst;
 
// Set threshold and maxValue
double thresh = 200;
double maxValue = 255; 
 
// Binary Threshold
threshold(src,dst, thresh, maxValue, THRESH_BINARY);

Results

The following table shows the results of applying binary thresholding on the input image with different threshold values.

Thresholding Results Thresholding Results Thresholding Results
thresh = 90 thresh = 140 thresh = 200

2. BINARY INVERSE

THRESH_BINARY_INV Binary inverse thresholding Binary inverse thresholding
threshold type rule output signal

If the intensity of the pixel src(x,y) is greater than thresh, then the new pixel intensity is set to 0, otherwise it is set to maxVal

C++ Code

threshold(src,dst, thresh, maxValue, THRESH_BINARY_INV);

Results

Applying inverse binary thresholding on the input image with different threshold values leads to the following results.

Inverse thresholding results Inverse thresholding results Inverse thresholding results
thresh = 90 thresh = 140 thresh = 200

3. TRUNCATE

THRESH_TRUNC Truncate Thresholding Truncate Thresholding Output
threshold type rule output signal

The maximum intensity value for the pixel is thresh. If src(x,y) is greater than thresh its value is truncated.

C++ Code

threshold(src,dst, thresh, maxValue, THRESH_TRUNC);

Results

The following table shows the result of applying Truncate thresholding to the input image. Note that all values above the threshold (e.g. 200) are set to the threshold value (e.g. 200), and all values less than or equal to the threshold are unchanged, maxValue is ignored.

Truncate Thresholding Result Truncate Thresholding Result Truncate Thresholding Result
thresh = 90 thresh = 140 thresh = 200

4. TO ZERO

THRESH_TOZERO Thresholding to zero Thresholding to zero
threshold type rule output signal

If src(x,y) is less than thresh, the new pixel intensity is set to 0. The maxValue is ignored.

C++ Code

threshold(src,dst, thresh, maxValue, THRESH_TOZERO);

Results

Thresholding to zero result 90 Thresholding to zero result 140 Thresholding to zero result 200
thresh = 90 thresh = 140 thresh = 200

5. TO ZERO INVERSE

THRESH_TOZERO_INV Thresholding to zero inverse Thresholding to zero inverse output
threshold type rule output signal

If src(x,y) is greater than thresh the new pixel value is set to 0

C++ Code

threshold(src,dst, thresh, maxValue, THRESH_TOZERO_INV);

Results

The following table shows the results of applying “Inverted Threshold to Zero” to the input image. The artifacts on the boundaries are because some of the pixels are below and others above the threshold, resulting in an interrupted border where the pixels are above the threshold.

Thresholding to zero inverse result 90 Thresholding to zero inverse result 140 Thresholding to zero inverse result 200
thresh = 90 thresh = 140 thresh = 200

So this was the introduction on Simple Thresholding. In Part 2 of “Thresholding for Mobile OCR: An Introduction” we will give you an insight on Otsu Thresholding and Adaptive Thresholding!

QUESTIONS? LET US KNOW!

If you have questions, suggestions or feedback on this, please don’t hesitate to reach out to us via FacebookTwitter or simply via [email protected]yline.com! Cheers!