Overlay two images using transparency

105 visualizaciones (últimos 30 días)
GPB el 28 de Nov. de 2023
Editada: DGM el 6 de Dic. de 2023
I'm trying to overlay two images using imagesc. The background image should be in grayscale while the foreground one should be colored (let's say Parula). Both images have the same dimensions, so the foreground will have to be trasnparent in some places. None of them is a binary mask.
I would like something like this: https://www.mathworks.com/matlabcentral/answers/475448-how-to-overlay-two-colormaps-using-imagesc-command BUT these shows two different-sized images, so they don't have to play with transparency.
close all
FA(FA == 0) = NaN; % I want NAN values to be transparent
A = imagesc(flipud(T1w')); % Background
hold on;
B = imagesc(flipud(FA'), [0 1]); % Foreground
And here I tried many things, most of them don't work. I succeed on making white the FA=0 values by applying a colormap [1 1 1; parula(512)]. Now I'd like the same but instead of white, transparent. Any idea?

Respuesta aceptada

Image Analyst
Image Analyst el 29 de Nov. de 2023
  2 comentarios
GPB el 6 de Dic. de 2023
Ok, last week I made it work. Thank you (also to Walter Roberson). I will leave here the code. Basically it was done by converting each image to uint8 and then using the ind2rgb function.
T1 = cast(255.*T1,'uint8');
FA = cast(255.*FA,'uint8');
idx = FA ~= 0;
% Normal image (Background) ------------------------------------
% Masked image (Foreground) ------------------------------------
% Overlaped images -------------------------------
T1 = ind2rgb(T1,gray);
FA = ind2rgb(FA,parula);
BG = imshow(T1); hold on; FG = imshow(FA);
set(FG,'AlphaData', idx);
Walter Roberson
Walter Roberson el 6 de Dic. de 2023
idx = FA ~= 0;
That is logical data.
set(FG,'AlphaData', idx);
Historically, setting the AlphaData of an image() object to logical values has given an error message. That is the reason that I used the 0+LOGICALEXPRESSION form -- zero plus a logical results in a double.
You could even use
set(FG,'AlphaData', +idx);
as the semi-obscure unary-plus operator converts logicals to doubles.

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 28 de Nov. de 2023
FA(FA == 0) = NaN; % I want NAN values to be transparent
A = imagesc(flipud(T1w')); % Background
hold on;
B = imagesc(flipud(FA'), [0 1], 'AlphaData', 0+isfinite(FA')); % Foreground, transparent at nan
  2 comentarios
GPB el 28 de Nov. de 2023
Ok, although your code does not work, I managed to make transparent the regions where FA is 0. (Indeed is by using AlphaData, isnan(FA), and moving the [0 1] scaling factor to the end of the imagesc call). Thank you for that. Now, how do I change the colormap in each of the images? As I said, I would like the background to be gray and the foreground colored.
There are solutions to this... (e.g. https://www.mathworks.com/matlabcentral/answers/272371-how-can-i-overlay-pseudocolor-images-with-different-colormaps) but somehow I cant make it work with the tight_subplot I'm using.
close all;
fig = figure('Position', [10 10 1400 600]);
[ha, pos] = tight_subplot(1,1,[.001 .001],[.08 .04],[.02 .06]);
FA(FA == 0) = NaN; % I want NAN values to be transparent
ax1 = ha(1);
imagesc(flipud(T1w')); % Background
colormap(ax1, gray(512)); % In grayscale
hold on;
ax2 = ha(1);
h2 = imagesc(flipud(FA'), 'AlphaData', ~isnan(flipud(FA')),[0 1]); % Foreground
colormap(ax2, parula(512))
(I know that in this toy example doesn't make much sense to use tight_subplot, but I'm using this code to build a bigger figure and would like to use it.)
Thanks again!
Walter Roberson
Walter Roberson el 28 de Nov. de 2023
any one axes can only have a single colormap. However you can ind2rgb to convert intensity information to rgb.

Iniciar sesión para comentar.

DGM el 6 de Dic. de 2023
Editada: DGM el 6 de Dic. de 2023
How would I do it?
If the goal is to produce a raster image as output, and you don't need other corresponding graphics objects (e.g. colorbars, rulers), I'd use MIMT gray2pcolor() and compose the output directly.
% create test images, show them
fg = imread('rad.png');
bg = fliplr(fg);
montage({fg bg},'border',5,'backgroundcolor','r')
% create alpha for foreground
mask = fg < 128; % whatever condition is appropriate
% convert both images to pseudocolor
% gray2pcolor() behaves similar to imagesc(), but with raster output
% similarly, you can either specify explicit input levels, or use image extrema
bg = gray2pcolor(bg,cool(256),'cdscale'); % use extrema
fg = gray2pcolor(fg,parula(256),[0 128],'cdscale'); % use values corresponding to mask threshold
% composite the images using the mask
% this is insensitve to class and depth
% mask can be binarized or graduated
%outpict = replacepixels(fg,bg,mask); % this is also MIMT
% you could do the composition without replacepixels()
% but it's clumsy, verbose, and poorly-generalized
% it's also sensitive to differences of class and depth
% this logical composition technique cannot support graduated masks
outpict = bg;
outpict(repmat(mask,[1 1 3])) = fg(repmat(mask,[1 1 3])); % mask must be logical-class
% save the output (or display it or whatever)


Más información sobre Lighting, Transparency, and Shading en Help Center y File Exchange.




Community Treasure Hunt

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

Start Hunting!

Translated by