| Customize Help

Advanced color matching settings and concepts



In addition to applying fundamental color matching specifications, you can also use advanced settings to write highly customized color matching applications. This includes color band selection, distance normalization, color space encoding, and the histogram match mode.

Color band specification settings

By default, all bands are used when performing the match operation. You can, however, specify one or two specific bands, using McolControl() with M_BAND_MODE.

For example, if you are using HSL images, you can match with only the hue (H) component by setting M_BAND_MODE to M_COLOR_BAND_0. This can be useful if your image has non-uniform lighting, shadows, or highlights. A similar match can be performed if you are matching in CIELAB and match with only the chrominance components (bands A and B), by setting M_BAND_MODE to M_COLOR_BAND_1 + M_COLOR_BAND_2.

Distance normalization settings

MIL calculates color distances (with McolMatch() or McolDistance()) as numerical floating-point values and internally stores them as such. These exact distance values are then written to the destination buffer, provided that this buffer is of type floating-point. If the buffer is of type integer, distance values are truncated; that is, due to the constraint of the buffer type, the fractional (decimal) portion of the distance result is not written.

However, before color distance results are written to the destination buffer, you can normalize them according to a multiplicative factor, using either the NormalizeValue parameter (for McolDistance()) or McolControl() with M_DISTANCE_IMAGE_NORMALIZE (for McolMatch()). You can specify a specific normalization value, or use M_MAX_NORMALIZE to normalize distances with the greatest calculated distance. This value corresponds to the result M_MAX_DISTANCE (McolGetResult()). To specify no normalization, use M_NO_NORMALIZE.

