MATRIX REPRESENTATION OF COLOUR IMAGES

39 visualizaciones (últimos 30 días)
Anand P
Anand P el 6 de Jun. de 2019
Comentada: Walter Roberson el 8 de Jun. de 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

Respuesta aceptada

Walter Roberson
Walter Roberson el 6 de Jun. de 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 comentarios
Anand P
Anand P el 6 de Jun. de 2019
Editada: Anand P el 6 de Jun. de 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 el 8 de Jun. de 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);

Iniciar sesión para comentar.

Más respuestas (2)

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

Iniciar sesión para comentar.


Priysha Aggarwal
Priysha Aggarwal el 6 de Jun. de 2019
Editada: Priysha Aggarwal el 6 de Jun. de 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 comentario
Walter Roberson
Walter Roberson el 6 de Jun. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Images en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by