Histogram ThresholdingCognex VisionPro

This topic describes the process of histogram threshold computation. Histogram threshold computation computes a threshold that is appropriate for segmenting an image into object and background pixels; it is often used to compute a segmentation threshold for blob analysis.

Some Useful Definitions

This section defines some terms and concepts used in this topic.

TermDefinition
bimodal histogramA histogram that has two peaks or modes.
binAn element in the histogram's array of integers.
histogramA one-dimensional function of pixel values. A histogram can be represented as an array of integers. The value at element n in the array is the number of pixels in the image with pixel value equal to n .
multimodal histogramA histogram that has multiple peaks or modes.
thresholdA value that divides a histogram into two groups of pixel values.
varianceA statistical measure of the spread of data.
within-group varianceThe weighted sum of the variances for groups of pixel values.
Threshold Computation Overview

Histogram threshold computation computes a threshold that is appropriate for segmenting an image into object and background pixels; it is often used to compute a segmentation threshold for blob analysis.

Minimizing Within-Group Variance

VisionPro’s histogram threshold computation function uses a thresholding technique called minimizing within-group variance (WGV). This technique is particularly useful for obtaining a threshold value for noisy images, saturated images, or multimodal histograms.

The following figure shows an image and its associated histogram:

Search Blob Theory Histogram WGV splash

Any threshold t divides the pixel values in this histogram into two groups. As shown below, the pixel values in the resulting groups can be represented by two new histograms where

Search Blob Theory Histogram WGV variance 1

and

Search Blob Theory Histogram WGV variance 2

are the respective variances for the groups of pixel values in each of the new histograms.

Search Blob Theory Histogram WGV split

The optimum threshold divides the histogram into two groups such that each group has the minimum within-group variance. For any given threshold, the within-group variance

Search Blob Theory Histogram WGV wgv

is defined by the weighted sum of the variances of the two groups:

Search Blob Theory Histogram WGV wgv equation

where the weight factors

Search Blob Theory Histogram WGV weightfactor 1

and

Search Blob Theory Histogram WGV weightfactor 2

equal the number of pixels in each group divided by the total number of pixels in the image.

The histogram threshold computation function evaluates the within-group variance for all possible threshold values, and returns the threshold that yields the minimum within-group variance. If more than one threshold minimizes the within-group variance, the function returns the smallest value.

The following figure shows the computed threshold for the illustrated histogram and image.

Search Blob Theory Histogram WGV computed

Note: If the supplied histogram contains only one nonzero value, the WGV technique cannot be applied. In this case, the function returns the index of the nonzero value as the threshold.

Calculating a Threshold

To calculate a threshold, use GetMinimumWGVThreshold. If you specify a partial histogram, it must have at least two nonzero bins between the left and right indices. The threshold computation includes the left index value but excludes the right index value. The following figure shows how you might obtain a threshold for a portion of a multimodal histogram:

Search Blob Theory Histogram WGV range

Specifying a portion of a multimodal histogram

Threshold Results

In addition to returning the computed threshold, the function also returns a score value in the range 0.0 through 1.0. Higher scores indicate better separation between the two groups determined by the threshold. A score of zero means that the histogram contains only one nonzero bin.

The following figure shows the separation between groups associated with various scores.

Search Blob Theory Histogram WGV scores