Overlay transparent image on top of current figure

16 views (last 30 days)
Hello,
I have been attempting (for far to long) to overlay a transparent image of the outline of footprints on top of a figure I have created. I've view previous posts on this topic but I can't seem to get it to work. Below is my simple code thus far, and the image I would like to use is attached. Can anybody please help?
clear all
clc
figure
LeftPosterior = rectangle('Position',[0 0 1 1],'FaceColor',[0 .5 .5]);
hold on
RightPosterior = rectangle('Position',[1 0 1 1],'FaceColor',[0 .8 .8]);
LeftAnterior = rectangle('Position',[0 1 1 1],'FaceColor',[0 .9 .9]);
RightAnterior = rectangle('Position',[1 1 1 1],'FaceColor',[0 .3 .3]);
axis([0 2 0 2])
axes('position',[0 0 1 1]);
[img, map, alphachannel] = imread('Footprint Pic.png');
image(img, 'AlphaData', alphachannel);

Accepted Answer

DGM
DGM on 8 Feb 2023
Edited: DGM on 8 Feb 2023
Try this:
LeftPosterior = rectangle('Position',[0 0 1 1],'FaceColor',[0 .5 .5]);
hold on
RightPosterior = rectangle('Position',[1 0 1 1],'FaceColor',[0 .8 .8]);
LeftAnterior = rectangle('Position',[0 1 1 1],'FaceColor',[0 .9 .9]);
RightAnterior = rectangle('Position',[1 1 1 1],'FaceColor',[0 .3 .3]);
xrange = [0 2];
yrange = [0 2];
axis([xrange yrange])
[img, map, alphachannel] = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1289535/Footprint%20Pic.png');
image(xrange,yrange,img,'AlphaData',im2double(alphachannel));
% the origin of an image is the NW corner
% so you'll either have to flip the image and/or the y-axis
% to get things oriented the way you want
set(gca,'ydir','reverse');
Note that I flipped the y-axis. This is normally what image()/imshow() do when they're called first. If you want the origin to stay in the SW corner, you'll have to flip() the image. In general, you'd want to flip both the image and its alpha channel. In this specific case, there is actually no object content in img. The entire object content is in the alpha data, so you'd really only need to flip alpha.
  2 Comments
Josh Tome
Josh Tome on 8 Feb 2023
Awesome, thanks so much for the help! If I want to flip the alpha data, is the line of code similar to flipping the image?
Set(alphachannel,'ydir','reverse')
DGM
DGM on 8 Feb 2023
The images are just arrays, so:
alphachannel = flipud(alphachannel);
or
alphachannel = flip(alphachannel,1);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!