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

 채택된 답변

Matt Fig
Matt Fig 2012년 8월 6일

6 개 추천

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

Philipp
Philipp 2012년 8월 8일
편집: Philipp 2012년 8월 8일
Hi Matt, thanks for you helpful answer, it really addresses the problem I was struggling with. I found it also to be useful to create the lookup table directly from jet(1:1000) without the need of an interpolation.
br philipp
Seth
Seth 2013년 5월 1일
This is very helpful Matt - solved my problem as well - very efficiently I might add.
The only thing I would add is that if you don't want to display a figure during this process (just work with data), then you need to modify the following lines:
Change: C = colormap; % Get the figure's colormap.
To: C = jet(64); %State the name of the colormap and the number of levels
For whatever reason, the function "colormap" creates and displays an empty window where as directly defining the colormap by name and number of levels suppresses the creation of a new figure.
Thanks again for the great piece of code.
Bruno
Bruno 2014년 1월 31일
Such beatiful peace of code. I'm very thankful!
Mathieu
Mathieu 2015년 8월 12일
Thank you so Much Matt, very helpful!!
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')

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jurgen
Jurgen 2013년 5월 1일

1 개 추천

The FEX has mat2im and real2rgb. Can recommend both!

댓글 수: 1

Jurgen
Jurgen 2013년 5월 1일
Just noticed this thread was old...

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2012년 8월 6일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 White에 대해 자세히 알아보기

태그

질문:

2012년 8월 6일

댓글:

DGM
2023년 1월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by