This topic contains the following sections.
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.
This section defines some terms and concepts used in this topic.
| Term | Definition |
| bimodal histogram | A histogram that has two peaks or modes. |
| bin | An element in the histogram's array of integers. |
| histogram | A 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 histogram | A histogram that has multiple peaks or modes. |
| threshold | A value that divides a histogram into two groups of pixel values. |
| variance | A statistical measure of the spread of data. |
| within-group variance | The weighted sum of the variances for groups of pixel values. |
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.
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:

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

and

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

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

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

where the weight factors

and

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.

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.
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:

Specifying a portion of a multimodal histogram
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.
