From: Joseph on
Hi, all,

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.

The code is pasted below:

function SphereBW=XYZD2Sphere(BW,XYZD)

SphereBW=false(size(BW,1),size(BW,2),size(BW,3));

cx=XYZD(1);cy=XYZD(2);cz=XYZD(3);diameter=XYZD(4);

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

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.

Many thanks!!!

Joseph
From: Nic Roberts on
Hi Joseph

It isnt imediatly apparent from your code what it is you are trying to achieve. When I run your code I just get a massive array of zeros. Is your intent to remove a sphere of data from BW?

Nic

"Joseph " <wg_smile4ever(a)hotmail.com> wrote in message <i2bil4$aqh$1(a)fred.mathworks.com>...
> Hi, all,
>
> 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.
>
> The code is pasted below:
>
> function SphereBW=XYZD2Sphere(BW,XYZD)
>
> SphereBW=false(size(BW,1),size(BW,2),size(BW,3));
>
> cx=XYZD(1);cy=XYZD(2);cz=XYZD(3);diameter=XYZD(4);
>
> 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
>
> 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.
>
> Many thanks!!!
>
> Joseph
From: Joseph on
Hi, Nic,

Thanks for your help.

SphereArray=XYZD2Sphere(BW,XYZD);

I want to generate a binary point cloud of a sphere in an array as same size as the input BW. The XYZD is a 4-element vector. The first 3 elements are the XYZ coordinates of the sphere's center point and the 4th element is the diameter of the sphere.

For example:
SphereArray=XYZD2Sphere(false(128,128,128),[50 60 70 30]);
patch(isosurface(SphereArray,0));

will get an output SphereArray a 128-128-128 binary array with a 30-cm diameter sphere (trues) located around center point [50 60 70].

But now the sphere centers at [60 50 70]. I guess the x and y axis swap. I cannot find the errors from the code.

"Nic Roberts" <dingtheking(a)googlemail.com> wrote in message <i2bpem$9ug$1(a)fred.mathworks.com>...
> Hi Joseph
>
> It isnt imediatly apparent from your code what it is you are trying to achieve. When I run your code I just get a massive array of zeros. Is your intent to remove a sphere of data from BW?
>
> Nic
>
> "Joseph " <wg_smile4ever(a)hotmail.com> wrote in message <i2bil4$aqh$1(a)fred.mathworks.com>...
> > Hi, all,
> >
> > 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.
> >
> > The code is pasted below:
> >
> > function SphereBW=XYZD2Sphere(BW,XYZD)
> >
> > SphereBW=false(size(BW,1),size(BW,2),size(BW,3));
> >
> > cx=XYZD(1);cy=XYZD(2);cz=XYZD(3);diameter=XYZD(4);
> >
> > 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
> >
> > 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.
> >
> > Many thanks!!!
> >
> > Joseph