Overlay two images using transparency
68 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Image Analyst
2023-11-29
Possibly of interest to you is Steve's blog:
2 个评论
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
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 个评论
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
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')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Red 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!