MATLAB: How can I show different values of the image in different colors

colormapdigital image processingimage processingImage Processing ToolboxMATLAB

I have a gray scale image with values that differ between 0 and 255. I want to show value 0 as black , value 255 as white, value between 1 to 100 as red, and so on. how can I do that , without changing the image values?

Best Answer

Here it is how to remap a grey scale image into color image by selecting gray values intervals:
%original grayscale image
A=rgb2gray(imread('peppers.png'));
figure; imshow(A);
% 0->black 1:100->red 101-254->green 255->white
cmap = [0,0,0;1,0,0;0,1,0;1,1,1];
%remap original image to indeces 1,2,3,4
B=zeros(size(A));
B(A==0)=1;
B(1<=A & A<=100)=2;
B(101<=A & A<=254)=3;
B(A==255)=4;
%define color image by remapping
RGB = ind2rgb(B,cmap);
%remapped image
figure; imshow(RGB);
Related Question