I am new to Matlab and using it for image processing. I am writing a function to generate a point cloud of a sphere on a 3D image the size of which is same as the input image BW. Another input XYZD is a 4-element vector containing the X,Y and Z positions of the center point and the diameter of the sphere. The output of the function SphereBW is the new 3D image with the sphere point cloud.
for x=1:size(BW,1) for y=1:size(BW,2) for z= 1:size(BW,3) if (x-cx)^2+(y-cy)^2+(z-cz)^2<(diameter/2)^2 SphereBW(x,y,z)=1; end end end end
But the sphere I generate is put to a wrong location in the new image where it seems that the X and Y coordinates are swapped. e.g. if my input BW size is [512 512 100] and XYZD vector is [250 150 50 30] the center point of the sphere locates at [150 250 50] in the output image SphereBW.
Please help me to figure out what is wrong with my function. If you know how to write any other elegant codes (without for loops), please teach me as well.