Alphamap using Binary Mask

2 ビュー (過去 30 日間)
Sordin
Sordin 2017 年 4 月 22 日
コメント済み: Walter Roberson 2017 年 4 月 24 日
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.
  2 件のコメント
Image Analyst
Image Analyst 2017 年 4 月 22 日
Do you mean that you want to create a PNG file with transparent pixels? If so, clarify which pixels are to be transparent - Saturn, or the space? Because when you say "the image itself", well, that is the entire image - both Saturn and the space. They're both part of the image.
Sordin
Sordin 2017 年 4 月 23 日
I want the space to be transparent. The Saturn must be completely opaque. I am working on stacking slices to make a volumetric representation of medical data. 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.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2017 年 4 月 23 日
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;
  2 件のコメント
Sordin
Sordin 2017 年 4 月 24 日
Thank you very much, the last one for grayscale images works perfectly. However, I get the following error using your code for an RGB image:
Error using cast
Too many input arguments.
Are you sure that is the correct syntax?
Walter Roberson
Walter Roberson 2017 年 4 月 24 日
You are using an older MATLAB that does not support that syntax. You can replace
cast(~mask, 'like', rgbImage)
with
cast(~mask, class(rgbImage))

サインインしてコメントする。

その他の回答 (2 件)

KSSV
KSSV 2017 年 4 月 22 日
I = imread('saturn.jpg') ;
I = rgb2gray(I) ;
I(I<50) = 255 ;
imshow(I)

Walter Roberson
Walter Roberson 2017 年 4 月 23 日
I = imread('saturn.png');
a = im2bw(I, graythresh(I));
image(I, 'alphadata', double(a) )
Alpha data needs to be between 0 and 1 (inclusive), so just double() the logical data to get suitable alpha data.

カテゴリ

Help Center および File ExchangeLighting, Transparency, and Shading についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by