How can I overlay transparent color on a grayscale image in MATLAB 6.0 (R12)?
Show older comments
I have two figure windows that display images. I do not know beforehand the range of image intensities for each image. I would like one of the figures to display a grayscale image with a region of the image displayed in color. I would like to overlay some color on a grayscale background image so that the color is transparent.
Accepted Answer
More Answers (2)
Image Analyst
on 10 Nov 2022
Edited: MathWorks Support Team
on 25 May 2023
0 votes
Overlaying a transparent, color-tinted overlay onto a gray scale image is available using R2017b and later using the labeloverlay function.
Otherwise see Steve's blog:
Why is that the accepted answer for this question? That seems to have nothing to do with the question at all.
The question appears to be about doing local alpha blending between two images, or perhaps between a solid color patch and an image. It's reasonable to think that it's about doing in-figure compositing uising the available graphics tools, but since the graphics tools aren't even necessary to accomplish the composition, the limitations thereof really don't matter.
I'm going to pick one case to demonstrate -- a grayscale image with a partial overlay of a color image. In this case, both images have identical object content, but that's not required. The images only need to have the same class and geometry. I don't have R12 or documentation for R12, so forgive me if anything here doesn't work back that far. I've only tested this back to R2009b.
% i'm assuming that imread() could be used to read the file
FG = imread('peppers.png'); % assuming input is uint8
% idk if rgb2gray() existed, but if it did, it wouldn't be in the base toolbox
% how this is made gray is unimportant, so i'll just take the channel mean
% i'm assuming that mean() will accept integer-class inputs
BG = uint8(mean(FG,3)); % assuming input is uint8
% expand images as needed
fgchans = size(FG,3);
bgchans = size(BG,3);
if fgchans==3 && bgchans==1
BG = repmat(BG,[1 1 3]);
elseif fgchans==1 && bgchans==3
FG = repmat(FG,[1 1 3]);
elseif fgchans ~= bgchans
% i'm assuming error() is available in R12
error('one or both of the image is not a typical I/RGB image; what is this?')
end
% both images should now have the same number of channels
% define a region of interest that lies within the image geometry
xrange = 150:350;
yrange = 100:300;
% extract ROI from both images
ROIfg = double(FG(yrange,xrange,:));
ROIbg = double(BG(yrange,xrange,:));
% composite the image segments
alpha = 0.3; % pick some opacity
% i'm just going to dump back into ROIbg instead of making a new array
ROIbg = alpha*ROIfg + (1-alpha)*ROIbg;
% insert the result back into the original BG image
BG(yrange,xrange,:) = uint8(ROIbg); % assuming input is uint8
% class handling can be made more robust,
% but i'm not going to bother if i'm left guessing about what's in R12
% anyone familiar with R12 should be able to figure that out
% display the result
image(BG)
Disregarding my lazy treatment of input class, if someone can tell me why that won't work in R12, I'd be happy to learn what's changed.
As to the whole "I do not know beforehand the range of image intensities" comment, I don't know if that's to say that the images are potentially mismatched-class or if they're potentially arbitrarily-scaled floats. I'm going to choose to ignore that complication, as the OP no longer needs that detail answered, and I've already beaten both dead horses.
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!