make the white background transparent
51 views (last 30 days)
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.
0 Comments
Accepted Answer
Guillaume
on 7 Jun 2016
First, to answer your exact question: it's not possible in matlab, since matlab does not have a concept of a transparency channel.
A transparency channel is an additional channel (on top of the 1 grayscale or the 3 RGB channels) that indicates the transparency of each pixel. You could always create such a channel but matlab wouldn't know what to do with it except if you were to save the image as PNG (or possibly other formats that support transparency). Matlab certainly cannot display transparency.
So, if you want to play around with transparency, you're better off doing it in some other program. Your favorite image editor (GIMP, Photoshop, Paint.Net, etc.) is a lot better suited for this.
However, I don't see how converting the pixels to transparent is going to help for your ultimate goal. You're trying to convert a raster image into a vectorial image which basically means doing shape recognition. There's nothing built-in in matlab to do that. You can certainly use the image processing and computer vision toolbox to help you but you're going to have to work for this. Again, you're probably better off working in something other than matlab.
Note: to save that image as PNG with the white pixels transparent, assuming the image is loaded as uint8:
%img: source image as RGB uint8
alphachannel = all(img == 255, 3); %replace 255 with 1 if img is double
imwrite(img, 'somename.png', 'Alpha', alphachannel);
2 Comments
More Answers (1)
DGM
on 4 Apr 2022
Edited: DGM
on 27 May 2022
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!