Color

Representation of color in MATLAB

It is represented through a MxNx3 matrix, where M is the width of the image, N is the height and there are three matrices, one for each color component in the space in which it is represented. Usually, this color space is RGB (Red, Green, Blue).

For each pixel of the image, instead of having a numeric value representing the level of grey, we have a vector that represents the color. Each component of the vector represents the intensity of the color in the corresponding channel.

RGB, Red, Green and Blue colors

In MATLAB, you can not only use RGB, but many other color spaces like CMY, CMYK, HSV, HSI, NTSC Color Space, etc. Here we'll work exclusively with RGB and CMY.

Color maps

Another way of representing an image in MATLAB is through a color map, where we have an image represented by a matrix X (MxN) and a matrix C of dimensions Kx3, that represents the color space, where K is the number of colors in that space.

Each component of matrix X is an integer between 1 and K. To know the color of that pixel, we just select the corresponding row in C, the three values of that row will represent the color of the corresponding pixel.

Representation conversion

To change the representation of an image from RGB to Colormap, we can use the following command:

[X, map] = rgb2ind(f, num_of_colors, option)

where f is the original image in the format MxNx3 and RGB color space, num_of_colors is the maximum number of colors that the map is going to have, option can be 'dither' and 'nodither'. The first value will choose the colors for which we are going to reduce the RGB color space in an intelligent way, as a function of the colors that exist in the image. The second value will scale the values in a proportional way. As a resutl it returns X and map. The first one is the image with the indexes and the second is the color map.

To represent the result, we use the following command:

figure, imshow(X, map)

Intensity transformations

To perform an intensity transformation in all the color components or in independent components (red, blue, green, cyan, magenta...) we'll use the following MATLAB command:

g = ice('image', f)

where f is the original image and g is the resulting image after a transformation of its intensity values.

The command shows the following window through the UI:

Color edition window

Color filtering

The filtering of color images is done in the same way than with images in gray scale, with imfilter(), but instead of passing it a gray scale image, we pass it a color image.

Each of the color images are filtered separately and then they are joined back again in a single image with three color components.

Colored borders detection

The border detection is not transformable any more as the filtering, we can not detect the borders in each color component and then join the images because it wouldn't work.

We are now faced with the following problem (where r, g and u are unit vectors):

u=Rxr+Gxg+Bxb

v=Ryr+Gyg+Byb

gxx=u·u=uT·u=Rx2+Gx2+Bx2

gyy=v·v=vT·v=Ry2+Gy2+By2

gxy=u·v=uT·v=RxRy+GxGy+BxBy

θ(x,y)=12tan-12gxygxx-gyy

Fθ(x,y)=12gxx+gyy+gxx-gyycos2θ+2gxysin2θ12

F represents the gradient in the θ direction, where the gradient has a maximum.

To calculate the value of F we use the following command:

[VG, A, PPG] = colorgrad(f)

where f is the original image, VG the borders image, A is the image that represents the value of the angle, and PPG is an image with the sum of the gradients in gray levels of each one of the color components of the image f.

You can practice these concepts using snippet10.