How do I make 3D sphere for convolution?

조회 수: 22 (최근 30일)
Lieke Pullen
Lieke Pullen 2022년 1월 12일
댓글: Lieke Pullen 2022년 1월 13일
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!

채택된 답변

Rik
Rik 2022년 1월 12일
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
Image Analyst
Image Analyst 2022년 1월 13일
편집: Image Analyst 2022년 1월 13일
@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 2022년 1월 13일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 1월 12일
편집: Image Analyst 2022년 1월 13일
SE = strel('sphere',r) % creates a 3-D spherical structuring element whose radius is r pixels.
out = convn(inputImage, SE.Neighborhood);
  댓글 수: 3
Rik
Rik 2022년 1월 12일
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 2022년 1월 12일
Thanks Matt for the correction.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by