Overlay two images using transparency

조회 수: 99 (최근 30일)
GPB
GPB 2023년 11월 28일
편집: DGM 2023년 12월 6일
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
figure();
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?
Thanks!

채택된 답변

Image Analyst
Image Analyst 2023년 11월 29일
  댓글 수: 2
GPB
GPB 2023년 12월 6일
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) ------------------------------------
axes(ha(1));
imshow(T1);
colormap(ha(1),gray);
% Masked image (Foreground) ------------------------------------
axes(ha(2));
imshow(FA);
colormap(ha(2),parula);
% Overlaped images -------------------------------
axes(ha(3));
T1 = ind2rgb(T1,gray);
FA = ind2rgb(FA,parula);
BG = imshow(T1); hold on; FG = imshow(FA);
set(FG,'AlphaData', idx);
Walter Roberson
Walter Roberson 2023년 12월 6일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2023년 11월 28일
figure();
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
GPB
GPB 2023년 11월 28일
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
axes(ha(1));
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
set(ax2,'color','none','visible','off')
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 2023년 11월 28일
any one axes can only have a single colormap. However you can ind2rgb to convert intensity information to rgb.

댓글을 달려면 로그인하십시오.


DGM
DGM 2023년 12월 6일
편집: DGM 2023년 12월 6일
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)
imwrite(outpict,'blah.png')
imshow(outpict,'border','tight')

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by