Superimposing two imagesc graphs over each other.

177 views (last 30 days)
I have a matrix P which varies with time t,
P(:, :, t) ;
a variable Pmax,
Pmax = max(max(P(;,:,t)));
which I visualise through imagesc, step by step to create an animation:
figure
for i = 1:10:1000
imagesc(P(:, :, i), [0, Pmax]);
axis square
colorbar
end
I have another variable B, which I would like to visualise superimposed over the first imagesc plot, in a different colorbar/colour grading as P.
How could this be achieved? I was thinking of using a surf plot viewed from above instead (the two plots would be 2D, and the view set directly above to seem as a 2D plot). However to achieve this through the imagesc command would be much better.
Please share any ideas below, thank you!
  3 Comments
Shashvat Verma
Shashvat Verma on 20 Aug 2019
I was hoping to have the two images at 50 % transparency.
I wouldn't know how to map the images to the colormap manually, or how to put another set of axis over the other, if you could point me to the right direction with that?
The axis are the same for both images I am trying to overlap.
Thank you
Adam
Adam on 20 Aug 2019
How to map data onto a colourmap depends on the data and whether it is continuous or discrete. If you have an integer image, for example uint8, then something like e.g.
a = uint8( magic( 10 ) );
cmap = jet( 100 );
figure; imagesc( a );
colormap( cmap );
res = reshape( cmap( a, : ), [size(a) 3] );
figure; imagesc( res );
They should show the same image, but one uses the colourmap (and thus can be manipulated by instructions such as caxis) while the other is hard-mapped to RGB so is 3d.
If your data is floating point then you have to bin it to apply the colourmap. You can create a colourmap of whatever type and size you want, then bin your data into that many bins via histogram-based functions.
Likewise if you want the range to be different than the default you have to do more manipulations when mapping the data onto the colourmap (e.g. the above example only uses values 1 to 100 and these are mapped over the full colourmap range).
Transparency is applied by setting the 'AlphaData' property with imagesc, in your case you would set it to a scalar 0.5.
This will make the top image have 50% transparency. I doubt you want the bottom image to also have 50% transparency as this wouldn't make much sense with nothing underneath it.

Sign in to comment.

Accepted Answer

Subhadeep Koley
Subhadeep Koley on 22 Aug 2019
It is difficult to give exact solution without the exact data. But, below is an example of how you can superimpose 2 data using imagesc with different transparency & colormap.
%plot first data
ax1 = axes;
im = imagesc(ax1,imread('cameraman_512.png'));
im.AlphaData = 0.5; % change this value to change the background image transparency
axis square;
hold all;
%plot second data
ax2 = axes;
im1 = imagesc(ax2,imread('fighter.png'));
im1.AlphaData = 0.5; % change this value to change the foreground image transparency
axis square;
%link axes
linkaxes([ax1,ax2])
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
%add differenct colormap to different data if you wish
colormap(ax1,'summer')
colormap(ax2,'winter')
%set the axes and colorbar position
set([ax1,ax2],'Position',[.17 .11 .685 .815]);
cb1 = colorbar(ax1,'Position',[.05 .11 .0675 .815]);
cb2 = colorbar(ax2,'Position',[.88 .11 .0675 .815]);
cameraman_512.png
fighter.png
overlap.png
  2 Comments
Saeed Mohammadian
Saeed Mohammadian on 9 Sep 2019
Thank you very much.
That worked, I appreciate your time
Ritoban Basu Thakur
Ritoban Basu Thakur on 17 Oct 2024
@Subhadeep Koley this is a very nice construction, could you or someone put this on fileexchange? Something slightly different here https://www.mathworks.com/matlabcentral/fileexchange/26305-overlay-2-matrices-in-an-imagesc-like-plot, but I really needed your bit of code!! Thanks.

Sign in to comment.

More Answers (1)

DGM
DGM on 17 Oct 2024
Edited: DGM on 17 Oct 2024
It can be done, but unless your colormaps are very simple and non-intersecting, you can't expect to be able to visually (or even programmatically) interpret the values in either image anymore. At that point, there's not much point in even having colorbars.
If values don't matter, then composition can just be done in RGB without needing to mess with overlaying axes. Likewise, that allows us to save the result without resorting to screenshots.
% two arbitrarily-scaled images of the same geometry
A = im2double(imread('cameraman.tif'));
B = rand()*fliplr(A) + randn();
A = rand()*A + randn();
% two colormaps of any length
CTa = cool(256);
CTb = parula(16);
% convert both images to pseudocolor RGB
% default behavior scales color to the data extrema
% 'cdscale' mode emulates the quantization used by imagesc()
PCa = gray2pcolor(A,CTa,'cdscale');
PCb = gray2pcolor(B,CTb,'cdscale');
% combine the two images with some alpha
alpha = 0.5;
outpict = PCb*alpha + PCa*(1-alpha);
imshow(outpict)
... but if you can't actually read the values in either image, is the composite pseudocolor image worth having? Is there a better way to visualize ... whatever it is that needs to be visualized?
% two arbitrarily-scaled images of the same geometry
A = im2double(imread('cameraman.tif'));
B = rand()*fliplr(A) + randn();
A = rand()*A + randn();
% pick which is represented on which map axis
datax = A;
datay = B;
% generate a 2D colormap patch for use as a "colorbar" of sorts
% this does linear interpolation in sRGB, so expect it to be muddy
% both CT0 and CTpatch will be upside-down for correct mapping and presentation
mapw = 256; % width/height of map
CT0 = [1 1 0; 0 0 1; 1 0 0; 0 1 0]; % corner colors [NW; SW; NE; SE]
CT0 = 1 - CT0; % seems a bit more visually distinct when inverted
CT0 = flipud(reshape(ctflop(CT0),2,2,3)); % now map is a 2x2 RGB image
xq = linspace(1,2,mapw);
yq = xq.';
CTpatch = zeros(mapw,mapw,3);
for c = 1:3
CTpatch(:,:,c) = interp2(CT0(:,:,c),xq,yq,'bilinear');
end
% interpolate data to make pseudocolor image
% specify color axes extents for colormapping
climx = imrange(datax);
climy = imrange(datay);
% clamp data to map extrema
xq = imclamp(datax,climx);
yq = imclamp(datay,climy);
% interpolate
dataimage = zeros([size(datax) 3]);
for c = 1:3
dataimage(:,:,c) = interp2(climx,climy,CT0(:,:,c),xq,yq,'bilinear');
end
% plot the data
subplot(1,2,1)
imshow(dataimage)
% plot a "colorbar"
subplot(1,2,2)
image(climx,climy,CTpatch)
set(gca,'ydir','normal')
axis square
grid on
xlabel('Image A')
ylabel('Image B')
That's confusing to read, but at least it's unambiguous.

Categories

Find more on Colormaps 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!