How to add a 'colorbar' and set 'clim' to a colour image overlaid on a grey level image?

13 views (last 30 days)
Hi all,
I am trying to overlay a colour image on a grey level image. However, when I try to plot the 'colorbar' and set the 'clim'. Matlab always produce a colorbar according to the underneath grey level image.
However, I want to get the colorbar for the overlaid colour image. Any suggestions would be appreciate. Thanks a lot.
%%Example codes:
greyImage = imread('AT3_1m4_08.tif');
colorImage = imread('hestain.png');
figure,
greyImagePlot = image(greyImage); colormap(gray); hold on;
overlayImage = imagesc(colorImage, ...
'CDataMapping', 'scaled', 'HitTest', 'off');
alF = 0.5.*ones(size(colorImage, 1), size(colorImage, 2));
set(overlayImage, 'AlphaData', alF);
colorbar; % This will show a grey scale colorbar not the colour one I want
set('CLim', [0 100]); % Also, the colormap limit here is not working
axis off
axis image

Accepted Answer

Sean de Wolski
Sean de Wolski on 27 Jun 2013
  2 Comments
Aaronne
Aaronne on 27 Jun 2013
No working. My question is not set a colorbar of a certain range, but the colorbar of the overlaid color image.
Sean de Wolski
Sean de Wolski on 27 Jun 2013
Ahh. So your color image is still RGB. It's not being shown as indexed into the colormap which is why it's not showing up. In order for it to show up with the colorbar, you will have to convert it to an indexed image using ind2rgb.
%%Example codes:
greyImage = imread('AT3_1m4_08.tif');
colorImage = imread('hestain.png');
[colorImg,map] = rgb2ind(colorImage,256);
figure,
greyImagePlot = image(greyImage);
colormap([gray(256);map]);
hold on;
overlayImage = image(double(colorImg)+256, ...
'CDataMapping', 'scaled', 'HitTest', 'off');
alF = 0.5.*ones(size(colorImage, 1), size(colorImage, 2));
set(overlayImage, 'AlphaData', alF);
%colorbar; % This will show a grey scale colorbar not the colour one I want
%set('CLim', [0 100]); % Also, the colormap limit here is not working
axis off
axis image
%Now this
h = colorbar;
set(h,'YLim',[257 512]);
As you can see though, the conversion to indexed image defintiely causes a loss of resolution.

Sign in to comment.

More Answers (1)

C Stewart
C Stewart on 20 Jun 2014
I just had the same problem - here's one workaround which seems to leave a functional colorbar:
(1) plot the color image (2) make the colorbar (3) plot the grayscale (4) send the grayscale to the back (uistack(h,'bottom'))
however this surely shouldn't be required... mathworks any comment???
  1 Comment
Image Analyst
Image Analyst on 20 Jun 2014
Sean works for the Mathworks.
I don't see the need. Isn't the RGB color image just the grayscale image with the colorbar applied? If so, then why is it not okay to show the gray scale image only, with the colormap applied and the color bar showing? It would be the same thing. Why is there a need to display a true color RGB image when the pseudocolored grayscale image would look identical ?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!