make the white background transparent
Show older comments

Hello, I would like to convert the white pixels in the attached image to be 100% transparent. Could you please help with this? I'm going to eventually convert this image into an svg file to open in illustrator. Thank you.
Accepted Answer
More Answers (1)
Tangential, but ...
It's true IPT/MATLAB has no idea what to do with it, but it's certainly possible. I don't remember how much of it was in MIMT in 2016, but a large part of MIMT currently works with I/IA/RGB/RGBA images (but not everything). Then again, even IPT tools like imrotate() or imresize() don't really care how many channels there are. The biggest obstacle is the lack of an image viewer.
inpict = imread('diagram.jpg');
inpict = imresize(inpict,0.25); % doesn't need to be giant for a demo
% use image directly as alpha to preserve any antialiasing/linearity
% set color channel(s) to solid color (black) to avoid fringe effects
iapict = joinalpha(zeros(size(inpict)),255-inpict);
% write IA image
imwrite(iapict(:,:,1),'IA.png','alpha',iapict(:,:,2))
% display the image
imshow2(iapict,'tools')

... but MIMT imshow2() can handle IA/RGBA and multiframe images. View controls are a bit janky in new MATLAB versions, but it works.

Similarly with RGBA:
% use image directly as alpha
% set color channel(s) to solid color (purple)
cpict = colorpict([size(inpict) 3],[160 50 255],'uint8');
rgbapict = joinalpha(cpict,255-inpict);
% unnecessarily split color/alpha for sake of
% demonstrating convenience tools
[rgb alph] = splitalpha(rgbapict);
% write RGBA image
imwrite(rgb,'RGBA.png','alpha',alph)

Ultimately, the lack of IA/RGBA support is probably of little consequence to most people. I doubt most technical image processing tasks need it. Handling alpha is more of a manipulation/composition concern, and IPT doesn't have any composition tools, so there isn't much point unless you use third-party tools anyway.
P.S. For cases which aren't as simple as this one, one might alternatively use MIMT color2alpha() to do the same.
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!