How can insert PNG image as marker and remove transparent region?

Hey,
I was trying to insert a .png image as a marker into my plot. I am aware that to insert a image as marker you need find xdata/ydata and use hold to insert image into original plot. I was able to do that but I couldn't manage to show only the image, white-grey part is still there. What I want is to remove this white-grey part and only show the aircraft as marker.
My code: (marker position needs to be updated but this is good enough for testing)
clc
clear
x = linspace(1,10,20);
y = linspace(1,10,20);
[cdata,colormap] = imread("f16.png");
figure
hold on
p = scatter(x,y);
xdata = p.XData;
ydata = p.YData;
% imshow(cdata,colormap)
%
ax2 = axes("Position",[xdata(1,4)/10,ydata(1,4)/10,0.1,0.1]);
imshow(cdata,colormap,Parent=ax2);

 Accepted Answer

DGM
DGM on 9 Dec 2022
Edited: DGM on 9 Dec 2022
You need to read the alpha data from the image and then apply it to the image object.
See this thread. I include a similar method for using a transparent PNG as a custom marker:
In your case, you don't need the custom marker for all points, but the task of reading the image and setting the object properties should still apply.
Depending on where this is going, you might also find this useful:

2 Comments

I read your first response. I saw same solutions in other posts but imread does not return an alpha value for my .png file.
edit: Problem was with the picture. I tried another one. It's got alpha values. Will try it based on your suggestion asap.
Ah I didn't notice that was an indexed PNG. Yeah, you'll find fake transparent images all over the web. It's easy to fix in this case.
[inpict map] = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1225612/f16.png');
% index 2,4 (1,3) are the fake transparent BG
alpha = inpict~=1 & inpict~=3;
% convert to RGB to free up colormapping for other objects
inpict = ind2rgb(inpict,map);
% for sake of clarity against the white webpage
% i'm going to put another image behind it.
bgpict = imread('llama.jpg');
imshow(bgpict); hold on
% display the image at some arbitrary location/scale
hi = imshow(inpict,'xdata',400+[1 410],'ydata',250+[1 400]);
hi.AlphaData = alpha;

Sign in to comment.

More Answers (0)

Asked:

on 9 Dec 2022

Commented:

DGM
on 10 Dec 2022

Community Treasure Hunt

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

Start Hunting!