I need to add a colorbar to an indexed image showing by imshow()
27 views (last 30 days)
Show older comments
I need to add a colorbar to an indexed image previously defined. After using imshow(), I cannot add a colorbar to the image with the specified limits ( from 0 to 5.44 ). The unexpected result is shown below the code. Can I set the colorbar without manually changing like this? Or are there some better solutions?
%RGB is the int matrix of the indexed matrix, where the maximum is 596.
map1=jet(596);
map1(1,:)=[1,1,1]; %To set the background color to white.
imshow(RGB,map1);
h=colorbar
h.Limits=[0,5.44];
2 Comments
Walter Roberson
on 15 Jul 2021
The only part of that which concerns me is the white going to 2 not just to 1. The appearance of unbroken blue is not a problem: when you stretch jet to 596 entries then the first several are 0 red, 0 green, and blue ranging from 132 to 137, which is not enough of a change to be visible.
Walter Roberson
on 15 Jul 2021
map1=jet(596);
map1(1,:)=[1,1,1]; %To set the background color to white.
map1_8 = im2uint8(map1);
map1_8(1:6,:)
rgb8 = reshape(map1_8, 1, [], 3);
gray8 = rgb2gray(rgb8);
gray8(1:6)
The human eye is least sensitive to changes in intensity of blue. You can see from the gray8 table that the entire span from entry 2 to entry 6 is only brightness 15 to brightness 16 -- not enough to reliably tell the difference between.
So, as I indicated, the double white is the only thing that concerns me about what was posted.
Accepted Answer
DGM
on 15 Jul 2021
Edited: DGM
on 15 Jul 2021
It seems to me that what you're trying to do is just scale the tick labels and not actually scale the mapping (which is what you're already doing). This is a simple example that should be easy enough to adapt. There are other ways to do the same.
indpict = imread('cameraman.tif');
map1 = jet(256);
cbscale = [0 5.44];
imshow(diff(cbscale)*double(indpict)/256 + min(cbscale),[]);
colormap(map1);
colorbar;
I don't know if you're going to get your custom colormap to line up easily. You could also just replace the tick labels directly, but bear in mind that (just like in this example) the first and last tick labels don't always correspond to the actual scale extents.
indpict = imread('cameraman.tif');
map1 = jet(256);
cbscalein = [0 size(map1,1)];
cbscaleout = [0 5.44];
imshow(indpict,map1);
h = colorbar;
h.TickLabels = diff(cbscaleout)*(h.Ticks-cbscalein(1))/diff(cbscalein) + cbscaleout(1);
Of course, tick positions are just being reused in this example, so they don't correspond to nice neat numbers. That could also be adjusted.
indpict = imread('cameraman.tif');
map1 = jet(256);
cbscalein = [0 size(map1,1)];
cbscaleout = [0 5.44];
% maybe explicitly set the tick positions
ticks = [0:0.5:5 5.44];
imshow(indpict,map1);
h = colorbar;
h.Ticks = diff(cbscalein)*(ticks-cbscaleout(1))/diff(cbscaleout) + cbscalein(1);
h.TickLabels = diff(cbscaleout)*(h.Ticks-cbscalein(1))/diff(cbscalein) + cbscaleout(1);
More Answers (1)
Image Analyst
on 15 Jul 2021
RGB images do not take colormaps - it simply does not apply. You can use it for indexed images though. See this:
% indexedImage is a uint16 indexed matrix, where the maximum is 596.
indexedImage = uint16(round(596 * rand(40, 60)));
map1 = jet(596);
map1(1,:) = [1,1,1]; % To set the background color to white.
imshow(indexedImage, 'Colormap', map1, 'InitialMagnification', 800);
h = colorbar
caxis([0, 596]);
% Now change range to 0 - 5.96 (1/100th as much so it will be mostly red
% since most pixel values are greater than 5.96 and will appear with the
% color at the top of the colorbar, which is red).
promptMessage = sprintf('Do you want to change the limits of the colorbar?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
return; % or break or continue.
end
caxis([0, 5.96]);
Note how the appearance changes once you change the limits of the range that the colorbar applies to with caxis().
See Also
Categories
Find more on Blue in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!