Simple RGB Images

RGB images in Matlab are stored as three-dimensional arrays, rgb[h][v][3]. They can be constructed by concatenating three two-dimensional arrays:

rgb = cat(3,r,g,b);
where r, g, and b are dimensioned h-by-v. Note that the range of color levels in each plane (or channel) is from 0 to 1.

Linear ramp

Create a 100 x 100 linear ramp as follows:

x = ones(100,1)*linspace(0,1);
imshow(x);
imwrite(x,'grays.jpg');

The default for linspace is a 100 element array.

Color ramp

Vary red horizontally and blue vertically:

rgb = cat(3,x,zeros(100,100),x');
imwrite(rgb,'ramp.jpg');

Suppose we wanted the black corner to be at the lower left instead of the upper left:

rgb = cat(3,x,zeros(100,100),flipud(x'));
imwrite(rgb,'ramp_flipped.jpg');

We could also have flipped the previous rgb matrix.

hsv to rgb conversion

Create a color ramp in hsv space, setting s = 1. Convert the result to rgb:

hsv = cat(3,x,ones(100),x');
rgb = hsv2rgb(hsv);
imwrite(rgb,'rgb1.jpg');

Create a color ramp in hsv space, setting v = 1. Convert the result to rgb:

hsv = cat(3,x,x',ones(100));
rgb = hsv2rgb(hsv);
imwrite(rgb,'rgb2.jpg');


Maintained by John Loomis, last updated 28 Dec 1999