MATLAB: How to overlay a foreground image over a background image

MATLAB

How do I overlay an image over a background image in a figure window in MATLAB 8.1 R2013a?

Best Answer

MATLAB 8.1 R2013a does not have a simple way to place 2 images in a figure window. However, there is a workaround:
1) Create a figure 'f'. 
CODE
figure1 = figure;
2) Create 2 AXES ax1 and ax2 with 'f' assigned as the parent to both axes.
CODE
ax1 = axes('Parent',figure1);
ax2 = axes('Parent',figure1);
  3) Set the 'Visibility' property for both of these to off. 
CODE
set(ax1,'Visible','off');
set(ax2,'Visible','off');
 
4) Read the first image using IMREAD and capture the output parameters in [a,map,alpha].
CODE
[a,map,alpha] = imread('foreground.png');
5) Display the image using IMSHOW on the first axes and store the handle in a variable 'I'.
CODE
I = imshow(a,'Parent',ax2);
 
6) Set the 'AlphaData' property of 'I' to 'alpha' retrieved in the Step 3.
CODE
set(I,'AlphaData',alpha);
 
7) Display the background image on the second axis using IMSHOW. 
CODE
imshow('Background.jpg','Parent',ax1);