Hi all,
I have a (239,239,85) binary matrix in which the 1's correspond to the mask of a geometrical shape (see attachment). I want to fit a sphere around it, so that it fits best to the binary mask. I have already tried this: https://nl.mathworks.com/matlabcentral/fileexchange/34129-sphere-fit-least-squared, but that does not work since the matrix is singular. Can anybody help me out? Thanks in advance!

댓글 수: 1

Walter Roberson
Walter Roberson 2022년 2월 18일
If the matrix is singular then enter the data all lies in the same plane or else you are passing the wrong information to the fitting function.

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

 채택된 답변

KSSV
KSSV 2022년 2월 18일

0 개 추천

load('matrixfit.mat')
[m,n,p] = size(tumor3) ;
[X,Y,Z] = ndgrid(1:m,1:n,1:p) ;
k = tumor3(:) ;
x = X(:) ;
y = Y(:) ;
z = Z(:) ;
P = [x(k==1) y(k==1) z(k==1)] ;
[Center,Radius] = sphereFit(P) ; % Use that file exchange function
I got:
Center = [100.8695 103.3629 37.2319] ;
Radius = 3.0785 ;

댓글 수: 5

Lieke Pullen
Lieke Pullen 2022년 2월 18일
Thank you! This is exactly what I was looking for.
Walter Roberson
Walter Roberson 2022년 2월 18일
Are X and Y reversed here? Remember that X is the second index of an array and Y is the first index of an array.
Lieke Pullen
Lieke Pullen 2022년 2월 21일
편집: Lieke Pullen 2022년 2월 21일
I have a follow-up question on plotting the sphere. I have extracted the radius and center via your script, but when I want to draw the sphere, it does not give me the right results, since the center is not at the center of the coordinates given. The script is given below
array_x=239; array_y=239; array_z=85;
sphere=create_sphere(round(Radius),array_x,array_y, array_z, round(Center));
function solid_sphere=create_sphere(radius,array_x,array_y, array_z,Center)
ax=(array_x-1)/2; ay=(array_y-1)/2; az=(array_z-1)/2;
axlin=linspace(-ax,ax,array_x); aylin=linspace(-ay,ay,array_y); azlin=linspace(-az,az,array_z);
[X,Y,Z]=meshgrid(axlin, aylin, azlin);
solid_sphere= sqrt((X+(0.5*Center(1))).^2 + (Y+(0.5*Center(2))).^2 + (Z+(0.5*Center(3))).^2) <= radius ;
end
Can anybody help me out? Thanks in advance!
Torsten
Torsten 2022년 2월 21일
Usually,
solid_sphere= sqrt((X-Center(1)).^2 + (Y-Center(2)).^2 + (Z-Center(3)).^2) <= radius ;
Is it different in your case ?
I think so, since I have made a linspace varying from -119 to -119 (for x and y), and from -43 to 43 for z. I used the equation you mentioned in the following script:
array_x=239; array_y=239; array_z=85;
sphere=create_sphere(round(Radius),array_x,array_y, array_z, round(Center));
function solid_sphere=create_sphere(radius,array_x,array_y, array_z,Center)
[X,Y,Z]=meshgrid(0:1:array_x-1, 0:1:array_y-1, 0:1:array_z-1);
solid_sphere= sqrt((X-madCenter(1)).^2 + (Y-Center(2)).^2 + (Z-Center(3)).^2) <= radius ;
end
But that also does not give me the right center unfortunately.

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

추가 답변 (0개)

카테고리

제품

릴리스

R2021b

질문:

2022년 2월 18일

댓글:

2022년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by