convert matrix to rgb

91 visualizaciones (últimos 30 días)
Philipp
Philipp el 6 de Ag. de 2012
Comentada: DGM el 15 de En. de 2023
dear matlab developers,
i have a double matrix (MxN). is there a way to convert it to RGB?
when i use imagesc(matrix), all the values are color coded and displayed, but is there a way to write the RGB values into a new array (MxNx3)?
br philipp

Respuesta aceptada

Matt Fig
Matt Fig el 6 de Ag. de 2012
I wonder if this is what you mean, Philipp?
% Say this is the given matrix:
G = rand(10,10)*300 - 100;
% Use IMAGESC to plot G.
figure('pos',[100 100 1000 800]);
% colormap(copper) % You realize this affects final image (H)?
subplot(1,2,1)
imagesc(G);
title('IMAGESC (MxN)')
% Now make an RGB image that matches display from IMAGESC:
C = colormap; % Get the figure's colormap.
L = size(C,1);
% Scale the matrix to the range of the map.
Gs = round(interp1(linspace(min(G(:)),max(G(:)),L),1:L,G));
H = reshape(C(Gs,:),[size(Gs) 3]); % Make RGB image from scaled.
subplot(1,2,2)
image(H) % Does this image match the other one?
title('IMAGE (MxNx3)')
  5 comentarios
Mathieu
Mathieu el 12 de Ag. de 2015
Thank you so Much Matt, very helpful!!
DGM
DGM el 15 de En. de 2023
You could also use ind2rgb() with the desired colortable.
% create a 2D array that's not in standard image data scale
myarray = repmat(linspace(-1,1,200),[100,1]);
% create whatever color table
CT = jet(256);
% rescale to index range (multiple ways to do this)
%scaledpict = round(rescale(double(myarray),1,size(CT,1))); % R2017b+
%scaledpict = gray2ind(mat2gray(myarray),size(CT,1)); % IPT
scaledpict = round(1 + mat2gray(myarray)*(size(CT,1)-1));
% convert to RGB
outpict = ind2rgb(scaledpict,CT);
% display it
imshow(outpict)
% or save it
imwrite(outpict,'mypicture.png')

Iniciar sesión para comentar.

Más respuestas (3)

Jurgen
Jurgen el 1 de Mayo de 2013
The FEX has mat2im and real2rgb. Can recommend both!
  1 comentario
Jurgen
Jurgen el 1 de Mayo de 2013
Just noticed this thread was old...

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 6 de Ag. de 2012
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
Cast to uint8 if you want to display or save to a file. Scale to 0-255 if it's not already in that range. mat2gray() might help.

Walter Roberson
Walter Roberson el 6 de Ag. de 2012

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by