Reputation: 2371
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
Upvotes: 1
Views: 6391
Reputation: 7751
A reference for single figure/multiple colormaps can be found here http://www.mathworks.fr/support/solutions/en/data/1-GNRWEH/index.html
Using images in particular, one can use 'subimage' function.
I also use 'FreezeColor' and 'cbfreeze' functions from 'matlabcentral' when a home-made solution is too tricky. http://www.mathworks.com/matlabcentral/fileexchange/7943-freezecolors-unfreezecolors http://www.mathworks.com/matlabcentral/fileexchange/24371
A straightforward - and lazy - solution for keeping a colorbar across multiple plots within the same axes: first plot the colour image and its colorbar, freeze the colorbar, then plot over the grey level image (no transparency), and finally plot the colour image again (transparency).
Here is a piece of code.
figure;
%first step: RGB image and colorbar
overlayImage = imagesc(colorImage, 'CDataMapping', 'scaled', 'HitTest', 'off');
alF = 0.5.*ones(size(colorImage, 1), size(colorImage, 2));
set(overlayImage, 'AlphaData', alF);
colorbar;
set(gca, 'CLim', [0 100]);
cbfreeze; %from 'COLORMAP and COLORBAR utilities' in Matlab Central
%second step: gray image (no transparency)
greyImagePlot = image(greyImage); colormap(gray); hold on;
%third step: plot colour image
overlayImage = imagesc(colorImage, ...
'CDataMapping', 'scaled', 'HitTest', 'off');
alF = 0.5.*ones(size(colorImage, 1), size(colorImage, 2));
set(overlayImage, 'AlphaData', alF);
axis off
axis image
Upvotes: 3