MATRIX REPRESENTATION OF COLOUR IMAGES

32 vues (au cours des 30 derniers jours)
Anand P
Anand P le 6 Juin 2019
Commenté : Walter Roberson le 8 Juin 2019
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

Réponse acceptée

Walter Roberson
Walter Roberson le 6 Juin 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 commentaires
Anand P
Anand P le 6 Juin 2019
Modifié(e) : Anand P le 6 Juin 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 le 8 Juin 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);

Connectez-vous pour commenter.

Plus de réponses (2)

KSSV
KSSV le 6 Juin 2019
Read bout rgb2gray
  5 commentaires
KSSV
KSSV le 6 Juin 2019
Yes...so 3D is converted to 1D using rgb2gray....REad about imagesc also.
Walter Roberson
Walter Roberson le 6 Juin 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.

Connectez-vous pour commenter.


Priysha Aggarwal
Priysha Aggarwal le 6 Juin 2019
Modifié(e) : Priysha Aggarwal le 6 Juin 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 commentaire
Walter Roberson
Walter Roberson le 6 Juin 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by