Thresholding
This example shows how to binarize a grayscale image with three different algorithms available in ImageDev.
The first binarization algorithm applied in this example is Thresholding. It simply sets each pixel having an
intensity between 60 and 255 to 1. Other pixels are set to 0.
The second binarization algorithm is ThresholdingByCriterion. It is very similar to Thresholding. Only one value is used to binarize the input image. A criterion is also used to decide whether a pixel must be set to 1 or 0, by comparing its intensity to the threshold value. In this example, intensities greater than 40 are set to 1.
At last, an AutoThresholdingBright algorithm is applied. This algorithm computes automatically a threshold by analyzing the input image histogram and sets the pixels having an intensity greater than this value to 1. Several methods are available to compute this threshold, this example uses the Otsu method. The selected threshold is returned in the output structure and printed in the standard output.
Figure 1. Thresholding algorithms (a) the initial image, (b) thresholding between 60 and 255,
(c) thresholding pixel intensities greater than 40, and (d) automatic thresholding with the Otsu method
Each thresholding result is saved in the project directory. As the binarization algorithms produce images with intensities between 0 and 1, they are normalized between 0 and 255 beforehand to visualize them correctly with a standard image viewer.
See also
The second binarization algorithm is ThresholdingByCriterion. It is very similar to Thresholding. Only one value is used to binarize the input image. A criterion is also used to decide whether a pixel must be set to 1 or 0, by comparing its intensity to the threshold value. In this example, intensities greater than 40 are set to 1.
At last, an AutoThresholdingBright algorithm is applied. This algorithm computes automatically a threshold by analyzing the input image histogram and sets the pixels having an intensity greater than this value to 1. Several methods are available to compute this threshold, this example uses the Otsu method. The selected threshold is returned in the output structure and printed in the standard output.
![]() (a) |
![]() (b) |
![]() (c) |
![]() (d) |
(c) thresholding pixel intensities greater than 40, and (d) automatic thresholding with the Otsu method
Each thresholding result is saved in the project directory. As the binarization algorithms produce images with intensities between 0 and 1, they are normalized between 0 and 255 beforehand to visualize them correctly with a standard image viewer.
#include <ImageDev/ImageDev.h> #include <ioformat/IOFormat.h> #include <string.h> using namespace imagedev; using namespace ioformat; int main( int argc, char* argv[] ) { int status = 0; try { // ImageDev library initialization if ( imagedev::isInitialized() == false ) imagedev::init(); // Open a tif file to binarize auto imageInput = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "objects.tif" ); // Threshold image pixels having an intensity between 60 and 255 std::cout << "Thresholding between a minimum and maximum value..." << std::endl; auto imageOutput = thresholding( imageInput, { 60, 255 } ); // Rescale the binary image intensities between 0 and 255 for visualization and save it imageOutput = rescaleIntensity( imageOutput, RescaleIntensity::OutputType::UNSIGNED_INTEGER_8_BIT, RescaleIntensity::RangeMode::MIN_MAX, { 2, 98 }, { 0, 255 }, { 0, 255 } ); ioformat::writeView( imageOutput, "T03_02_manual.png" ); // Threshold image pixels having an intensity greater than 60 std::cout << "Thresholding by comparison to a value..." << std::endl; imageOutput = thresholdingByCriterion( imageInput, ThresholdingByCriterion::ComparisonCriterion::GREATER_THAN_OR_EQUAL_TO, 40 ); // Rescale the binary image intensities between 0 and 255 for visualization and save it imageOutput = rescaleIntensity( imageOutput, RescaleIntensity::OutputType::UNSIGNED_INTEGER_8_BIT, RescaleIntensity::RangeMode::MIN_MAX, { 2, 98 }, { 0, 255 }, { 0, 255 } ); ioformat::writeView( imageOutput, "T03_02_criterion.png" ); // Threshold image pixels automatically with Otsu's method std::cout << "Automatic thresholding with Otsu method..." << std::endl; auto result = autoThresholdingBright( imageInput, AutoThresholdingBright::RangeMode::MIN_MAX, { 0, 255 }, AutoThresholdingBright::ThresholdCriterion::FACTORISATION ); std::cout << "Otsu threshold value = " << result.outputMeasurement->threshold( 0 ) << std::endl; // Rescale the binary image intensities between 0 and 255 for visualization and save it imageOutput = rescaleIntensity( result.outputBinaryImage, RescaleIntensity::OutputType::UNSIGNED_INTEGER_8_BIT, RescaleIntensity::RangeMode::MIN_MAX, { 2, 98 }, { 0, 255 }, { 0, 255 } ); ioformat::writeView( imageOutput, "T03_02_auto.png" ); std::cout << "This example ran successfully." << std::endl; } catch ( const imagedev::Exception& error ) { // Print potential exception in the standard output std::cerr << "ImageDev exception: " << error.what() << std::endl; status = -1; } // ImageDev library finalization imagedev::finish(); // Check if we must ask for an enter key to close the program if ( !( ( argc == 2 ) && strcmp( argv[1], "--no-stop-at-end" ) == 0 ) ) std::cout << "Press Enter key to close this window." << std::endl, getchar(); return status; }
See also