Use same colorbar on new figure

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?

Respuestas (1)

Image Analyst
Image Analyst el 27 de Nov. de 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 comentarios

Deniz Terzioglu
Deniz Terzioglu el 27 de Nov. de 2023
This still creates a new colorbar with a colormap based on the new image (black and white), I want to use the same colorbar and same colormap on the new image.
Image Analyst
Image Analyst el 27 de Nov. de 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
Deniz Terzioglu el 27 de Nov. de 2023
No, converted_image is RGB, background_layer is greyscale. If I add a colorbar after the second chunk of code above, the colorbar on the second figure will be greyscale. On the second figure, instead of creating a new colorbar I want to use the same colorbar shown on the first figure.
Adam Danz
Adam Danz el 27 de Nov. de 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
cb1 =
ColorBar with properties: Location: 'eastoutside' Limits: [0 255] FontSize: 8.1000 Position: [0.6191 0.5843 0.0256 0.3418] Units: 'normalized' Use GET to show all properties
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')
DGM
DGM el 28 de Nov. de 2023
Editada: DGM el 28 de Nov. de 2023
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.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2021b

Preguntada:

el 27 de Nov. de 2023

Editada:

DGM
el 28 de Nov. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by