Thresholding images means reducing each pixel to a certain range of values. Some operations can be performed more efficiently on thresholded images. Images with full grayscale levels are useful for some tasks, but have redundant information for others. The MIL package provides the following types of threshold operations:
Binarizing, using MimBinarize().
Adaptive binarizing, using MimBinarizeAdaptive().
Clipping, using MimClip().
A binarizing operation reduces an image to two grayscale values by comparing each pixel value in the image against one or two specified threshold values (for example, whether each pixel value is above a threshold value, or within the range of two threshold values). Pixels that meet the specified condition are typically set to the maximum possible value of the image (for example, 255 if the image is 8-bit or 1 if the image is a floating-point buffer), while other pixels are set to 0. Binary images are useful when trying to identify geometric patterns and objects in your image since they are not cluttered with shading information.
When using MimBinarize(), it is important to select threshold values that preserves the required information. For example, in the previous image, an incorrect threshold value might inappropriately change pixels of particles into background pixels, resulting in fewer particles than the number that actually exist.
If you are unable to properly preserve the required information when binarizing your images with MimBinarize(), try an adaptive binarization with MimBinarizeAdaptive(). Though typically longer to execute, an adaptive binarization is more robust than a conventional binarization, particularly when dealing with poor or varying illumination or contrast. For more information, see the Adaptive binarizing section later in this chapter.
If the binarizing condition uses only one threshold value, MimBinarize() can automatically determine the threshold value from the source image's characteristics. It can establish the threshold using one of the following threshold modes:
Bimodal.
Triangular bisection.
In bimodal threshold mode (using M_BIMODAL), a histogram of the source image is internally generated. Then, if the histogram contains only two peaks, the threshold value is set to the intensity value at the lowest point between these two peaks, on the assumption that these peaks represent the object and the background. If the histogram contains more than two peaks, the threshold value will typically be set between the intensity values at the two principal peaks, although exceptions exist for the situations where the principal peaks occur closest to the minimum intensity value (pure black) or the maximum intensity value (full saturation). Note that the bimodal threshold mode works best when the histogram contains two visible peaks.
Regardless of the number of threshold values that the binarize condition uses, you can pass MimBinarize() the threshold values manually. You can manually pass them in one of the following threshold modes:
Percentile.
Fixed.
The percentile threshold mode (specified using M_PERCENTILE_VALUE) allows you to specify the threshold value(s) for the condition as a percentage of the histogram data. This mode uses an internally-generated cumulative histogram, whereby the X-axis represents the intensity value and the Y-axis represents the percentage of pixels equal to or less than the intensity value.
In this mode, you specify a threshold value by specifying the percentage of pixels that must be equal to or less than the required threshold value. For example, if the specified value is 90, the cumulative histogram shown below depicts that 90% of the values are below 170. Therefore, the threshold value is 170.
Alternatively, you can use the fixed threshold mode (with M_FIXED) to explicitly specify the threshold value(s) manually.
Clipping changes the image data less dramatically than binarizing. It changes the data to include only the range of pixel values in which you are interested. MimClip() takes a condition with at most two threshold points and replaces only those pixels that meet the condition with given values. Pixels that do not meet the condition are unaffected.
This can be useful to change data from one data type to another. For example, if you have a 16-bit result, but most of the pixels are less than 256, you could clip the result into an 8-bit buffer, and set all the pixels that are too big to the largest possible value, that is, 255.