필터 지우기
필터 지우기

Make only the highest values of grayscale image transparent

조회 수: 6 (최근 30일)
Roger Beal
Roger Beal 2024년 6월 10일
답변: DGM 2024년 6월 11일
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