How do I make 3D sphere for convolution?

Hi all,
I want to make a 3D sphere where the x, y and z coordinates are being solved in a 3D array, so I can convolute this with a kernel I have. But, I don't seem to both save the coordinates and get the shape right at the same time. I have the following code:
figure;
half=50;
for k=1:100
for j=1:100
for i=1:100
mesh_sphere(i,j,k)=((i-half).^2)+((j-half).^2)+((k-half).^2);
end
end
surf(mesh_sphere(:,:,k));
hold on
end
hold off
After this, I also want to use other shapes that I can test my kernel on, such as a hollow sphere and an ellipsoid. How can I make those and also same them in a 3D array? Thank you in advance!

 Réponse acceptée

Rik
Rik le 12 Jan 2022
If you want a sphere in a 3D array, you should probably do something like this:
array_width=21;%must be odd
radius=5;%must be smaller than half the array width for the sphere to fit
a=(array_width-1)/2;
a=linspace(-a,a,array_width);
[X,Y,Z]=meshgrid(a);
solid_sphere= sqrt(X.^2 + Y.^2 + Z.^2) <= radius;
For a shell you can repeat this with two radii and use the or operator |. For an elipsoid you need you can scale the coordinate values in each direction according to the partial radius you want.

9 commentaires

Lieke Pullen
Lieke Pullen le 12 Jan 2022
Modifié(e) : Lieke Pullen le 12 Jan 2022
Hi Rik,
Thanks for your answer, it works fine! If I want two radii, with a hollow corem where should I put the | operator?
Since the sphere variable is logical: between the two generated spheres.
Note that the difference between the two radii should be large enough, otherwise your shell will disappear.
Also, it shouldn't actually be or, it should be xor.
array_width=21;%must be odd
small_solid_sphere=create_solid_sphere(4,array_width);
big_solid_sphere=create_solid_sphere(5,array_width);
hollow1= xor(small_solid_sphere,big_solid_sphere);
small_solid_sphere=create_solid_sphere(4.9,array_width);
big_solid_sphere=create_solid_sphere(5,array_width);
hollow2= xor(small_solid_sphere,big_solid_sphere);
subplot(1,2,1),imshow(hollow1(:,:,11))
subplot(1,2,2),imshow(hollow2(:,:,11))
function solid_sphere=create_solid_sphere(radius,array_width)
a=(array_width-1)/2;
a=linspace(-a,a,array_width);
[X,Y,Z]=meshgrid(a);
solid_sphere= sqrt(X.^2 + Y.^2 + Z.^2) <= radius;
end
Lieke Pullen
Lieke Pullen le 13 Jan 2022
Hi Rik,
Thank you for your elaboration. I am now wondering, how do I make the space between the shell of the larger sphere and smaller sphere solid? I want that to convolve the sphere minus the smaller sphere, and now, these two are the same.
Rik
Rik le 13 Jan 2022
I don't really understand what you mean. Isn't this equivalent to having radii of 20 and 5 for the big and small spheres?
Lieke Pullen
Lieke Pullen le 13 Jan 2022
Okay, I will try to explain more thoroughly:
I have a kernel that I want to convolve with certain shapes to see what it does to the shape. I want to do this with a solid sphere, and with a sphere which has a hollow core of a certain radius (like a donut, but then in 3D). What I am wondering: with the method you proposed, does this mean that the radii between the shell of the smaller sphere and the shell of the bigger sphere are also ''filled'', so that these values are 1, and the values within the inner shell are zero?
The shell is a kind of hack. It takes two spheres of different radii and looks where only one of them exists. That sounds exactly what you want. You don't want two shells and fill what is in between them, you want a single sphere with a thick shell. So just subtract the sphere with a small radius from the sphere with the large radius and you have the shape you describe.
array_width=101;%must be odd
small_solid_sphere=create_solid_sphere(25,array_width);
big_solid_sphere =create_solid_sphere(5 ,array_width);
hollow1= xor(small_solid_sphere,big_solid_sphere);
imshow(hollow1(:,:,(end-1)/2))
function solid_sphere=create_solid_sphere(radius,array_width)
a=(array_width-1)/2;
a=linspace(-a,a,array_width);
[X,Y,Z]=meshgrid(a);
solid_sphere= sqrt(X.^2 + Y.^2 + Z.^2) <= radius;
end
Lieke Pullen
Lieke Pullen le 13 Jan 2022
Ah, I get it know! I think I misunderstood what the xor-function did. Thank you!
Image Analyst
Image Analyst le 13 Jan 2022
Modifié(e) : Image Analyst le 13 Jan 2022
@Lieke Pullen like I showed in my answer below, the strel() function creates the sphere in one line of code. The convolution is also done in one line of code.
Lieke Pullen
Lieke Pullen le 13 Jan 2022
@Image Analyst I know, and I very much appreciate the effort. But I wanted to manually also change some of the proposed parameters, and this what this does to the shape. For me, Rik's option worked best.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 12 Jan 2022
Modifié(e) : Image Analyst le 13 Jan 2022
SE = strel('sphere',r) % creates a 3-D spherical structuring element whose radius is r pixels.
out = convn(inputImage, SE.Neighborhood);

3 commentaires

Possibly you meant,
out = convn(inputImage, SE.Neighborhood);
Rik
Rik le 12 Jan 2022
I don't remember this object notation. Admittedly, I haven't used this function in years, but I will look up this function in the release notes to see whether I forgot, or if they changed it.
Image Analyst
Image Analyst le 12 Jan 2022
Thanks Matt for the correction.

Connectez-vous pour commenter.

Produits

Version

R2020a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by