How can I make a specific color of an image transparent?
이전 댓글 표시
I have a binary image which I am overlaying on top of another image. I would like the white portion of the binary image to be transparent so that only the black covers the background image. I have tried everything with alphadata and can only seem to change the transparency of the entire binary image rather than a single color. Are there are methods which will solve me problem? Any help would be much appreciated.
Thanks
댓글 수: 1
Walter Roberson
2012년 1월 11일
set the alphadata of the binary image to be 1 minus the binary image (presuming that 1 in the binary image is for white.)
답변 (2개)
David Young
2012년 1월 10일
There may be a way that allows you to overlay the images using graphics operations, but here's an alternative way that does it by making a new array, which you can then display.
First some data for illustration:
img = imread('saturn.png'); % an rgb image
bw = sum(img,3) > 400; % a binary image to overlay
Now the main operation. (If the main image is grayscale rather than rgb, you don't need the call to repmat.) The parts of the main "under" the ones remain unchanged, the parts "under" zeros get set to zero.
mask = cast(bw, class(img)); % ensure the types are compatible
img_masked = img .* repmat(mask, [1 1 3]); % apply the mask
Display the result:
imshow(img_masked);
댓글 수: 6
Spencer
2012년 1월 11일
MUHAMMAD ZAHID
2021년 2월 17일
I have same issue.
Kindly provide full coding by using both images.
One as Background (RGB)
&
Second as Foreground (White part of Black & White transparent)
Walter Roberson
2021년 2월 18일
See my comment above about AlphaData. 0 is transparent, so draw the background first, hold, draw the foreground with AlphaData set 0 for places to be transparent
@Walter RobersonI have similar question. Alpha set to 0 works perfectly for setting the second layer to be transparent, but my issue is I need alpha color to be gray other than always white. Is there a solution/work around for this? Thank you so much!
Walter Roberson
2021년 5월 7일
If this is for display purposes then draw a grey background first and then draw the foreground with AlphaData set to 0 for the places intended to be transparent.
joann ren
2021년 5월 7일
편집: Walter Roberson
2023년 12월 15일
@Walter RobersonYes, thank you so much for your tip! I also made it by reading the following. This is exactly the solution you provided! learned. Thank you so much!! https://www.mathworks.com/company/newsletters/articles/image-overlay-using-transparency.html
You already have a mask, so I'm not concerned with how to create a mask.
% an image and an antialiased mask
inpict = imread('peppers.png'); % RGB, uint8
mask = imread('sources/standardmods/pep/redpepmask.png'); % I, uint8
% compose the output
% replacepixels(FG,BG,mask)
outpict = replacepixels(inpict,0,mask); % one line
% show it
imshow(outpict)

The image can be gray or RGB. The mask can be logical, binarized numeric, or graduated/antialiased numeric. The classes don't need to match, so long as everything is properly scaled for its class. It all works just the same. The output is a clean raster image instead of a stack of graphics objects destined to become a screenshot.
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!