× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Coloring an 8-bit grayscale image in MATLAB

Ray Phan
Apr 29, 2015
<p>What you are asking is to perform a <a href="http://en.wikipedia.org/wiki/False_color#Pseudocolor" rel="nofollow">pseudo colouring</a> 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 <a href="http://www.mathworks.com/help/matlab/ref/ind2rgb.html" rel="nofollow"><code>ind2rgb</code></a> to create your colour image given the grayscale intensities / indices of your image. </p> <p>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:</p> <p><img src="http://i.stack.imgur.com/dP9eY.gif" alt=""></p> <p>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.</p> <p>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:</p> <pre><code>cmap = hsv(256); </code></pre> <p>Now, given your grayscale image in your MATLAB workspace is stored in <code>imageArray</code>, simply use <code>ind2rgb</code> this way:</p> <pre><code>colourArray = ind2rgb(double(imageArray)+1, cmap); </code></pre> <p>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. <code>colourArray</code> will contain your pseudo coloured image. Take note that we <strong>offset</strong> the grayscale image by 1 and also <strong>cast to <code>double</code></strong>. 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 <code>[0,255]</code>, and we want to use this to index into the colour map, we must make this go from <code>[1,256]</code> to allow the indexing. In addition, you are most likely using <code>uint8</code> images, and so adding 1 to a <code>uint8</code> 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 <code>double</code> so that we can increase the precision of the image and then add 1 to allow the image to go to 256 if merited.</p> <p>Here's an example using the <code>cameraman.tif</code> image that's part of the image processing toolbox. This is what it looks like:</p> <p><img src="http://i.stack.imgur.com/ZJXXt.png" alt="enter image description here"></p> <p>So we can load in that image in MATLAB like so:</p> <pre><code>imageArray = imread('cameraman.tif'); </code></pre> <p>Next, we can use the above image, generate a HSV colour map then pseudocolour the image:</p> <pre><code>cmap = hsv(256); colourArray = ind2rgb(imageArray+1, cmap); </code></pre> <p>We get:</p> <p><img src="http://i.stack.imgur.com/BgSUC.png" alt="enter image description here"></p> <hr> <p>Take note that you don't <strong>have</strong> 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 <strong>floating-point</strong> and range from <code>[0,1]</code>. For example, these are the first 10 rows of the HSV colour map generated above:</p> <pre><code>&gt;&gt; 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 </code></pre> <p>You can then use this custom colour map into <code>ind2rgb</code> to pseudocolour your image.</p> <hr> <p>Good luck and have fun!</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/29816835/Coloring%20an%208-bit%20grayscale%20image%20in%20MATLAB/29817886">Stack Overflow</a>.</p>
comments powered by Disqus