Overlay two images using transparency
73 ビュー (過去 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 で Orange についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!