Make only the highest values of grayscale image transparent

1 visualización (últimos 30 días)
Roger Beal
Roger Beal el 10 de Jun. de 2024
Respondida: DGM el 11 de Jun. de 2024
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

Respuestas (2)

Walter Roberson
Walter Roberson el 10 de Jun. de 2024
Editada: Walter Roberson el 10 de Jun. de 2024
image(BackgroundImage);
imageMax = max(YourGrayscaleImage(:));
alphaMask = double(YourGraycaleImage == imageMax);
hold on
image(YourGrayscaleImage, 'alphadata', alphaMask);
hold off
colormap(gray(256))

DGM
DGM el 11 de Jun. de 2024
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