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


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 ThresholdingSimple Thresholding
input signalinput image


THRESH_BINARYBinary ThresholdingBinary Thresholding
threshold typeruleoutput 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);


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

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


THRESH_BINARY_INVBinary inverse thresholdingBinary inverse thresholding
threshold typeruleoutput 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);


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

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


THRESH_TRUNCTruncate ThresholdingTruncate Thresholding Output
threshold typeruleoutput 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);


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 ResultTruncate Thresholding ResultTruncate Thresholding Result
thresh = 90thresh = 140thresh = 200


THRESH_TOZEROThresholding to zeroThresholding to zero
threshold typeruleoutput 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);


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


THRESH_TOZERO_INVThresholding to zero inverseThresholding to zero inverse output
threshold typeruleoutput 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);


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 90Thresholding to zero inverse result 140Thresholding to zero inverse result 200
thresh = 90thresh = 140thresh = 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!


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]! Cheers!