user1578688
user1578688

Reputation: 427

Coloring an 8-bit grayscale image in MATLAB

I have an 8-bit grayscale image with different values (0,1,2,3,4,..., 255). What I want to do is color the grayscale image with colors like blue, red, etc. Until now, I have been doing this coloring but only in a greyscale. How can I do it with actual colors?

Here is the code I have written so far. This is where I am searching for all values that are white in an image and replacing them with a darkish gray:

for k = 1:length(tifFiles)
    baseFileName = tifFiles(k).name;
    fullFileName = fullfile(myFolder, baseFileName);
    fprintf(1, 'Now reading %s\n', fullFileName);
    imageArray = imread(fullFileName);

    %// Logic to replace white grayscale values with darkish gray here
    ind_plain = find(imageArray == 255);
    imageArray(ind_plain) = 50;

    imwrite(imageArray, fullFileName);
 end

Upvotes: 2

Views: 2515

Answers (1)

rayryeng
rayryeng

Reputation: 104464

What you are asking is to perform a pseudo colouring of an image. Doing this in MATLAB is actually quite easy. You can use the grayscale intensities as an index into a colour map, and each intensity would generate a unique colour. First, what you need to do is create a colour map that is 256 elements long, then use ind2rgb to create your colour image given the grayscale intensities / indices of your image.

There are many different colour maps that are available to you in MATLAB. Here are the current available colour maps in MATLAB without the recently added Parula colour map that was introduced in R2014:

How the colour maps work is that lower indices / grayscale values have colours that move toward the left side of the spectrum and higher indices / grayscale values have colours that move toward the right side of the spectrum.

If you want to create a colour map with 256 elements, you simply use any one of those colour maps as a function and specify 256 as the input parameter to generate a 256 element colour map for you. For example, if you wanted to use the HSV colour map, you would do this in MATLAB:

cmap = hsv(256);

Now, given your grayscale image in your MATLAB workspace is stored in imageArray, simply use ind2rgb this way:

colourArray = ind2rgb(double(imageArray)+1, cmap);

The first argument is the grayscale image you want to pseudocolour, and the second input is the colour map produced by any one of MATLAB's colour mapping functions. colourArray will contain your pseudo coloured image. Take note that we offset the grayscale image by 1 and also cast to double. The reason for this is because MATLAB is a 1-indexed programming language, so we have to start indexing into arrays / matrices starting at 1. Because your intensities range from [0,255], and we want to use this to index into the colour map, we must make this go from [1,256] to allow the indexing. In addition, you are most likely using uint8 images, and so adding 1 to a uint8 will simply saturate any values that are already at 255 to 255. We won't be able to go to 256. Therefore, you need to cast the image temporarily to double so that we can increase the precision of the image and then add 1 to allow the image to go to 256 if merited.

Here's an example using the cameraman.tif image that's part of the image processing toolbox. This is what it looks like:

enter image description here

So we can load in that image in MATLAB like so:

imageArray = imread('cameraman.tif');

Next, we can use the above image, generate a HSV colour map then pseudocolour the image:

cmap = hsv(256);
colourArray = ind2rgb(imageArray+1, cmap);

We get:

enter image description here


Take note that you don't have to use any of the colour maps that MATLAB provides. In fact, you can create your own colour map. All you have to do is create a 256 x 3 matrix where each column denotes the proportion of red (first column), green (second column) and blue (third column) values per intensity. Therefore, the first row gives you the colour that is mapped to intensity 0, the second row gives you the colour that is mapped to intensity 1 and so on. Also, you need to make sure that the intensities are floating-point and range from [0,1]. For example, these are the first 10 rows of the HSV colour map generated above:

>> cmap(1:10,:)

ans =

    1.0000         0         0
    1.0000    0.0234         0
    1.0000    0.0469         0
    1.0000    0.0703         0
    1.0000    0.0938         0
    1.0000    0.1172         0
    1.0000    0.1406         0
    1.0000    0.1641         0
    1.0000    0.1875         0
    1.0000    0.2109         0

You can then use this custom colour map into ind2rgb to pseudocolour your image.


Good luck and have fun!

Upvotes: 7

Related Questions