Make only the highest values of grayscale image transparent

1 次查看(过去 30 天)
For a grayscale image with values from 0 to 255, I would like to make only the highest values transparent, so the rest of the image will be black when I overlay it on another image

回答(2 个)

Walter Roberson
Walter Roberson 2024-6-10
编辑:Walter Roberson 2024-6-10
image(BackgroundImage);
imageMax = max(YourGrayscaleImage(:));
alphaMask = double(YourGraycaleImage == imageMax);
hold on
image(YourGrayscaleImage, 'alphadata', alphaMask);
hold off
colormap(gray(256))

DGM
DGM 2024-6-11
I'm going to assume that the goal is to combine two images of the same page geometry and then save the result. If the end goal is to have a composite image to keep, then don't rely on in-figure composition and screenshots. Just compose the image.
Generate a mask by some means -- for example, logical thresholding:
% two images of the same class, depth, and page geometry
BG = imread('cameraman.tif'); % uint8
FG = fliplr(BG); % a second test image
% select everything lighter than 65% gray
mask = FG > (255*0.65);
% assemble the output using logical indexing
outpict = BG;
outpict(mask) = FG(mask);
imshow(outpict,'border','tight')
Of course that's not robust at all. If you want to deal with soft or graduated masks, color images, or mixed numeric classes, then there are plenty of examples:
If your goal is something else, then you'll have to elaborate.

Community Treasure Hunt

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

Start Hunting!

Translated by