How can I overlay transparent color on a grayscale image in MATLAB 6.0 (R12)?
조회 수: 15(최근 30일)
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.
MathWorks Support Team 2022년 11월 10일
편집: MathWorks Support Team 2022년 11월 10일
The ability to overlay transparent color directly on a grayscale image is not available in MATLAB.
This is because for a grayscale image, the default figure colormap is also in grayscale, hence requiring colormap modification to display colors.
To work around this issue, you can modify the "CData" (Color Data) property of an object. For more information about the "CData" property, see the following URL:
It is also possible to modify the colormap based on z-values. Here is an example of how to do this:
h = mesh(peaks);
C = get(h,'ZData');
i1 = find(C<=-2);
i2 = find(C>-2 & C<=4);
i3 = find(C>4);
C(i1) = 1 + 0*i1;
C(i2) = 2 + 0*i2;
C(i3) = 3 + 0*i3;
colormap([.5 .5 .5; 1 0 0; 0 0 1])
The FIND command is used to find the Z values that were in the desired range. Type the following at the MATLAB command prompt for more information about FIND.
Another workaround is to find the instance of a certain color within the figure. Once you have found the location, you can then specify an alternate color value. More information on this process can be found in the Related Solution:
Image Analyst 2022년 11월 10일
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:
DGM 2022년 11월 12일
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?')
% 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
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.