How to overlay a foreground image over a background image?

146 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);
 
 
 
  1 Comment
DGM
DGM on 9 Aug 2022
Edited: DGM on 9 Aug 2022
Pay attention to what is actually being performed by this example. Two images are being composited using the alpha content of the foreground image. If your image file has no alpha content, then alpha will be an empty array, hence the mismatched array size error.
Let's disregard the error for a moment. Conceptually speaking, if the foreground has no alpha content, then it should be expected that it is treated as if opaque. If you're loading an image with no alpha content and expecting it to be rendered with some wholly unspecified alpha that neither I nor MATLAB can guess on your behalf, then you will be disappointed.
If you want some scalar opacity, then set alpha to a scalar.
ax1 = axes('Parent',gcf);
ax2 = axes('Parent',gcf);
set(ax1,'Visible','off');
set(ax2,'Visible','off');
bg = imread('peppers.png');
fg = flipdim(bg,2);
hi = imshow(fg,'Parent',ax2);
set(hi,'AlphaData',0.5);
imshow(bg,'Parent',ax1);
This should work in R2013a and earlier. I tested this in R2009b.
That said, I doubt anyone replying is using R2013a or older. If you aren't using a version that old, then this entire thread is not relevant to your needs. In contemporary versions, image objects can be stacked in a common axes just like other objects. There's no need for the workaround that is the focus of this thread; just stack the images and set the alpha.
bg = imread('peppers.png');
fg = flip(bg,2);
imshow('peppers.png'); hold on;
hi = imshow(fg);
set(hi,'AlphaData',0.5);
In either case, if your goal is to actually composite images for output to an image file, then using figures as an ad-hoc compositor is a bad idea in any version. If you want to composite images with associated alpha content or scalar opacity, then composite the images directly and save them with imwrite() instead of saving a screenshot of a figure.
These answers should serve as an introduction to basic compositing with and without third-party tools.

Sign in to comment.

More Answers (0)

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!