Making White Pixels in an Opaque TIF Image Transparent and Saving as a PNG
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi everyone,
I have a TIF file stored in the .mat file named 'Opaque_Image.mat'. I have attached this .mat file for your reference. It is a 944x944x3 uint8 type of image and contains a colorful circle surrounded by white pixels. I wrote the .m file below to turn this image into a PNG file which would have the white pixels in the original TIF file replaced with transparent pixels. Unfortunately I end up getting a PNG file which has opaque black pixels instead of transparent pixels. Could you please help me solve this problem? Thank you for your answers in advance.
clc
clear all
load('Opaque_Image.mat')
[K L M]=size(opaque_image);
A = ones(K,L);
for i=1:K
for j=1:L
if opaque_image(i,j,1) == 255 && opaque_image(i,j,2) == 255 && opaque_image(i,j,3) == 255
A(i,j) = 0;
end
end
end
AA = logical(A);
mask = cast(AA, class(opaque_image));
transparent_image = opaque_image .* repmat(mask, [1 1 3]);
imwrite(transparent_image,'Transparent_Image.png');
0 commentaires
Réponse acceptée
DGM
le 24 Avr 2021
Modifié(e) : DGM
le 24 Avr 2021
Yeah imwrite() is really clumsy about handling certain file types and options. For PNG, the alpha channel has to be separated and passed as an optional argument.
dd = load('Opaque_Image.mat');
inpict = dd.opaque_image;
th = 253; % pick some upper threshold
whmask = ~all(inpict>th,3);
imwrite(inpict,'circlething.png','alpha',double(whmask))
Of course, now you can't really tell because the website bg is white ...
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!