3

I want to superimpose two images of same dimensions in matlab. I tried to use imfuse function but the image I got was not the same as I wanted.

The first image is the negative of the image obtained after applying Canny edge detector to my original image. I want to impose this negative image with black edges onto my original image.

Can someone suggest some other function or method for superimposition of two images ?? Thanks and Regards.

mcAfee
  • 209
  • 1
  • 3
  • 8

3 Answers3

6

You may use the 'AlphaData' property of the second image:

>> imshow( origImg ); hold on;
>> h = imagesc( edgeImg ); % show the edge image
>> set( h, 'AlphaData', .5 ); % .5 transparency
>> colormap gray
Shai
  • 111,146
  • 38
  • 238
  • 371
6

Try this to superimpose two images.

figure,imshowpair(originalImage,edgeImage);

This will give you a single figure which is a combination of the two. imshowpair has some additional options like blend,diff,montage. Try them too.

PRABHAKARAN
  • 101
  • 2
  • 6
2

I found something, I thought I should share here.

As Shai and Steve mentioned using AlphaData of an image gives a very nice result in many cases. However if you need to save the image with the original resolution (and not using getframe, print, saveas, etc), the following would help.

(I use the second example in Steve's article)

% Reading images
E = imread('http://www.mathworks.com/cmsimages/63755_wm_91790v00_nn09_tips_fig3_w.jpg');
I = imread('http://www.mathworks.com/cmsimages/63756_wm_91790v00_nn09_tips_fig4_w.jpg');

% normalizing images
E = double(E(:,:,1))./double(max(E(:)));
I = double(I(:,:,1))./double(max(I(:)));

Here is overlaying using AlphaData (Opacity):

figure, imshow(E), hold on
red = cat(3, ones(size(E)), zeros(size(E)), zeros(size(E)));
h = imshow(red);
set(h, 'AlphaData', I);

To get the exact same appearance as above but in one matrix (which I could not achieve using imfuse), you can use this simple code:

Comb = E;
Comb(:,:,1) = (1-I).*E + I; % red
Comb(:,:,2) = (1-I).*E; % green
Comb(:,:,3) = (1-I).*E; % blue

figure, imshow(Comb)

Hope it helps someone!

Community
  • 1
  • 1
p8me
  • 1,840
  • 1
  • 15
  • 23
  • nice! is it to somehow possible display multiple overlayed colors, instead of one single `reddish` color? I would like to display a superimposed blue, red, yellow, green for specific pixels. I've binary masks containing the pixels that should be superimposed for each color. – Tin Sep 06 '15 at 20:38