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) |
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; }
using System; using ImageDev; using IOLink; using IOFormat; namespace T03_02_Thresholding { class Program { static void Main(string[] args) { int status = 0; try { // Initialize the ImageDev library if not done if (Initialization.IsInitialized() == false) Initialization.Init(); // Open a tif file to binarize ImageView imageInput = ViewIO.ReadImage("Data/images/objects.tif") as ImageView; // Threshold image pixels having an intensity between 60 and 255 Console.WriteLine("Thresholding between a minimum and maximum value..."); ImageView imageOutput = Processing.Thresholding(imageInput, new double[] { 60, 255 }) as ImageView; // Rescale the binary image intensities between 0 and 255 for visualization and save it imageOutput = Processing.RescaleIntensity(imageOutput, RescaleIntensity.OutputType.UNSIGNED_INTEGER_8_BIT); ViewIO.WriteView(imageOutput, "T03_02_manual.png"); // Threshold image pixels having an intensity greater than 40 Console.WriteLine("Thresholding by comparison to a value..."); imageOutput = Processing.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 = Processing.RescaleIntensity(imageOutput, RescaleIntensity.OutputType.UNSIGNED_INTEGER_8_BIT); ViewIO.WriteView(imageOutput, "T03_02_criterion.png"); // Threshold image pixels automatically with Otsu's method Console.WriteLine("Automatic thresholding with Otsu method..."); var result = Processing.AutoThresholdingBright(imageInput, AutoThresholdingBright.RangeMode.MIN_MAX, null, AutoThresholdingBright.ThresholdCriterion.FACTORISATION); Console.WriteLine("Otsu threshold value = " + result.outputMeasurement.threshold(0)); // Rescale the binary image intensities between 0 and 255 for visualization and save it imageOutput = Processing.RescaleIntensity(result.outputBinaryImage, RescaleIntensity.OutputType.UNSIGNED_INTEGER_8_BIT); ViewIO.WriteView(imageOutput, "T03_02_auto.png"); // Notify the garbage collector that the created images can be freed imageInput.Dispose(); imageOutput.Dispose(); } catch (Exception error) { // Print potential exception in the standard output System.Console.WriteLine("HelloImageDev exception: " + error.ToString()); status = -1; } // ImageDev library finalization Initialization.Finish(); // Check if we must ask for an enter key to close the program if (!((args.Length >= 1) && (args[0] == "--no-stop-at-end"))) { System.Console.WriteLine("Press Enter key to close this window."); System.Console.ReadKey(); } System.Environment.Exit(status); } } }
import imagedev import imagedev_data import ioformat # Initialize the ImageDev library if not done if (imagedev.is_initialized() == False): imagedev.init() # Open a tif file to binarize image_input = ioformat.read_image(imagedev_data.get_image_path("objects.tif")) # Threshold image pixels having an intensity between 60 and 255 print("Thresholding between a minimum and maximum value...") image_output = imagedev.thresholding(image_input, [ 60, 255 ]) # Rescale the binary image intensities between 0 and 255 for visualization and save it image_output = imagedev.rescale_intensity(image_output, imagedev.RescaleIntensity.UNSIGNED_INTEGER_8_BIT) ioformat.write_view(image_output, "T03_02_manual.png") # Threshold image pixels having an intensity greater than 40 print("Thresholding by comparison to a value...") image_output = imagedev.thresholding_by_criterion(image_input, comparison_value = 40) # Rescale the binary image intensities between 0 and 255 for visualization and save it image_output = imagedev.rescale_intensity(image_output, imagedev.RescaleIntensity.UNSIGNED_INTEGER_8_BIT) ioformat.write_view(image_output, "T03_02_criterion.png") # Threshold image pixels automatically with Otsu's method print("Automatic thresholding with Otsu method...") result = imagedev.auto_thresholding_bright(image_input, threshold_criterion=imagedev.AutoThresholdingBright.FACTORISATION) # Rescale the binary image intensities between 0 and 255 for visualization and save it image_output = imagedev.rescale_intensity(result[0], imagedev.RescaleIntensity.UNSIGNED_INTEGER_8_BIT) ioformat.write_view(image_output, "T03_02_auto.png") # ImageDev library finalization imagedev.finish()
See also