There are two basic ways to superimpose images in MATLAB. One involves using transparency for overlaying images objects that may not be exact rectangles, and the other involves indexing into the image data to replace pixels.
Using transparency allows you to achieve the effect of partly seeing through a pixel, rather than completely hiding the background. Indexing allows you to effectively create a new image object that is a combination of pixels from multiple image objects.
-----------------------------------------
1. Using transparency to superimpose images:
Suppose you have an image with pixels that are transparent or partially transparent, and you have another image that should serve as the background. You want the first image to let the background show through wherever there is transparency.
Note that, when using this method, you are actually dealing with multiple image objects. The superimposing is achieved by overlapping the image objects. Additionally, when using semitransparent pixels, you can achieve the effect of partially seeing through a pixel rather than completely hiding the background.
Here is an example that illustrates this method. Over a background image, you will overlay another image at a location within the background. If you are simply trying to place images within other images without using transparency, then you can ignore the code that sets the 'AlphaData' property of the image objects:
bg = ind2rgb(repmat(1:64,64,1),jet(64));
im = rand(100,100,3);
imAlphaData = repmat(0:1/size(im,2):1-1/size(im,2),size(im,1),1);
hf = figure('units','normalized','position',[.2 .2 .6 .6]);
ax1 = subplot(2,3,1);
ibg = image(bg);
axis off
title('Background')
ax2 = subplot(2,3,4);
iim = image(im);
axis off
title('Image without transparency yet')
ax3 = subplot(2,3,[2:3, 5:6]);
ibg2 = image(bg);
axis off
hold on
iim2 = image(im,'XData',[30 50],'YData',[10 30]);
set(iim2,'AlphaData',imAlphaData);
title(sprintf('Using transparency while overlaying images:\nresult is multiple image objects.'))
If you want to extract a circle from the image, and overlay just that
(NOTE: This part of the example requires the Image Processing Toolbox function ROIPOLY to create the circle mask.):
t = 0:.1:2*pi;
[imheight,imwidth] = size(im(:,:,1));
x = round(20*cos(t)+round(imwidth/2));
y = round(20*sin(t)+round(imheight/2));
imCircleAlphaData = roipoly(im,x,y);
axes(ax3);
iim3 = image(im,'XData',[0 30],'YData',[30 60]);
set(iim3,'AlphaData',double(imCircleAlphaData));
-----------------------------------------
2. Using indexing to superimpose images:
Use this method if you have an image that you want to place in a scene, but prefer to have only a single image as a resultant. The following example achieves this by replacing the scene pixels with the object image pixels.
scene = rand(100,100,3);
obj = ind2rgb(repmat(1:64,64,1),jet(64));
hf2 = figure('units','normalized','position',[.2 .2 .6 .6]);
axi1 = subplot(2,3,1);
iscene = image(scene);
axis off
title('Scene')
axi2 = subplot(2,3,4);
iobj = image(obj);
axis off
title('Object image')
result = scene;
rowshift = 20;
colshift = 0;
result((1:size(obj,1))+rowshift, (1:size(obj,2))+colshift, :) = obj;
ax3 = subplot(2,3,[2:3, 5:6]);
iresult = image(result);
axis off
hold on
title(sprintf('Using indexing to overlay images:\nresult is one image object'))
If you want to extract a circle from the object, and overlay just that:
(AGAIN, NOTE: This part of the example requires the Image Processing Toolbox function ROIPOLY to create the circle mask.):
t = 0:.1:2*pi;
[objheight,objwidth] = size(obj(:,:,1));
x = round(20*cos(t)+round(objwidth/2));
y = round(20*sin(t)+round(objheight/2));
objCircleMask = repmat(roipoly(obj,x,y),[1,1,3]);
rowshift = 0;
colshift = 30;
result2 = result;
replacementPixels = ( result((1:size(obj,1))+rowshift, (1:size(obj,2))+colshift, :) ...
.* double(~objCircleMask) ) + ( obj .* double(objCircleMask) );
result2((1:size(obj,1))+rowshift, (1:size(obj,2))+colshift, :) = replacementPixels;
axes(ax3);
delete(iresult)
iresult2 = image(result2);
If you need to resize the object image before placing it in the scene, then you will find the Image Processing Toolbox's IMRESIZE command useful: