Use same colorbar on new figure
Show older comments
I have a list of values which I am displaying with scaled colours and on this figure I have placed a colorbar:
clims = [min(matrix(:, 1)) max(matrix(:, 1))];
norm = imagesc(matrix(:, 2)', clims);
colormap hot
cb = colorbar;
This is then converted into an RGB double which I have named converted_image. I have another image (background_layer), and I have masked a part of it. After scaling converted_image to background_layer, I overlay converted_image over the mask, creating a new figure:
figure;
imshow(background_layer)
hold on
colourimage = imshow(converted_image);
alpha = background_layer .* mask;
set(colourimage, 'AlphaData', alpha);
I want to be able to display the same colorbar on the new figure. How could I do this?
Answers (1)
Image Analyst
on 27 Nov 2023
After you've called figure and imshow to create your second image, call colorbar again
cmap = hot(256);
colormap(cmap);
colorbar;
Of course the color bar doesn't apply to an RGB image, but it should at least show up on the figure. If the ends of the color bar don't have the values you want then call clim or caxis (pre-2022).
6 Comments
Deniz Terzioglu
on 27 Nov 2023
Image Analyst
on 27 Nov 2023
You said "This is then converted into an RGB double which I have named converted_image." so are you saying that is wrong? Are you saying that converted_image is actually grayscale?
Deniz Terzioglu
on 27 Nov 2023
Adam Danz
on 27 Nov 2023
A screenshot would be nice.
Is this what you mean:
grayImage = imread('cameraman.tif');
subplot(2, 1, 1);
imshow(grayImage);
cmap = hot(256);
colormap(cmap);
cb1 = colorbar
colorBarTicksLabels = cb1.TickLabels;
title('Indexed Image with Hot Colormap')
% Convert image to rgb
rgbImage = ind2rgb(grayImage, cmap);
% Display RGB image
subplot(2, 1, 2);
imshow(rgbImage);
cb2 = colorbar;
cb2.TickLabels = colorBarTicksLabels;
title('RGB Image displaying Hot Colormap')
Whenever you call imshow(), it will reassert its default colormap preference (gray) on the parent axes. You'll have to specify the desired colormap after the call to imshow() that draws the colormapped background image.
Consider a simplified version.
% a simple composition with a binary mask
converted_image = im2double(imread('peppers.png')); % RGB, double
background_layer = im2gray(converted_image); % I, double
alpha = ~imread('pepcircmask.png'); % I, logical
clims = [0.1 0.9]; % or whatever is appropriate
imshow(background_layer,clims) % this is colormapped
hold on
colourimage = imshow(converted_image); % this is not colormapped
set(colourimage, 'AlphaData', alpha);
colorbar
colormap('hot')
I don't know what your images look like or the purpose of the composition, but bear in mind that this graduated blending will probably make the blended areas unreadable. That is, the colors in the image no longer correspond to the colors in the colorbar, so the colorbar is no longer useful for figuring out what the image values are.
% the same thing, but with the graduated blending as suggested
converted_image = im2double(imread('peppers.png')); % RGB, double
background_layer = im2gray(converted_image); % I, double
mask = ~imread('pepcircmask.png'); % I, logical
clims = [0.1 0.9]; % or whatever is appropriate
imshow(background_layer,clims) % this is colormapped
hold on
colourimage = imshow(converted_image); % this is not colormapped
alpha = background_layer .* mask; % this will likely make your composite image unreadable
set(colourimage, 'AlphaData', alpha);
colorbar
colormap('hot')
In either case, the limits of the colorbar will be determined either in the relevant call to imshow(), or can be specified afterwards using caxis()/clim() as IA already mentioned.
If you're instead trying to create a fake colorbar to represent the contents of a pseudocolor RGB image instead of the colormapped background image, that's a different story.
Again, seeing screenshots or image examples would probably help.
Categories
Find more on White in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

