I need to add color shapes to dose distribution images

9 views (last 30 days)
I'm trying to add colour shades to the following image Image 1 to get this type of a second image with color shades Image 2. I'm not sure which code to use for.

Answers (3)

KSSV
KSSV on 28 Feb 2022
Read about patch, area.

DGM
DGM on 28 Feb 2022
Edited: DGM on 7 Jul 2022
You can do this a couple different ways. One way would be to use a transparent contour object over an image. Getting alpha support on contour objects is a bit of a workaround, but it's possible:
% get an image and some placeholder data
A = imread('cameraman.tif');
[X Y] = meshgrid(linspace(-60,60,100));
Z = exp(-(hypot(X,Y)/40).^2); % some Z data (a gaussian)
% replicate the MxNx1 image to MxNx3
% this forces imagesc()/image()/imshow() to not use colormapping
% this way the contour() object is the only colormapped object in the axes
A = repmat(A,[1 1 3]);
% display the image and contour
% colocation is done by controlling the xdata/ydata for the image object
% and the x,y data associated with the contour
imagesc([-100 100],[-100 100],A); hold on
[~,hc] = contourf(X,Y,Z);
hc.LineStyle = 'none';
axis equal
colormap(parula(256))
% need to force the object to be initialized
% otherwise it may take a second or two before it exists
drawnow
filltriangles = hc.FacePrims;
% set the fill objects to transparent
for k = 1:length(filltriangles)
% Have to set this. The default is 'truecolor' which ignores alpha.
filltriangles(k).ColorType = 'truecoloralpha';
% The 4th element is the 'alpha' value. First 3 are RGB. Note, the
% values expected are in range 0-255.
filltriangles(k).ColorData(4) = 127;
end
Now you might not want the contour to include everything out to the square boundaries. Maybe you only want the interior levels to be shown. In that case, just figure out which levels those are and set the "background" levels to 0% opacity.
% if you want to make the contour background regions fully transparent
filltriangles(1).ColorData(4) = 0;
Alternatively, you could flip the stacking order and use a transparent image.
EDIT:
I just noticed you're using R2014b. I think this should still work, but 14b is the first release where it theoretically would, and I don't think this is a documented technique, so ymmv.

yanqi liu
yanqi liu on 1 Mar 2022
yes,sir,may be use label2rgb and set AlphaData to view,such as
center1 = -10;
center2 = -center1;
dist = sqrt(2*(2*center1)^2);
radius = dist/2 * 1.4;
lims = [floor(center1-1.2*radius) ceil(center2+1.2*radius)];
[x,y] = meshgrid(lims(1):lims(2));
bw1 = sqrt((x-center1).^2 + (y-center1).^2) <= radius;
bw2 = sqrt((x-center2).^2 + (y-center2).^2) <= radius;
bw = bw1 | bw2;
figure
imshow(bw,'InitialMagnification','fit'), title('bw')
D = bwdist(~bw);
D = -D;
D(~bw) = Inf;
L = watershed(D);
L(~bw) = 0;
rgb = label2rgb(L,'jet',[.5 .5 .5]);
figure
imshow(bw,'InitialMagnification','fit')
hold on;
h=imshow(rgb,'InitialMagnification','fit');
set(h, 'AlphaData',0.5);

Products


Release

R2014b

Community Treasure Hunt

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

Start Hunting!