The normalization performed is as follows (when using M_MAX_NORMALIZE):

  • (Distance/GreatestCalculatedDistance) x MaximumPossibleValueOfDestBuf (for destination buffer's of type integer).

  • (Distance/GreatestCalculatedDistance) (for destination buffer's of type floating-point).

    If the destination buffer is of type floating-point, you would typically not normalize the distance results, since all this will do is return a value between 0.0 and 1.0.

The following example shows the distance between two colors (FirstSourceImage - SecondSourceImage) using no normalization (M_NO_NORMALIZE), and using maximum normalization (M_MAX_NORMALIZE).

Distance results are always clipped at the destination buffer's maximal possible value, which can occur when specifying M_NO_NORMALIZE or a specific normalization factor. For example, when using an 8-bit unsigned destination buffer, distances higher than 255 will be written as 255. Note that if you specify a destination buffer of type floating-point, distance results would almost never be greater than the buffer's maximal possible value, since its limit is extremely high.

Once normalized, the resulting value (grayscale) written in the destination buffer does not represent a precise distance (between colors) and should never be interpreted as such. You can however make the general conclusion that the higher (brighter) this grayscale value is, the greater the color distance was. Normalization can therefore be seen as a way to remap distance values according to the destination buffer's dynamic range to minimize the loss of data and to obtain a buffer that you can then use for other types of grayscale processing, such as thresholding and blob analysis.

Normalization is applied when drawing the distance image (M_DRAW_DISTANCE), using either McolDraw() or McolMatch(). This can typically improve the display and visualization of this image. Note that since MIL internally stores distance values as 32-bit float, you can still draw the distance image in an unsigned image buffer of type integer.

Color space encoding

Color space encoding (of the source color space) determines how color is transformed from the range represented in an image buffer to its native (theoretical) range, which is device independent. For example, RGB color space data is represented in an image buffer as values between 0 and 255 (8-bit); these values are then mapped to their native data range, which consists of all real numbers between 0 and 1.

You must set the color space encoding according to the actual dynamic range of your color data, using McolControl() with M_ENCODING. By default, the Color Analysis module assumes that you are using an 8-bit color space encoding, regardless of the depth of your buffers. If this default is not appropriate, you should modify it with M_ENCODING accordingly. For example, if your color data was acquired with a 16-bit camera, you should set M_ENCODING to M_16BIT.

A number of predefined color space encoding settings are provided (M_nBIT). These are typically sufficient for most applications. You can even use image buffers that exceed the actual dynamic range of your color data, provided that their content respects this range. For example, if you set M_ENCODING to M_8BIT, you can use 16-bit image buffers that contain values between 0 and 255 (8-bit); however, if they contain values outside this range, results will be inconsistent.

If required, you can explicitly set, for each band, specific offset (M_OFFSET_BAND_n) and scale (M_SCALE_BAND_n) values that specify how the color data should be transformed from the range represented in an image buffer to its native (theoretical) range. In this case, you must set M_ENCODING to M_USER_DEFINED.

Performing the encoding

To actually encode the data, the following native range is used for the color spaces:

RGB

Color space band

Native Min

Native max

R

0.0

1.0

G

0.0

1.0

B

0.0

1.0

LAB

Color space band

Native Min

Native max

L

0.0

100.0

A

-128.0

127.0

B

-128.0

127.0

HSL

Color space band

Native Min

Native max

H

0.0

1.0 (normalized angle)

S

0.0

1.0

L

0.0

1.0

If you use M_nBIT, the Color Analysis module takes the color space's native range, and the range represented in the image buffer, and uses it to internally apply the following offset and scale, to perform the encoding:

Offset

Color Space

M_ENCODING

M_OFFSET_BAND_0

M_OFFSET_BAND_1

M_OFFSET_BAND_2

RGB

M_nBIT

0

0

0

CIELAB

M_8BIT

0

128

128

M_nBIT

0

128 x (2 n -1) / 255

128 x (2 n -1) / 255

HSL

M_nBIT

0

0

0

Scale

Color Space

M_ENCODING

M_SCALE_BAND_0

M_SCALE_BAND_1

M_SCALE_BAND_2

RGB

M_nBIT

1.0 / (2 n -1)

1.0 / (2 n -1)

1.0 / (2 n -1)

CIELAB

M_nBIT

100.0 / (2 n -1)

255 / (2 n -1)

255 / (2 n -1)

HSL

M_nBIT

1.0 / (2 n )

1.0 / (2 n -1)

1.0 / (2 n -1)

In this table, n refers to the buffer's depth.

Note that the H component in HSL is represented by a normalized angle, which is transformed according to the MIL angle convention. For example, in an 8-bit image buffer, 360° is mapped to 256. Since 0° and 360° are the same angle, both use the same numerical value, which is 0.

If you use M_USER_DEFINED, the following encoding is applied using the specified offset (M_OFFSET_BAND_n) and scale (M_SCALE_BAND_n), for each band:

  1. The offset (M_OFFSET_BAND_n) is subtracted from the color value represented by the image buffer.

  2. The resulting color value, as modified by M_OFFSET_BAND_n, is then multiplied by M_SCALE_BAND_n.

To determine the scale and offset values that you should use with M_USER_DEFINED, you can perform the following calculations:

Where:

  • [n1, n2] is the native range of the color space.

  • [e1, e2] is the numerical range of the image buffer.

Histogram matching concepts

M_HISTOGRAM_MATCHING mode uses histogram bins as the basis of the matching operation. A frequency is associated with each bin, corresponding to the number of occurrences of the bin's color triplets within the image or color-sample.

You must specify the number of bins for each color band using McolControl() with M_NB_BINS_BAND_n. The total number of histogram bins contained within the color space is the product of the number of bins located along each color band. For example, if M_NB_BINS_BAND_0 is set to 3, M_NB_BINS_BAND_1 is set to 4, and M_NB_BINS_BAND_2 is set to 5, the total number of histogram bins is 3 * 4 * 5 = 60 bins.

The histogram matching is performed according to the steps described below:

  1. The color histograms of the target image and predefined color-samples are generated in the color space used to perform the matching.

  2. The frequency of each bin is calculated based on the individual frequencies of its constituent pixels. The bin frequencies are then normalized between 0.0 and 1.0, with 1.0 representing the sum of the frequencies of all bins.

  3. Matching occurs between the two bins with the highest frequencies, provided their color distance is within the distance specified using McolControl() with M_DISTANCE_TOLERANCE. The match score S is incremented according to the formula Sn = Sn-1 + min(f bin1, f bin2)*(1 - colorDistance(colorbin1, colorbin2)), where:

    • f represents a normalized bin frequency (0.0 <= f <= 1.0).

    • colorDistance corresponds to the normalized color distance between the two specified colors, within the color space used for matching (0.0 <= colorDistance <= 1.0).

  4. The lesser of the two bin frequencies is then subtracted from both bin frequencies, resulting in the lesser bin being completely emptied.

  5. Steps 3 and 4 are repeated until no more bins can be matched.

  6. The final match score is normalized as a percentage between 0.0 and 100.0, with a score of 100.0 indicating a perfect match.

The relevance score for the matching operation is calculated according to the formula RelevanceScore = (100.0 - BestScore)-1 / sum((100.0 - Score)-1 ), where:

  • BestScore refers to the score of the winning color-sample.

  • The sum is taken over all the color-samples, and Score refers to the score of the sample being summed.

You can also perform histogram matching using McolSetMethod() with M_HISTOGRAM_VOTE. In this case, MIL computes color histograms for each defined color-sample and, for each target pixel, identifies its histogram bin. Each target pixel votes for the best color-sample, provided its histogram bin is not empty. The number of votes that a color-sample accumulates determines its score. The color-sample with the highest score above the acceptance (McolControl() with M_ACCEPTANCE) is the target area's best-matched color-sample.