フィルターのクリア

How to change the black color into transparency in png format?

13 ビュー (過去 30 日間)
Mei Synn Tan
Mei Synn Tan 2017 年 5 月 9 日
コメント済み: Walter Roberson 2021 年 3 月 12 日
colorImage = imread('13100.png');
grayImage = rgb2gray(colorImage);
mserRegions = detectMSERFeatures(grayImage);
mserRegionsPixels = vertcat(cell2mat(mserRegions.PixelList));
mserMask = false(size(grayImage));
ind = sub2ind(size(mserMask), mserRegionsPixels(:,2), mserRegionsPixels(:,1));
mserMask(ind) = true;
f=figure();imshow(mserMask,'Border','tight');
maskedimage = immultiply(colorImage, repmat(mserMask, [1, 1, size(colorImage, 3)]));
imshow(maskedimage,'Border','tight');
TransparencyMaskedImage = maskedimage;
TransparencyMaskedImage(repmat(~mserMask, 1, 1, 3)) = ??;
imshow(TransparencyMaskedImage,'Border','tight');

採用された回答

Walter Roberson
Walter Roberson 2017 年 5 月 9 日
To just write it transparent with imwrite, you do not need any of the three lines
TransparencyMaskedImage = maskedimage;
TransparencyMaskedImage(repmat(~mserMask, 1, 1, 3)) = ??;
imshow(TransparencyMaskedImage,'Border','tight');
instead,
imwrite(maskedimage, 'YourFileName.png', 'Transparency', [0 0 0]);
If you want to display the image with transparency, then
mask = double( any(maskedimage, 3) );
image(maskedimage, 'AlphaData', mask ) %notice this is not imshow
  1 件のコメント
Mei Synn Tan
Mei Synn Tan 2017 年 5 月 9 日
編集済み: Mei Synn Tan 2017 年 5 月 9 日
Thanks for helping. Have a nice day

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

その他の回答 (1 件)

Muhammad Zeeshan Ahmed Khan
Muhammad Zeeshan Ahmed Khan 2021 年 3 月 12 日
When you image() or imagesc() or imshow(), pass an option 'AlphaData' that is an array with the desired transparency, with 1 meaning to show the image completely and 0 meaning not showing the image at all (that is, show the background completely there.)
If the PNG had alpha information stored with it that you read with imread() then you should be able to use that transparency information directly.
[YourImage, ~, ImageAlpha] = imread('YourFile.png');
image(YourImage, 'AlphaData', ImageAlpha)

Community Treasure Hunt

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

Start Hunting!

Translated by