Hello Matt
I have tried your code and it works well.
But I don't understand fully about the sentence direction=cross...
Why the direction should be like that. I have restudied corresponding algebra knowledge, the direction vector should be the singular vector of centeredLine, isn't it?
Your last two sentences jumps too quickly for me. Thank you.
"Matt J" wrote in message <ijgp4s$r8u$1@fred.mathworks.com>...
> "Toni " <annihilaatio@yahoo.com> wrote in message <ijg4b8$1f5$1@fred.mathworks.com>...
> >
> > Hello all,
> >
> > how would it be possible or the most easiest way to fit a line in 3D
> > image, e.g. (100x100x100)? Is lsqcurvefit the one to use here and
> > if so, how would i actually define the objective function?
>
> That seems like overkill. You need to extract the coordinates of sample points on the line from the image. Then, you can use an SVD approach like the following:
>
>
> %Fake data for line passing through [1,1,1] in direction [1,2,3]
> lineData=bsxfun(@plus, [1,1,1], (1:.01:1).'*[1,2,3]);
> Noise=rand(size(lineData))*.01;
> lineData=lineData+Noise;
>
>
>
> xyz0=mean(lineData,1),
>
> centeredLine=bsxfun(@minus,lineData,xyz0);
>
> [U,S,V]=svd(centeredLine);
>
> direction=cross(V(:,end),V(:,end1));
> direction=direction./direction(1),
>
>
> xyz0 =
>
> 1.0048 1.0048 1.0052
>
>
> direction =
>
> 1.0000
> 2.0019
> 3.0023
