MATLAB Answers

How to overlay a foreground image over a background image?

465 views (last 30 days)
How do I overlay an image over a background image in a figure window in MATLAB 8.1 R2013a?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Feb 2015
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);
 
 
 
  2 Comments

Sign in to comment.

More Answers (0)

Categories

Tags

No tags entered yet.

Products


Release

R2013a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!