Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to superimpose 2 image using MATLAB, thanks

2,571 views
Skip to first unread message

Jani Korhonen

unread,
Feb 10, 2011, 3:21:06 PM2/10/11
to
Dear all,

I want to show a heatmap (e.g. hm) on another image (e.g. im), how can I do this? I have tried the following codes:

imagesc(hm), colormap(hot);
axes('color','none')
hold on, h=imagesc(im);
alpha(h, 0.5);

However, the displayed hm and im have different sizes (but their resolutions are same to each other), and the image im was converted vertically.

I am looking forward to any suggestions. Thanks a lot.

Best regards,

Jani

Jani Korhonen

unread,
Feb 10, 2011, 3:34:04 PM2/10/11
to
"Jani Korhonen" <eagle...@hotmail.com> wrote in message <ij1hbi$ep4$1...@fred.mathworks.com>...

BTW, I want the heatmap hm to be transparent.

ImageAnalyst

unread,
Feb 10, 2011, 4:05:57 PM2/10/11
to
On Feb 10, 3:34 pm, "Jani Korhonen" <eagle_no...@hotmail.com> wrote:
> "Jani Korhonen" <eagle_no...@hotmail.com> wrote in message <ij1hbi$ep...@fred.mathworks.com>...

> > Dear all,
>
> > I want to show a heatmap (e.g. hm) on another image (e.g. im), how can I do this? I have tried the following codes:
>
> > imagesc(hm), colormap(hot);
> > axes('color','none')
> > hold on, h=imagesc(im);
> > alpha(h, 0.5);
>
> > However, the displayed hm and im have different sizes (but their resolutions are same to each other), and the image im was converted vertically.
>
> > I am looking forward to any suggestions. Thanks a lot.
>
> > Best regards,
>
> > Jani
>
> BTW, I want the heatmap hm to be transparent.

------------------------------------------------------------------------
http://www.mathworks.com/support/solutions/en/data/1-194D0/?solution=1-194D0
http://blogs.mathworks.com/steve/2008/08/20/image-visualization-using-transparency/

or just search for "transparent image" on MATLAB Central.

Jani Korhonen

unread,
Feb 10, 2011, 4:57:04 PM2/10/11
to
Hi, thanks for the information, I studied the ideas given by Steve and tried to implement it. My cods are like this:

imshow(im);
hold on, h=imagesc(hm), colormap(hot);
buff=logical(hm>0);
set(h, ''AlphaData', buff);

But what I got is not like the superimposition of two images. Was I doing something wrong?

Thanks a lot!

ImageAnalyst <imagea...@mailinator.com> wrote in message <23edcac7-2857-4f63...@z27g2000prz.googlegroups.com>...

mdgoh...@gmail.com

unread,
Apr 2, 2015, 2:41:46 AM4/2/15
to
can i have a matlab code that you have re-shearched for your project that could be used for final year project in electronics and communication field

Ken Garrard

unread,
Apr 4, 2015, 8:19:19 AM4/4/15
to
"Jani Korhonen" <eagle...@hotmail.com> wrote in message <ij1mvg$hu1$1...@fred.mathworks.com>...
Jani,
Try using the subimage function to plot the second image. I wrote code for this last summer and here is my function addimage that includes an option for scaling the overlaid image.

----

function h = addimage(img,ax,opt)
% ADDIMAGE Add an image to an existing set of axes
% h = addimage(img,ax,delta,crop)
% Input
% img image data (N x M or N x M x 3)
% ax axes handle, default = gca
% opt 'crop' the image is cropped to fit in the axes
% 'fit' the image is resized to fit in the axes (default)
% 'expand' the axes are expanded to show all of the image
% Output
% h handle of new image object

% Copyright 2015, North Carolina State University
% Written by Ken Garrard

% Check number of arguments
narginchk(1,3);

% Validate axes argument, default is current axes
if nargin < 2 || isempty(ax), ax = gca;
elseif ~ishghandle(ax) || ~strcmpi(get(ax,'type'),'axes')
error('Invalid axis handle argument');
end

% Validate option argument
if nargin < 3, opt = 'fit'; end
opt = find(strcmpi(opt,{'crop' 'fit' 'expand'}) > 0, 1);
if isempty(opt), opt = 2; end

% Range of values in a vector
rangefun = @(v)(max(v(:))-min(v(:)));

% Get axes limits and center
xl = xlim(ax);
yl = ylim(ax);
x0 = sum(xl)/2;
y0 = sum(yl)/2;

% Size of the image
[r,c] = dealcol(size(img));

% Resize the image to fit in the axes
if opt == 2
if rangefun(xl) > rangefun(yl)
[locX,locY] = resizeXY(xl,yl,c/r);
else [locY,locX] = resizeXY(yl,xl,r/c);
end

% Let the axes expand to accommodate the image
else
locX = [x0-c/2 x0+c/2];
locY = [y0-r/2 y0+r/2];
end

% Save hold state and switch it to ON
nxtplt = get(ax,'nextplot');
set(ax,'nextplot','add');

% Add the image to the current axes
set(ax,'XLimMode','auto','YLimMode','auto');
h = subimage(locX,locY,img);

% Restore the hold state
set(ax,'nextplot',nxtplt);

% Crop the image to fit in the axes
if opt == 1
xlim(xl);
ylim(yl);
end

% --- Rescale axis ranges while preserving aspect ratio
function [ud,vd] = resizeXY(ud,vd,rat)
if rat*rangefun(vd) < rangefun(ud)
ud = [ud(1) ud(1) + rat * rangefun(vd)];
else vd = [vd(1) vd(1) + 1/rat * rangefun(ud)];
end
end

end

----
Use the handle returned by addimage to set the 'alphadata' property to get the transparency level you want. For example,

p = peaks(201);
q = imread('pout.tif');
figure;
imagesc(p);
h = addimage(q,gca,'fit');
set(h,'alphadata',0.5);

Hope this helps,
Ken

JWall

unread,
Sep 19, 2016, 9:36:09 PM9/19/16
to
@Ken: Came across your code and just wanted to say thanks for sharing - worked perfectly for my application!


"Ken Garrard" wrote in message <mfoks1$6ov$1...@newscl01ah.mathworks.com>...
0 new messages