# 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. 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 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):

$\stackrel{\to }{u}=\frac{\partial R}{\partial x}\stackrel{\to }{r}+\frac{\partial G}{\partial x}\stackrel{\to }{g}+\frac{\partial B}{\partial x}\stackrel{\to }{b}$

$\stackrel{\to }{v}=\frac{\partial R}{\partial y}\stackrel{\to }{r}+\frac{\partial G}{\partial y}\stackrel{\to }{g}+\frac{\partial B}{\partial y}\stackrel{\to }{b}$

${g}_{xx}=\stackrel{\to }{u}·\stackrel{\to }{u}={\stackrel{\to }{u}}^{T}·\stackrel{\to }{u}={\left|\frac{\partial R}{\partial x}\right|}^{2}+{\left|\frac{\partial G}{\partial x}\right|}^{2}+{\left|\frac{\partial B}{\partial x}\right|}^{2}$

${g}_{yy}=\stackrel{\to }{v}·\stackrel{\to }{v}={\stackrel{\to }{v}}^{T}·\stackrel{\to }{v}={\left|\frac{\partial R}{\partial y}\right|}^{2}+{\left|\frac{\partial G}{\partial y}\right|}^{2}+{\left|\frac{\partial B}{\partial y}\right|}^{2}$

${g}_{xy}=\stackrel{\to }{u}·\stackrel{\to }{v}={\stackrel{\to }{u}}^{T}·\stackrel{\to }{v}=\left|\frac{\partial R}{\partial x}\right|\left|\frac{\partial R}{\partial y}\right|+\left|\frac{\partial G}{\partial x}\right|\left|\frac{\partial G}{\partial y}\right|+\left|\frac{\partial B}{\partial x}\right|\left|\frac{\partial B}{\partial y}\right|$

$\theta \left(x,y\right)=\frac{1}{2}{\mathrm{tan}}^{-1}\left[\frac{2{g}_{xy}}{\left({g}_{xx}-{g}_{yy}\right)}\right]$

${F}_{\theta }\left(x,y\right)={\left\{\frac{1}{2}\left[\left({g}_{xx}+{g}_{yy}\right)+\left({g}_{xx}-{g}_{yy}\right)\mathrm{cos}2\theta +2{g}_{xy}\mathrm{sin}2\theta \right]\right\}}^{\frac{1}{2}}$

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.