Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
3D line fitting

Subject: 3D line fitting

From: Toni

Date: 16 Feb, 2011 09:11:04

Message: 1 of 4


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?

Thanks in advance,

toni

Subject: 3D line fitting

From: Matt J

Date: 16 Feb, 2011 15:06:04

Message: 2 of 4

"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(:,end-1));
direction=direction./direction(1),


xyz0 =

    1.0048 1.0048 1.0052


direction =

    1.0000
    2.0019
    3.0023

Subject: 3D line fitting

From: Xu

Date: 18 Feb, 2013 18:27:06

Message: 3 of 4

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 re-studied 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(:,end-1));
> direction=direction./direction(1),
>
>
> xyz0 =
>
> 1.0048 1.0048 1.0052
>
>
> direction =
>
> 1.0000
> 2.0019
> 3.0023

Subject: 3D line fitting

From: Matt J

Date: 4 Mar, 2013 21:30:08

Message: 4 of 4

"Xu " <Xiao@dundee.ac.uk> wrote in message <kftrpq$8bs$1@newscl01ah.mathworks.com>...
> 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 re-studied 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.
==============

Hi Xu,

Looking back at this after 2 years, I think you could probably have just set

 direction=V(:,1)

which would be the singular vector corresponding to the maximum singular value.
I assume this is what you mean by "the singular vector of centeredLine".

The smallest-valued singular vectors V(:end) and V(end-1,:) are approximately perpendicular to centeredLine. In other words, they are basis vectors for a plane approximately perpendicular to the line. Taking their cross product gives the plane's normal, and should be parallel to centerdLine.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us