map pixel intensity to rainbow

9 views (last 30 days)
Justin
Justin on 2 Dec 2011
I am working with a unit16 only in the red channel (ImgRed)
In it's simplest form, I need create a new image (Irainbow) where the highest intensity pixel from ImgRed is represented by blue in Irainbow and the lowest intensity pixel in ImgRed is represented by red in Irainbow.
Any help?
I tried converting to double, indexing, and using colormap(jet). But it looks really bad.

Accepted Answer

Image Analyst
Image Analyst on 5 Dec 2011
Justin: Please run this demo and I think you'll be able to get what you want, if you try to understand it. It creates a sample uint16 image, then applies the jet colorbar between the max and min of that image, and relabels the tick marks on the colorbar to represent the actual numbers in the image. Note the image must be converted from uint16 to uint8 (or single or double) if you want to use a colormap.
% Demo to scale the jet colormap to the range of a uint16 image.
% By ImageAnalyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
% Generate sample image.
grayImage = peaks(600);
% Scale arbitrarily to, oh say, 0-23456 and make it uint16.
minValue = min(grayImage(:));
maxValue = max(grayImage(:));
range = maxValue - minValue;
grayImage = uint16(23456 * (grayImage - minValue) / (maxValue - minValue));
% All the above was simply to get some sample uint16 data.
% Update the maxvalue.
maxValue = max(grayImage(:));
% Don't let it be more than 256 or it doesn't work.
if maxValue > 256
maxValue = 256;
end
% Now create a custom jet colormap that goes from 0 to maxValue.
customJet = jet(double(maxValue)); % Jet take a double arg, not uint16.
% Even though a gray scale image can be uint16 if you want to
% apply a colormap, you need to consider this from the help:
% "An indexed image can be logical, uint8, single, or double."
% So we must cast your uint16 to double in the range of 0-1.
% Update these values
minValue = double(min(grayImage(:)));
maxValue = double(max(grayImage(:)));
range = maxValue - minValue;
% Now scale the uint16 to 0-1.
dblGrayImage = uint8(255 * (single(grayImage) - minValue) / (maxValue - minValue));
imshow(dblGrayImage, []);
title('Uint16 Peaks image with custom Jet Colorbar', 'FontSize', 20);
hcb = colorbar;
colormap(customJet);
colorbar;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Now fix up the tick mark labels.
% Get a cell array with new color bar labels.
numberOfTicks = 11; % We'll specify 11 labels on the colorbar.
% Compute the increment between each label number.
inc = round(maxValue/numberOfTicks);
caTickLabels = cell(1, numberOfTicks + 1); % Preallocate
yTickNumbers = zeros(1, numberOfTicks + 1); % Preallocate
for tickMark = 0 : numberOfTicks
% Compute the number.
theNumber = tickMark * inc;
% Convert it to a string.
caTickLabels{tickMark+1} = sprintf('%d', theNumber);
% Stuff it in the cell array.
yTickNumbers(tickMark+1) = 255 * tickMark / numberOfTicks;
end
% Apply the new labels.
hcb = colorbar('YTick', yTickNumbers, 'YTickLabel', caTickLabels);
set(hcb,'YTickMode','manual');

More Answers (2)

Image Analyst
Image Analyst on 2 Dec 2011
Don't convert to double. Leave it as integer. You just need to get your colormap correct. Apparently you don't like jet, so go ahead and design your own if you don't like any of the built-in colormaps such as jet, winter, autumn, hsv, etc.
  1 Comment
Justin
Justin on 2 Dec 2011
I would love to use jet if I could get it to work.
I viewed the tutorial:
load flujet; image(X) colormap(jet)
and it looked perfect. Exactly what I want. But I can't get it to work on my image. I noticed X was a double in the tutorial which is why I tried to change my image to double.
If I index my image and then use jet it just looks bad.
If I could get colormap(jet) to work on my unit16 like it does on X in the tutorial, then I would be very happy.

Sign in to comment.


Walter Roberson
Walter Roberson on 2 Dec 2011
The default jet colormap has 256 colors.
In theory you could construct the 65536 color equivalent by using
jet16 = jet(65536);
after which you could
colormap(jet16)
This is not something I would recommend directly.
I would suggest that you figure out how many bits of resolution you need to represent your results nicely enough. It could even be that imagesc(imgRed) with the normal jet colormap would be enough for your purposes. If not then colormap(jet(512)) and upwards by powers of 2 until you find one that looks okay.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!