Gray Scale to Pseudo color transformation


   Transformation of a gray scale image into pseudo color image helps in better visualization of the image. In this tutorial, different ways to apply pseudo color transformation to a gray scale image will be discussed along with the MATLAB Code.
The main idea behind pseudo color transformation is to perform three independent transformation (RED,GREEN and BLUE) on the grayscale or intensity image and map  the corresponding intensity value in the image to the result obtained.


Steps to be performed:





Functional Block Diagram

Reference: Digitial Image Processing by Gonzalez

MATLAB CODE:

%READ AN INPUT IMAGE
A = imread('cameraman.tif');
%PRE-ALLOCATE A MATRIX
Output = zeros([size(A,1) size(A,2) 3]);

%Define a colormap
map = colormap(jet(256));

%Assign the columns to 1-D RED,GREEN and BLUE
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);

%MAP THE COLORS BASED ON THE INTENSITY OF THE IMAGE

Output(:,:,1) = Red(A);
Output(:,:,2) = Green(A);
Output(:,:,3) = Blue(A);


Output = im2uint8(Output);
%DISPLAY THE IMAGE
imshow(Output);

%Save the image in PNG or JPEG format
imwrite(Output,'pseudo_color.jpg');






 Checkerboard with colormaps:



%READ AN INPUT IMAGE
A = imread('coins.png');
%RESIZE THE IMAGE
A = imresize(A,[256 256]);

%PRE-ALLOCATE THE OUTPUT MATRIX
Output = zeros([size(A,1) size(A,2) 3]);

%DEFINE SOME COLORMAPS
maps={'jet(256)';'hsv(256)';'cool(256)';'spring(256)';'summer(256)';'parula(256)';'hot(256)'};

%COLORMAP 1
map=colormap(char(maps(7)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);

R1 = Red(A);
G1 = Green(A);
B1 = Blue(A);

%COLORMAP 2
map=colormap(char(maps(6)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);
R2 = Red(A);
G2 = Green(A);
B2 = Blue(A);

%SIZE OF THE MATRIX
sz=64;

ind=0;

for i=1:sz:size(A,1)-(sz-1)
for j=1:sz:size(A,2)-(sz-1)

%APPLY COLORMAPS BASED ON THE SIZE SZ
if(mod(ind,2)==0)

            Output(i:i+(sz-1),j:j+(sz-1),1) = R1(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),2) = G1(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),3) = B1(i:i+(sz-1),j:j+(sz-1));




else
            Output(i:i+(sz-1),j:j+(sz-1),1) = R2(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),2) = G2(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),3) = B2(i:i+(sz-1),j:j+(sz-1));


end

        ind=ind+1;
end

    ind=ind+1;
end
Output = im2uint8(Output);

%FINAL IMAGE
imshow(Output);

EXPLANATION:
The above code is to combine two colormaps to obtain checkboard effect on the image. To obtain checkboards in different size change the 'sz' variable with 4,8,16..etc.




UPPER OR LOWER TRIANGLE :

MATLAB CODE:

%READ INPUT IMAGE
A = imread('coins.png');
%RESIZE IMAGE
A = imresize(A,[256 256]);
%PRE-ALLOCATE THE OUTPUT MATRIX
Output = ones([size(A,1) size(A,2) 3]);

%COLORMAPS
maps={'jet(256)';'hsv(256)';'cool(256)';'spring(256)';'summer(256)';'parula(256)';'hot(256)'};


%COLORMAP 1
map=colormap(char(maps(1)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);

R1 = Red(A);
G1 = Green(A);
B1 = Blue(A);


%COLORMAP 2
map=colormap(char(maps(7)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);




%RETRIEVE POSITION OF UPPER TRIANGLE
[x,y]=find(triu(Output)==1);
Output(:,:,1) = Red(A);
Output(:,:,2) = Green(A);
Output(:,:,3) = Blue(A);

for i=1:numel(x)

        Output(x(i),y(i),1)=R1(x(i),y(i));
        Output(x(i),y(i),2)=G1(x(i),y(i));
        Output(x(i),y(i),3)=B1(x(i),y(i));

end

Output = im2uint8(Output);

%FINAL IMAGE
imshow(Output);


EXPLANATION:
Upper triangular part of the image matrix with a colormap and lower triangular part of the image matrix with different colormap.


6 comments:

  1. Hey, i tried out the first part of your code. I get an error message "Array indices must be positive integers or logical values." "Output(:,:,1) = Red(A);"

    Do you know why it could be lik this?

    ReplyDelete
  2. Hey i tried out one part of your script, but it gives me an error:
    "Array indices must be positive integers or logical values.
    Error in GrayscaleToRGB (line 16)
    Output(:,:,1) = Red(A);"

    Do you know why it could be?

    ReplyDelete
  3. @Gena Cheburaschkin

    Its seems like dimension problem. Please check whether the input image is color or grayscale. If it is color then convert it into grayscale using rgb2gray()

    ReplyDelete
  4. @Aaron Angel


    Thank you Aaron,

    the original pic is grayscale.

    Maybe you can recognize the problem if i show you the information from workspace. I'm sorry, I'm not really good in matlab.

    Workspace:
    A: 512x640 uint16
    Blue: 256x1 double
    Green: 256x1 double
    map: 256x3 double
    Output 512x640x3 double
    Red: 256x1 double


    Can you see the problem now?

    ReplyDelete
  5. i have a code for pseudo color image processing but lecturer ask me to do in gui

    ReplyDelete