How can I overlay transparent color on a grayscale image in MATLAB 6.0 (R12)?

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

MathWorks Support Team
MathWorks Support Team on 10 Nov 2022
Edited: MathWorks Support Team on 10 Nov 2022
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;
set(h,'CData',C,'CDataMapping','direct');
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.
help 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:
  1 Comment
DGM
DGM on 12 Nov 2022
Edited: DGM on 12 Nov 2022
This doesn't seem to address the given question. Why would colormap editing be the preferred method of combining two images? How would editing the colormap be accomplished by editing the CData array instead of the colormap? Could combining two indexed images be accomplished by concatenating the maps and inserting new indices? Yes, but none of that is demonstrated or even unambiguously described.
How would editing the z-values of a mesh plot in order to manipulate the colormapping of the mesh plot have anything to do with one or more images in the same figure? It doesn't address the fact that there are still at least two colormapped objects (the mesh plot and at least one image) in the same axes. Messing around with the mapping of the mesh plot only messes up the mapping of the image object.
The last "workaround" Is a vague concept that doesn't seem to even apply to the problem anyway. How would you find certain colors within the figure? You might find certain colors within the image, but is that what's being suggested? If so, what part of the question required color-based masking? I can only guess, since there is no related link.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 10 Nov 2022
Edited: MathWorks Support Team on 25 May 2023
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
DGM on 12 Nov 2022
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.

MathWorks Support

Categories

Find more on Colormaps 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!