Image Types and Properties (Image Graphics) (MATLAB)

Whenever we deal with images we need to be aware that there are fundamentally three types of image data formats. You have already seen some of this in the previous examples. The first image data format is indexed images where two arrays are used to describe the image. The second image data format is intensity level images, comprised of a single array where each element indicates the relative intensity of a pixel. The third data format is called truecolor and uses three intensity level arrays, where each is the relative intensity of red, green, and blue primary colors.

Indexed Images

By indexed we mean that the image is created from information in two arrays: the first is an array of indexes into the second, which is a three-column array containing the red, green, and blue contributions for each pixel. The following code will load an image of a clown and display it in a Figure Window, looking like that shown in Figure 5.6.

tmp8414451_thumb

 

Who is that clown?


Figure 5.6 Who is that clown?

When we load clown.mat the arrays X and map were placed in the MATLAB workspace. The array X is an array of indices; row indexes actually, specifying which row from map (an 81×3 array) the pixel color is to be taken from. Looking at a few pixels from the upper left corner, we see

tmp8414453_thumb

Each element is a number corresponding to a row in the color map. We can find the color components for each of these pixels by

tmp8414454_thumb

The unique function returns only one instance of an element, and orders the results, so the first row here corresponds to an index value of 2, the second 44, the third 55, etc.. We can see these first few pixels, shown in Figure 5.7, with the following code.

tmp8414455_thumb

 

The upper left 5x5 of the clown image.

Figure 5.7 The upper left 5×5 of the clown image.

Try this.

tmp8414457_thumb

Use unique again to see how many different colors are used in this portion of the image.

tmp8414458_thumb

Intensity Level Images

An intensity image file does not provide a color map. Instead, the array describes the image by relative pixel amplitude. An example of such an image is the "raw" image from an imaging device such as a CCD (charge-coupled device) imager. These devices, being digital, assign a value to the pixel based on the intensity of the light falling on it . Most are either 8-bit or 16-bit devices, meaning that for any pixel the intensity range is either 28 (0-255) or 216 (065,535) where 0 is no light and the maximum is fully illuminated. When an image from such a device is viewed, we typically do so in shades of gray. However, since MATLAB uses color maps when it plots an image, intensity image or not, we need to tell MATLAB what kind of color map should be used. The default data type in MATLAB is 64-bit floating-point numbers, i.e., double precision. However most image data formats are designed to use no more file space than is necessary. It is indeed wasteful to use an 8-bit imaging device and store its output with 64-bit numbers. The MATLAB image functions typically will deal with images in their native format as either 8-bit (uint8) or 16-bit (uint16) unsigned integers.

When plotting intensity level images, you will find that MATLAB’s built-in color maps don’t always agree with the scale of your image. With the following code, a 256-level grayscale image is viewed using image along with the color map gray.

tmp8414459_thumb

Since the color maps in MATLAB are 64 levels, in this case, since there actually are 256 shades of gray, shades will be lost when using a built in color map as shown in Figure 5.8. Shades are lost since all indexes above 64 are mapped to the highest level in the map, in this case white.

 A 256-level gray scale image loses quality when used with a 64-level color map.

Figure 5.8 A 256-level gray scale image loses quality when used with a 64-level color map.

One simple solution is to create a 256-shades-of-gray color map in this manner:

tmp8414461_thumb

This will create a 256-shade color map and produce much better results as shown in Figure 5.9.

Same image using gray256.

Figure 5.9 Same image using gray256.

Another solution to the color map-scaling problem is the function imagsc, which serves the same purpose as image, but scales the image data to use the full extent of the color map. The following code will take an 8-bit intensity image X and scale it to the built-in MATLAB color map gray.

tmp8414463_thumb

Note that imagesc requires the image data to be of type double. Although upon close inspection once can discern loss of shades, since the color map has been used across the full extent of the image’s intensities, the results are quite good as shown in Figure 5.10.

The result of mapping a 256-gray-level image to 64 gray levels using imagesc.

Figure 5.10 The result of mapping a 256-gray-level image to 64 gray levels using imagesc.

Truecolor Images

Truecolor images are in essence a set of three intensity image arrays where one is red filtered, another green filtered, and the last one blue filtered. For any given pixel in the image, the corresponding element from each of the arrays contributes a proportionate amount of red, green, or blue.

The following function can be used to translate an indexed image into RGB format. Not only useful in seeing the relative contributions of red, green, and blue, but later we will see the need for converting indexed bitmaps to RGB in MATLAB when we present CData.

tmp8414465_thumb

 

 

tmp8414466_thumb

We can see the relative contribution of red, green, and blue using makergb along with,

tmp8414467_thumb

 

 

The red, green, and blue components of the clown image shown in relative intensity.

Figure 5.11 The red, green, and blue components of the clown image shown in relative intensity.

Next post:

Previous post: