| Customize Help

Dealing with color



MIL supports grabbing, displaying, accessing, and processing color images.

MIL can represent an object in color with a single color buffer, allocated with MbufAllocColor().

Grabbing

You grab from a color video source (typically a camera) into a color image buffer, as you would into a grayscale image buffer, by calling MdigGrab() or MdigGrabContinuous().

Before performing a color grab, you must allocate a digitizer, using MdigAlloc() (or MappAllocDefault()), specifying a digitizer configuration format (DCF) appropriate for your color camera. In addition, the digitizer and the image buffer must be allocated on the same system and have compatible dimensions. Once you have finished using the digitizer, you should free it, using MdigFree().

When grabbing from a color digitizer, each color component is transmitted simultaneously. The destination buffer must have the same number of color bands as the digitizer. The data is simultaneously stored in the appropriate component of the image buffer. When grabbing RGB, the red component is stored in the first color band, the green component is stored in the second color band, and the blue component is stored in the third color band.

If the hardware permits, you can control the digitization reference level of each acquisition path of the digitizer, using MdigControl() with the values in the For the general reference settings table.

Note, upon installation, if you specified a color camera, the default image buffer, allocated with MappAllocDefault(), will be a three-band color image buffer. If you didn't specify a color camera, but would now prefer to use one, you can update your defaults using the MILConfig utility.

Mapping grabbed data through a LUT

You can also correct or precondition input data by mapping it through a lookup table (LUT) upon acquisition (if the hardware permits). This requires that you associate a LUT buffer with the digitizer, using MdigControl() with M_LUT_ID.

You can associate either a single-band LUT buffer or a LUT buffer that has the same number of color bands as the digitizer. If you associate a single-band LUT buffer with the digitizer, each component of the digitizer LUT is loaded with the same data. If you associate a multi-band LUT buffer with the digitizer, each component of the digitizer LUT is loaded with the data from corresponding color band of the LUT buffer.

To disassociate the LUT buffer from the digitizer, you need to re-associate the default LUT with the digitizer, using MdigControl() with M_LUT_ID set to M_DEFAULT.

Displaying

You display a color-image buffer as you would a two-dimensional grayscale image buffer. You must first allocate the image buffer with a displayable attribute (M_DISP), and then select it for display, using MdispSelect(). To stop displaying the image buffer and leave the display blank, use MdispSelect() with M_NULL.

Before you can display a buffer, the display must be allocated, using MdispAlloc() (or MappAllocDefault()). The image buffer and the display should be allocated on the same system.

When you display a color image buffer (usually RGB), the first band is routed to the first output channel (usually red), the second band is routed to the second output channel (usually green), while the third band is routed to the third output channel (usually blue).

Managing color images

MIL supports the saving and loading of color images from disk in different file formats. See the MbufSave(), MbufLoad(), MbufRestore(), MbufImport(), and MbufExport() function reference descriptions for more details.

Note, all the MIL data allocation, access, and generation (Mbuf...() and MgenLut...()) functions can handle color image buffers.

Color processing and analysis

To process and analyze color images, MIL offers many solutions. The Color Analysis module, for example, accepts color images and allows you to perform color-based procedures such as transforming color data using relative color calibration, matching colors, calculating the difference between colors, and separating colors. The Image Processing module also accepts color images and offers a wide variety of operations that allow you to perform image enhancements, distortion corrections, and basic analysis. Unlike the Color Analysis module, the Image Processing module processes each band one at a time.

Most analysis modules usually require 1-band monochrome images. If you have a color image and you want to use it with a module that does not support color, you must do one of the following: convert the color image to grayscale, create a child buffer from one of the three color bands, or copy one of the three color bands to a 1-band buffer. After performing these operations and storing the transformed data in a monochrome buffer, you can pass it to an analysis module.

Converting color images to grayscale

You can use MimConvert() to convert color images to grayscale using M_RGB_TO_L, where L represents the luminance (L) component of the HSL color space, or using M_RGB_TO_Y, where Y represents the luminance (Y) component of the YUV color space. You can then use the converted image with an analysis module that does not support color.

You can also convert color images to grayscale using McolProject() with M_PRINCIPAL_COMPONENT_PROJECTION which, unlike MimConvert(), does not simply extract a single component of the color space. Instead, it uses the distribution of the image's color data to calculate the best grayscale conversion possible, minimizing the loss of information. This results in a grayscale image that can better differentiate the color in the original source image.

For more information on converting color images to grayscale, see the Converting to grayscale section of Chapter 19: Color processing and analysis.

Creating a child buffer from one of the three color bands

Since most analysis modules usually require 1-band monochrome images, you can use MbufChildColor() to allocate a 1-band child buffer within a 3-band color parent buffer. For example, if you have an RGB parent buffer, and use MbufChildColor() with M_RED, you will have a 1-band child buffer which contains the red color band of its parent. You can also use MbufChildColor2d() to select a 2-dimensional region in one of the color bands of the parent buffer.

For more information, see the Child buffers subsection of the Using child buffers, ROIs, or a copy to manipulate specific data areas section of Chapter 21: Data buffers.

Copying one of the three color bands to a 1-band buffer

Instead of using a child buffer to create a 1-band monochrome image from a color image, you can use MbufCopyColor() to copy one band of a 3-band color image buffer. For example, if you have an RGB source image buffer, and use MbufCopyColor() with M_RED, you will have a 1-band destination buffer which contains the red color band of its source. You can also use MbufCopyColor2d() to select a 2-dimensional region of the source buffer from which to copy.

For more information, see the Copying data from one buffer to another subsection of the Managing data buffers section of Chapter 21: Data buffers.

Color space conversion

You can convert color data between HSL and RGB color spaces using MimConvert() with M_HSL_TO_RGB or M_RGB_TO_HSL. If using the MIL Color Analysis module, you can call McolSetMethod() to convert RGB to LAB or HSL before performing McolMatch().

You can also use MimConvert() to convert sRGB to LAB (M_SRGB_TO_LAB), and to convert LAB to sRGB (M_LAB_TO_SRGB). In these cases, your RGB data must adhere to standard RGB specifications, referred to as sRGB, as defined by the International Electrotechnical Commission (IEC) Project Team 61966-2-1. MimConvert() allows many variations with which to convert to and from sRGB. For example, you can specify the LCH color space (a reinterpretation of LAB) and you can specify whether color data is linear (gamma corrected). For more information, see the Color spaces and converting between them section of Chapter 19: Color processing and analysis.

MimConvert() is available with MIL-Lite, while McolSetMethod() requires the full version of MIL.