How can I overlay in color a matrix over a grey scale image with Matlab2020

I have a a grey scale image I, and an array A of the same size that contains double between m and M. I would like to overlay in transparency the values contained in A over I, using the colormap jet for instance. I have attached an image of what I want.
With the previous version of Matlab I had, I was able to do so using imoverlay, specifying a background and a front image. But now, imoverlay is only for A binary, so my code is not working anymore.
I have all the toolboxes, in particular Image processing toolbox.
Thank you for your help

Antworten (1)

The function imagesc can be used to produce the desired results.
Assuming that I and A are 2-dimensional arrays, the following code snippet shows the image overlaying process with ‘jet’ colormap:
img = imagesc(I);
img.AlphaData = A;
colormap('jet');
You can go through the following link for further help:

5 Kommentare

Hello, Thank you for your answer, I tried this :
figure()
img = imagesc(I)
img.AlphaData = A;
img.AlphaData=0.8;
colormap('jet')
It does not give me the result I want. Instead, I get the image "I" displayed, in the color of 'jet'. I is a grey image and now it appears in color, with the brightest pixels in red and the darkest in blue. A does not appear at all on the figure. Moreover, the transparency apply to the image I, and not A. So for instance if I replace 0.8 to 0.0, I just get a white image.
What do you think I missed ?
PS : I found a custom-made "imoverlay.m" file that is doing this superposition in transparency. It wasn't in my MATLAB folder anymore after the update that's why it was not working. I put it back so now my code is working but I am curious to understand your method
Try casting A from double to uint8.
Attach A and I in a .mat file so people can try things
save('answers.mat', 'A', 'I');
Ok, I have put A (still double) and I in this mat file
I tried to cast A from double to uint8 but it didn't change anything
figure()
img = imagesc(I)
B=uint8(A);
img.AlphaData = B;
img.AlphaData=0.8;
colormap('jet')
This isn't really an issue with class. This is simply a problem with trying to have two independently colormapped objects in the same axes. Take one or both images and convert them to RGB so that they aren't colormapped anymore. In this example, I simply expand I on dim3 to prevent it from being colormapped. The second image isn't expanded, so it's rendered with the current colormap (jet).
load answers.mat
alpha = 0.5;
imshow(repmat(I,[1 1 3])); hold on
hi = imshow(A);
hi.AlphaData = alpha;
colormap(jet)
If the goal is to create an image, I'd avoid using graphics tools as an ad-hoc compositor. You can just combine the two images directly.
load answers.mat
alpha = 0.5;
% colormap the foreground image
A = ind2rgb(im2uint8(A),jet(256));
% composite via scalar alpha blending
outpict = replacepixels(A,I,alpha);
imshow(outpict)
That uses replacepixels() from MIMT. Basic alpha blending can be done without higher level tools, but if you're rolling your own, you will have to pay attention to image class, scaling, and depth.
load answers.mat
alpha = 0.5;
% colormap the foreground image
A = ind2rgb(im2uint8(A),jet(256));
% scalar alpha blending again
% images need to be floating-point and share a common scale
% depth should match, though in this case, implicit expansion works
I = im2double(I);
outpict = alpha*A + (1-alpha)*I;
imshow(outpict)
The output is the same as above

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 27 Aug. 2020

Kommentiert:

DGM
am 21 Okt. 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by