Core Optical, Inc.
HistogramProcessor GetEqualizedHistogramImageArray Method Industrial Strength, Scientific Grade
Applies the Histogram Equalization algorithm to the source data and returns an array of pixel values portraying the result. The original data is not altered.

Namespace: PrecisionImage.HistogramProcessing
Assembly: PrecisionImage (in PrecisionImage.dll) Version: (

public Array GetEqualizedHistogramImageArray(
	SourceData sourceData,
	int channel,
	HistogramResolution histogramResolution = HistogramResolution.EightBit,
	Int32Rect regionOfInterest = null,
	BinaryMask binaryMask = null,
	bool zeroOutMaskArea = true,
	GammaEncoding gammaEncoding = GammaEncoding.None


Type: PrecisionImage SourceData
A SourceData type containing the image data.
Type: OnlineSystem Int32
An OnlineInt32 type indicating the data channel to process.
histogramResolution (Optional)
Type: PrecisionImage.HistogramProcessing HistogramResolution
A HistogramResolution enumeration specifying the resolution of the histogram used internally by the algorithm when characterizing the source data. This parameter is optional and defaults to 8-bit histogram resolution.
regionOfInterest (Optional)
Type: OnlineSystem.Windows Int32Rect
An OnlineInt32Rect type indicating the region of interest. Coordinates are zero-index based. This parameter is optional and defaults to the entire image.
binaryMask (Optional)
Type: PrecisionImage.BinarizationAndMorphology BinaryMask
A BinaryMask type specifying a mask indicating areas of the image to bypass. Masked regions are not altered, nor are they used internally when determining the initial histogram. This parameter is optional.
zeroOutMaskArea (Optional)
Type: OnlineSystem Boolean
A OnlineBoolean indicating whether to the masked areas are zeroed in the output pixel array. If True, the masked region of the data appears black. If True, the masked area of the region is inserted unchanged in the resulting image array. This parameter is optional and defaults to True.
gammaEncoding (Optional)
Type: PrecisionImage GammaEncoding
A GammaEncoding enumeration specifying the gamma encoding of the unmasked region. If the unmasked region is to be zeroed out in the resulting image array this parameter is ignored. This parameter is optional and defaults to GammaEncoding.None

Return Value

A type OnlineArray containing the pixel values portraying the processed data.

OnlineSystem ArgumentNullException Thrown when sourceData is null..
OnlineSystem ArgumentOutOfRangeException Thrown when sourceData contains less than 1 data channel.
OnlineSystem ArgumentOutOfRangeException Thrown when channel is negative or out of range.
OnlineSystem ArgumentOutOfRangeException Thrown when regionOfInterest specifies a rectangular region not completely contained within the dimensions of the source data.
OnlineSystem ArgumentOutOfRangeException Thrown when the number of rows and/or columns in binaryMask do not match those of sourceData.

Use this method in conjunction with a OnlineWriteableBitmap or other type to generate the histogram-equalized image of the source data while avoiding the overhead associated with generating a new OnlineBitmapSource object. The returned OnlineArray object should be cast as either a Byte[] or UInt16[], depending on the histogramResolution argument i.e. cast as Byte[] if EightBit is specified for histogramResolution, otherwise cast as UInt16[].

If a region of interest was specified, the returned OnlineArray portrays the processed data of the specified region only. The OnlineArray object has length equal to the product of the region of interest width and height. Otherwise, the returned OnlineArray object has the same dimensions as the product of the source data rows and columns. The stride of the array (in pixels) is equal to either the region of interest width or source data columns, depending on whether the regionOfInterest parameter was used.

Histogram equalization is an inherently discrete operation i.e. the data is discretized into a series of histogram bins before and after equalization. To ensure optimum results and minimize discretization error, specify a HistogramResolution that most closely resembles the nature of the original data. For example, if the sourceData is originally sourced from a 16-bit image, specify histogramResolution to be SixteenBit.


The following example retrieves an array containing the histogram-equalization result from a central poriton of a 16-bit grayscale image and writes the result into a OnlineWriteableBitmap for display:

using PrecisionImage;
using PrecisionImage.HistogramProcessing;

// Read the source image from disk and load into a SourceData object: 
Uri imageUri                   = new Uri("Toxicysts.tif", UriKind.Relative);
TiffBitmapDecoder imageDecoder = new TiffBitmapDecoder(imageUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource SourceImage       = imageDecoder.Frames[0];
SourceData imageData           = new SourceData(SourceImage, GammaEncoding.None);

// Instantiate a WriteableBitmap from the original SourceImage: 
WriteableBitmap writeableBitmap = new WriteableBitmap(SourceImage);

// Define central region of image as the region of interest: 
Int32 RowStart    = writeableBitmap.PixelHeight / 4;
Int32 ColumnStart = writeableBitmap.PixelWidth / 4;
Int32Rect regionOfInterest = new Int32Rect(ColumnStart, RowStart, writeableBitmap.PixelWidth / 2, writeableBitmap.PixelHeight / 2);

// Histogram-equalize the region of interest portion of the data and acquire the array of updated pixel values: 
HistogramProcessor histProcessor = new HistogramProcessor();
Array pixelArray = histProcessor.GetEqualizedHistogramImageArray(imageData, 0, HistogramResolution.SixteenBit, regionOfInterest);

// Write the modified region of interest into the WriteableBitmap: 
Int32Rect sourceBufferRect = new Int32Rect(0, 0, regionOfInterest.Width, regionOfInterest.Height);
writeableBitmap.WritePixels(sourceBufferRect, pixelArray, sourceBufferRect.Width * 2, regionOfInterest.X, regionOfInterest.Y);

// Display modified image:
image0.Source = writeableBitmap;

The above example generates an image similar to the following:

See Also