Color map and color bar of the overlaid index image on a gray scale background image?

2 views (last 30 days)
Hi smart guys,
I use following example codes to overlay an index image on a background image (in the example below it is RGB but for my problem the background is a gray sacle image). Then my questions are:
1. How to display the colorbar on the side of the overlaid image? The colorbar should be the colorbar of the overlaid index image NOT the colorbar of the background image.
2. How to fix the color map range of the overlaid index image? I have several pairs of gray scale background + overlaid index image (using 'jet' color map). I need them to be shown in the same scale. I try to use set(iim2,'caxis', [0 1]); but the 'image' in Matlab hasn't got the property of 'caxis'.
Please help and thanks very much!
% Create the background
% This example uses a blend of colors from left to right, converted to a TrueColor image
% Use repmat to replicate the pattern in the matrix
% Use the "jet" colormap to specify the color space
bg = ind2rgb(repmat(1:64,64,1),jet(64));
% Create an image and its corresponding transparency data
% This example uses a random set of pixels to create a TrueColor image
im = rand(100,100,3);
% Make the image fade in from left to right by designing its alphadata
% Use repmat to replicate the pattern in the transparency fading
imAlphaData = repmat(0:1/size(im,2):1-1/size(im,2),size(im,1),1);
% Display the images created in subplots
hf = figure('units','normalized','position',[.2 .2 .6 .6]);
ax1 = subplot(2,3,1);
ibg = image(bg);
axis off
title('Background')
ax2 = subplot(2,3,4);
iim = image(im);
axis off
title('Image without transparency yet')
% Now set up axes that overlay the background with the image
% Notice how the image is resized from specifying the spatial
% coordinates to locate it in the axes.
ax3 = subplot(2,3,[2:3, 5:6]);
ibg2 = image(bg);
axis off
hold on
% Overlay the image, and set the transparency previously calculated
iim2 = image(im,'XData',[30 50],'YData',[10 30]);
set(iim2,'AlphaData',imAlphaData);
title(sprintf('Using transparency while overlaying images:\nresult is multiple image objects.'))

Answers (1)

Sean de Wolski
Sean de Wolski on 10 Dec 2012
You will need to set the the 'ylim' of the colorbar to only be the range of the indexed image and you will have to adjust the colormap and CData of the image to match this.
I am not completely sure what you are doing from this example, but it looks like you'll want something similar to what I did here: FEX:meshCanopy
  1 Comment
Aaronne
Aaronne on 10 Dec 2012
Hi Sean,
Thanks for your answer. I looked at your example, but still not sure how to do my job properly.
What I need is simple. I have a gray level image (background). I overlay an index image over this gray level image. The overlaid image will be given some color map, for example: jet.
1. I want to display a color bar on the side of the image. If I use 'colorbar' command directly, it will display the color bar for the gray level image. But what I need is the color bar of the overlaid image.
2. I need to fix the range of the color map for several overlaid index image + gray level background.
I don't understand how you use two handles like
colormap(Cmap)
set(H(1),'CData',I);
set(H(2),'CData',Midx);
that is with one color map, right?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!