MATRIX REPRESENTATION OF COLOUR IMAGES

28 views (last 30 days)
Why are some colour imges represented by 2 dimensional matrix while other colour images are represented by 3 dimensional matrix ?
For example, the file autumn.tif (which comes along with MATLAB tool box) is represented by a 3 dimensional matrix while the file 'canoe.tif' is represented by a 2 dimensional matrix ?
How do I convert a colour image represented by a 3 dimensional matrix into a colour image represented by a 2 dimensional matrix

Accepted Answer

Walter Roberson
Walter Roberson on 6 Jun 2019
You are seeing the difference between RGB ("Truecolor") images, which are rows x columns x 3, compared to a Pseudocolor images, which are rows x columns plus a something-by-3 colormap:
[canimg, canmap] = imread('canoe.tif');
size(canmap)
ans =
256 3
In order to get a pseudocolor image displayed in color, you need to activate its colormap:
colormap(canmap);
This is done on your behalf if you ask to imshow('canoe.tif')
  2 Comments
Anand P
Anand P on 6 Jun 2019
Edited: Anand P on 6 Jun 2019
Thanks for the reply. I have another query.
How do i convert a true colour image (3D RGB image ) into a pseudocolour image (2D image)?
Thanks in advance
Walter Roberson
Walter Roberson on 8 Jun 2019
[pimg, cmap] = rgb2ind(rgbimg);
In this form, rgb2ind will choose the colormap entries. You can also create your own colormap and pass it in, such as
cmap = parula(15);
ping = rgb2ind(rgbimg, cmap);

Sign in to comment.

More Answers (2)

KSSV
KSSV on 6 Jun 2019
Read bout rgb2gray
  5 Comments
KSSV
KSSV on 6 Jun 2019
Yes...so 3D is converted to 1D using rgb2gray....REad about imagesc also.
Walter Roberson
Walter Roberson on 6 Jun 2019
canoe.tif is pseudcolor. rgb2gray cannot be used with it. If you did want to convert it to gray for some reason, then you would use one of:
[img, cmap] = imread('canoe.tif');
imgray = rgb2gray( ind2rgb(img, cmap) );
or
[img, cmap] = imread('canoe.tif');
imgray = ind2rgb(img, rgb2gray(cmap) );
That is, you can convert it to RGB first and then convert it to gray, or you can convert the colormap to gray and then apply the gray colormap to the image.

Sign in to comment.


Priysha Aggarwal
Priysha Aggarwal on 6 Jun 2019
Edited: Priysha Aggarwal on 6 Jun 2019
Images can be represented as both 2D and 3D matrices. When the image is grayscale (i.e. black and white) then we need only 2 channels to represent it. Each entry in the 2D matrix can take values from 0-255 representing grayscale values. Example : Following is a 4x4 grayscale image.
a = [0 10 200 255
50 95 65 210
65 84 152 56
87 196 56 184]
But coloured images need 3 channels representation - RGB. Hence we need a 3D matrix to represent it. Example : m*n*3 matrix represents a coloured image of size m*n (and 3 channels).
To convert 3D to 2D image:
You can either work with any one of the 3 channels as follows:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
Or you can convert it to grayscale image using rgb2gray function.
Hope this helps!
  1 Comment
Walter Roberson
Walter Roberson on 6 Jun 2019
Matrices with integer data class use value ranges between intmin() and intmax() of the class. For example uint16 images the values range from 0 to 65535. 0 to 255 is only for uint8 images.
Matrices with floating point data class use value ranges between 0 and 1 for RGB images. This applies for single and double both.
You missed out on the possibility of pseudocolor images, which is the situation for canoe.tif

Sign in to comment.

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!