Overlay two images using transparency
조회 수: 48 (최근 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!