Clear Filters
Clear Filters

Make only the highest values of grayscale image transparent

6 views (last 30 days)
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

Answers (2)

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

DGM
DGM on 11 Jun 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!