MATLAB Answers

Issue reading (or?) writing transparency data from PNG with transparent background and feathered edges.

1 view (last 30 days)
Maxwell Bennett
Maxwell Bennett on 31 Jul 2021
Edited: Maxwell Bennett on 31 Jul 2021
I need to load pngs with a transparent background and feathered edges (ex: facesfeathered1.png) into matlab and superimpose them on top of another (noise) image at different levels of transparency. For whatever reason, matlab isn't reading, writing, or(?) showing any of the transparency data from the original png, so the background is white and the feathering around the face is black (ex: stim_1_transp_107.png). Below is the code to load and resize the transparent image & the code to superimpose the two images and save it.
[im1,~,alpha] = imread(fullfile(dataFolder,[imageName imageID '.png'])); %Load the image
im1 = imresize(im1,[512 512]);
alpha = imresize(alpha,[512 512]);
imwrite(im1, ['image' imageID '.png'], 'Alpha', alpha);
for k = 0:255
f1 = imshow(randImage,'Border','tight'); %display noise image
hold on; %Keep that one on the screen
h = imshow(im1,'Border','tight'); %put original image on top
hold on;
set(h, 'AlphaData', k/nSteps); %change alpha level of original image
q = getframe(gcf); %capture data for the figure
y = q.cdata(:,:,1); %grabs one color channel (want grayscale)
imwrite(y,[saveFolder,'stim_',imageID,'_transp_',num2str(k),'.png'],'png'); %save the image
clf %clear the figure to speed things up
end
Any help is appreciated, thanks!
  2 Comments
Maxwell Bennett
Maxwell Bennett on 31 Jul 2021
I couldn't find a way to specifically load alpha content for imshow by looking online or in the help info, so I thought it would be included just by calling the re-sized and saved im1? How can I use the alpha of the original image?
Every pixel in a png has r/g/b/alpha channels, right? The original png has transparent pixels on the outside (alpha 0), an opaque face in the center (alpha 1), and a feathered edge between them (alpha <1, >0). I'm trying to preserve all of those values and multiply them by a coefficient k/nSteps, then render the image on top of the noise image and save the result.
I'm sorry if this should be obvious, I'm very new to Matlab, I've just been teaching myself as I go over the past few weeks.

Sign in to comment.

Accepted Answer

DGM
DGM on 31 Jul 2021
Edited: DGM on 31 Jul 2021
Imshow() and most things in IPT aren't really set up to handle RGBA data directly in any convenient way. This is an example of one way to do it.
[inpict,~,alpha] = imread('facesfeathered1.png');
% make background
sout = size(inpict);
squaresize = [10 10];
xx = mod(0:(sout(2)-1),squaresize(2)*2)<squaresize(2);
yy = mod(0:(sout(1)-1),squaresize(1)*2)<squaresize(1);
cbpict = 0.3 + bsxfun(@xor,xx,yy')*0.4;
subplot(2,1,1)
imshow(cbpict,'border','tight'); hold on;
h = imshow(inpict,'border','tight');
set(h,'alphadata',alpha)
subplot(2,1,2)
imshow(cbpict,'border','tight'); hold on;
h = imshow(inpict,'border','tight');
set(h,'alphadata',alpha*0.5)
For some reason, it won't run in the web editor, but it runs fine in R2019b. If it breaks in newer versions, idk what changed.
Of course, you don't need to rely on imshow to do the compositing. You can just build the image yourself.
[inpict,~,alpha] = imread('facesfeathered1.png');
inpict = im2double(inpict);
alpha = im2double(alpha);
% make background
sout = size(inpict);
squaresize = [10 10];
xx = mod(0:(sout(2)-1),squaresize(2)*2)<squaresize(2);
yy = mod(0:(sout(1)-1),squaresize(1)*2)<squaresize(1);
cbpict = 0.3 + bsxfun(@xor,xx,yy')*0.4;
outpict1 = inpict.*alpha + cbpict.*(1-alpha);
outpict2 = inpict.*alpha*0.5 + cbpict.*(1-alpha*0.5);
subplot(2,1,1)
imshow(outpict1,'border','tight');
subplot(2,1,2)
imshow(outpict2,'border','tight');
I don't do either. I just use imshow2() from MIMT on the File Exchange:
[inpict,~,alpha] = imread('facesfeathered1.png');
rgbapict = cat(3,inpict,alpha);
imshow2(rgbapict); % many MIMT tools can use IA/RGBA images
Of course, imshow2() doesn't do some things that imshow() does, and it doesn't play nice with some of the view controls in the newest versions.
  1 Comment
Maxwell Bennett
Maxwell Bennett on 31 Jul 2021
Thank you so much, that's a thorough answer. I didn't call alpha correctly in set(), so changing
set(h, 'alphadata', (k/nSteps));
to
set(h, 'alphadata', alpha*(k/nSteps));
fixed it.
Really appreciate you taking the time to explain it for me :)

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!

Translated by