Overlay transparent image on top of current figure

17 ビュー (過去 30 日間)
Josh Tome
Josh Tome 2023 年 2 月 8 日
コメント済み: DGM 2023 年 2 月 8 日
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);

採用された回答

DGM
DGM 2023 年 2 月 8 日
編集済み: DGM 2023 年 2 月 8 日
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 件のコメント
Josh Tome
Josh Tome 2023 年 2 月 8 日
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 2023 年 2 月 8 日
The images are just arrays, so:
alphachannel = flipud(alphachannel);
or
alphachannel = flip(alphachannel,1);

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by