MATLAB: Alphamap using Binary Mask

alphamapdigital image processingimage processingImage Processing Toolboximage segmentation

Is it possible to make an alphamap for a figure based on its binary mask?
Here is a sample image and its binary:
I = imread('saturn.png');
a = im2bw(I, graythresh(I));
I want the regions in black to be made transparent and white regions to be opaque. So the only area visible is the image itself without the background.
Is this possible in Matlab? I couldn't find any information or examples in the documentation.

Best Answer

When you say "Being able to exclude the background of each image (i.e., the parts of the image that contain no useful data) would help a lot." It sounds like you want masking. So if the unwanted stuff was segmented so that it's white (like the Saturn in your binary image), then you can exclude that from the original image by masking like this:
% Mask the image using bsxfun() function

maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
that will give you a color image of the space/universe while the Saturn pixels would be completely black ("removed" from the image). Invert the mask using mask=~mask if you want the opposite stuff blacked out.
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(~mask, 'like', rgbImage));
If it's a simple grayscale image, you can erase mask pixels like this:
maskedGrayImage = grayImage; % Initialize
maskedGrayImage(mask) = 0;