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

조회 수: 2 (최근 30일)
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
DGM
DGM 2021년 7월 31일
I'm not really sure what you're trying to do. You're never using the alpha content of the original image, so why would the edges be represented in the output? Do you want to use the orignal alpha or some fixed uniform alpha, or the product of the two?
Maxwell Bennett
Maxwell Bennett 2021년 7월 31일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2021년 7월 31일
편집: DGM 2021년 7월 31일
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
Maxwell Bennett
Maxwell Bennett 2021년 7월 31일
편집: Maxwell Bennett 2021년 7월 31일
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 :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by