Histogram-Based Single Thresholding

This code snippet shows how to perform a minimum residue thresholding operation based on an histogram.

C++

#include <eVision/EasyImage/EasyImage.h>
using namespace eVision;
// Images constructor
EImageBW8 srcImage;
EImageBW8 dstImage;
// Histogram constructor
EBWHistogramVector histo;
// Compute the histogram
EasyImage::Histogram(&srcImage, &histo);
// Compute the single threshold (and the average pixel values below and above the threshold)
EHistogramThreshold histThreshold = EasyImage::HistogramThreshold(histo, (uint32_t)EThresholdMode_MinResidue);
// Source and destination images must have the same size
dstImage.SetSize(&srcImage);
// Perform the single thresholding
EasyImage::Threshold(&srcImage, &dstImage, histThreshold.Threshold);

C#

using eVision;
// Images constructor
EImageBW8 srcImage = new EImageBW8();
EImageBW8 dstImage = new EImageBW8();
// Histogram constructor
EBWHistogramVector histo = new EBWHistogramVector();
// Compute the histogram
EasyImage.Histogram(srcImage, histo);
// Compute the single threshold (and the average pixel values below and above the threshold)
EHistogramThreshold histThreshold = EasyImage.HistogramThreshold(histo, unchecked((uint)EThresholdMode.MinResidue));
// Source and destination images must have the same size
dstImage.SetSize(srcImage);
// Perform the single thresholding
EasyImage.Threshold(srcImage, dstImage, histThreshold.Threshold);

Python

import eVision
# Images constructor
src_image = eVision.EImageBW8()
dst_image = eVision.EImageBW8()
# Histogram constructor
histo = eVision.EBWHistogramVector()
# Compute the histogram
eVision.EasyImage.Histogram(src_image, histo)
# Compute the single threshold (and the average pixel values below and above the threshold)
histThreshold = eVision.EasyImage.HistogramThreshold(histo, eVision.EThresholdMode.MinResidue)
# Source and destination images must have the same size
dst_image.SetSize(src_image)
# Perform the single thresholding
eVision.EasyImage.Threshold(src_image, dst_image, histThreshold.Threshold)