MATLAB

Color mapping

MATLAB array indexing starts with 1. Same is the case when working with indexed images in MATLAB. To visualize indexed images a data array is required and it should contain values ranging from 1 to number of colors in color map used. If the range of values is large or number of unique values in data array are more than the number of colors in color palette then scaling of data is required. So that proper mapping of colors to array elements can be done. For example, a 2d array has value ranging from 1 to 64 then color map should have at least 64 colors for one to one mapping. If 2d array values are floating point value then scaling of data is required. MATLAB functions image() and imagesc() do this job. image() function does one to one mapping of color and data, imagesc() also scales the data to display is properly as an image. But imagesc() function does not modify the data array. Try below scripts in MATLAB.

Values with small variation

% Array to be visualized, it can be 1d or 2d

zval1=[0 1 2 3];

zval2=[1 2 3 4];

zval3=[2 3 4 5];

%RGBY

cmap = [1 0 0;

0 1 0;

0 0 1;

1 1 0];

colormap(cmap);

subplot(2,3,1);

image(zval1);

subplot(2,3,2);

image(zval2);

subplot(2,3,3);

image(zval3);

subplot(2,3,4);

imagesc(zval1);

subplot(2,3,5);

imagesc(zval2);

subplot(2,3,6);

imagesc(zval3);

Output

mappingsimple

In the first row of the above image, red and yellow color displayed in the first and third plot are repeated. Because image function does't map value 0 and 5 to any other color. Colors were applied in the following order

0 - Red

1 - Red

2 - Green

3 - Blue

4 - Yellow

5 - Yellow

In second row, colors are same in all the three plots. Colors were applied after scaling of data.

Values with large variation

% Array to be visualized, it can be 1d or 2d

zval1 = [1 2 3 4];

Output

zval2 = [-70 2 3 -0.6];

zval3 = [65 3 4 15];

%RGBY

cmap = [1 0 0;

0 1 0;

0 0 1;

1 1 0];

colormap(cmap);

ColorMappingLarge

subplot(2,3,1);

image(zval1);

subplot(2,3,2);

image(zval2);

subplot(2,3,3);

image(zval3);

subplot(2,3,4);

imagesc(zval1);

subplot(2,3,5);

imagesc(zval2);

subplot(2,3,6);

imagesc(zval3);

In the first row of above image, data with value 1 and less (1, -70, -0.6) is colored red. Data with value 4 and above (4, 65, 15) is colored yellow. Function imagesc displays the data after scaling. The second row, second plot represent zval2 (-70, 2, 3, -0.6). -70 is very small as compared to other values so it is colored with first color of the color palette. Other values are near to each other and large compared to -70 so these are colored with the highest color index. Similar mapping is done for zval3.

Saving image file

The above description is applicable when working inside MATLAB IDE. When an indexed image is saved as a file. Then scaling of data array is required. Otherwise the image file will not show correct colors.

x = linspace(-2*pi,2*pi,200);

Image in MATLAB

y = x;

[X,Y] = meshgrid(x,y);

Z = cos(X.^2+Y.^2);

nColor = 256; %number of colors

cmap = gray(nColor);

colormap(cmap);

imagesc(Z);

imwrite(Z,cmap,'unscaled.png');

%Do scaling of data then save image

% scale Z from 0 to 1

Z2 = (Z - min(Z(:)));

Z2 = Z2./max(Z2(:));

ScaledImageInMatlab

%Scale Z2 from 0 to 255

Z2 = uint8((nColor-1)*Z2);

%Save scaled image

imwrite(Z2,cmap,'scaled.png');

unscaled.png

UnscaledImageSaved

scaled.png

ScaledImageSaved

Switch case and function handle

To evaluate one of many mathematical function in a loop switch case can be used in the loop. This can be done using function handles as shown in the second script. Value of z is same in both the scripts. MATLAB function handles are just like function pointers in C.

Switch case in loop

x = 2;

y = 3;

z = 0;

choice = 2;

for n = 1:100

switch choice

case 1

z = z + x + y;

case 2

z = z + x + y^2 + y;

case 3

z = z + x^2 + y;

end

end

The script shows the use of anonymous functions to keep switch case out of loop.

x = 2;

y = 3;

z = 0;

choice=2;

switch choice

case 1

func = @(x,y)(x + y);

case 2

func = @(x,y)(x + y^2 + y);

case 3

func = @(x,y)(x^2 + y);

end

for n = 1:100

z = z + func(x,y);

end