Overlay transparent image on top of current figure
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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);
0 comentarios
Respuesta aceptada
DGM
el 8 de Feb. de 2023
Editada: DGM
el 8 de Feb. de 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 comentarios
DGM
el 8 de Feb. de 2023
The images are just arrays, so:
alphachannel = flipud(alphachannel);
or
alphachannel = flip(alphachannel,1);
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!