The script is to generate binary images with white particles on black background by reading partilce x,y,z coordinates and radius of each particle.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear
tic
specimen = '100F_';
fnum = 1;
intype = 'b.txt';
txtout = '.out';
imgout = '.png';
sf = 100.0; % adjust to modify image resolution
for oo = 1:fnum
F=xlsread('two_spheres.csv');
M(:,1) = F(:,1)*sf;
M(:,2) = F(:,2)*sf;
M(:,3) = F(:,3)*sf;
M(:,4) = F(:,4)*sf;
% adjust for particles that are beyond the left boundary
xmin = floor(min(M(:,1)-M(:,4)));
ymin = floor(min(M(:,2)-M(:,4)));
zmin = floor(min(M(:,3)-M(:,4)));
M(:,1) = M(:,1) - xmin;
M(:,2) = M(:,2) - ymin;
M(:,3) = M(:,3) - zmin;
[r c] = size(M);
Depth = round(max(M(:,1)+M(:,4)))+1;
Width = round(max(M(:,2)+M(:,4)))+1;
Height = round(max(M(:,3)+M(:,4)))+1;
I = zeros(Depth,Width,Height,'int8');
for jj = 1:r
rad = M(jj,4);
front = ceil(Depth - M(jj,1) + rad);
back = floor(Depth - M(jj,1) - rad)+1;
left = floor(M(jj,2) - rad)+1;
right = ceil(M(jj,2) + rad);
top = ceil(Height - M(jj,3) + rad);
bot = floor(Height - M(jj,3) - rad)+1;
for nn = back:front
for kk = left:right
for qq = bot:top
if ((kk - M(jj,2))^2+(nn-(Depth-M(jj,1)))^2+(qq-(Height-M(jj,3)))^2)<=rad^2
I(nn,kk,qq) = 1;
end
end
end
end
end
[RI CI DI] = size(I);
['File number ',num2str(oo),' has been processed']
end
numberOfSlices=numel(I(1,1,:));
for i=1:numberOfSlices
I1(:,:) = I(:,:,i);
I1=logical(I1);
imwrite(I1,strcat('data',num2str(i),'r.tif')) % Write to binary tif images
end
toc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The generated one slice binary image is shown as follow