How to display the intensity of 2D image as a colormap?
조회 수: 26 (최근 30일)
이전 댓글 표시
I have a 2D image. and i want to plot a colormap that represents the intensity of each pixel as a color like the following picture:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/290260/image.jpeg)
I tried to use colormap(image) but it makes error " Error using colormap (line 98)
Colormap must have 3 columns: [R,G,B]."
Does anyone know how to do this in matlab?
댓글 수: 2
채택된 답변
Image Analyst
2020년 5월 4일
편집: Image Analyst
2020년 5월 4일
You need to pass in a colormap, N-by-3 array with values between 0 and 1, to to the colormap() function, NOT an image. And you should probably also pass in a handles to the axes you want to apply it to, otherwise it will apply that colormap to every axes in the figure window, which is probably not what you want. So in short, you want:
myColorMap = hsv(256); % Create a colormap.
colormap(gca, myColorMap); % Now apply the colormap.
You can also pass the colormap directly into imshow() and skip calling the colormap() function if you want.
See this full demo and study it:
grayImage = imread('cameraman.tif');
subplot(3, 1, 1);
imshow(grayImage);
axis('on', 'image');
title('Original Image');
subplot(3, 1, 2);
% Create a colormap.
myColorMap = hsv(256);
% Display image with that colormap.
imshow(grayImage, 'Colormap', myColorMap);
colorbar;
axis('on', 'image');
title('Overlay Image');
% Or equivalently, using the colormap() function:
subplot(3, 1, 3);
% Display image with no colormap.
imshow(grayImage);
% Create a colormap.
myColorMap = hsv(256);
% Now apply the colormap. Pass in the axes or else it will apply the colormap
% to ALL images on the figure, which is probably not what you want.
colormap(gca, myColorMap);
colorbar;
axis('on', 'image');
title('Overlay Image');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/290344/image.png)
Now, if you already have a colormap, and you want to produce an RGB image, like to write to a file on disk rather than just pseudocoloring a displayed image, you want to call ind2rgb()
rgbImage = ind2rgb(grayImage, myColorMap);
댓글 수: 5
Image Analyst
2020년 5월 5일
If your image values go from 0 to 1, it should already do that. Attach your code and image if you still need help.
추가 답변 (1개)
Ameer Hamza
2020년 5월 4일
편집: Ameer Hamza
2020년 5월 4일
Try this
im = im2double(imread('peacock.jpg'));
im_gray = rgb2gray(im);
pcolor(flipud(im_gray))
shading interp
colormap(jet)
colorbar
You can choose a different colormap from a list here: https://www.mathworks.com/help/releases/R2020a/matlab/ref/colormap.html#buc3wsn-1-map or create your own colormap as an mx3 matrix where each row is an RGB color.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/290342/image.png)
참고 항목
카테고리
Help Center 및 File Exchange에서 Blue에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!