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
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?
채택된 답변
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.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!