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:
get endpoints of line after least squares fitting

Subject: get endpoints of line after least squares fitting

From: Aidy

Date: 24 Jan, 2011 09:49:03

Message: 1 of 7

hi guys,

I have some points on an image in pixels (u,v). I fit a line through it using least squares fit.
But having trouble in determining the starting and endpoints of the line.

can you guys suggest any possible way I should go about it?

thanks a lot
-- aiden

Subject: get endpoints of line after least squares fitting

From: Aidy

Date: 24 Jan, 2011 09:54:04

Message: 2 of 7

I forgot to include my code so far :


%------------------------------------------------------------------
function [a0 a1] =Linear_Regression(x,y)
    
n = length(x);

a1 = (n*sum(x.*y) - sum(x)*sum(y)) /(n*sum(x.^2)-(sum(x))^2);

a0 = mean(y) -a1*mean(x);

end
%------------------------------------------------------------------


%-------------------------------------------------------------------
clc;
clear all;
close all;

x = [107.35 111.51 117 111.29]
y = [107 146 154.61 114]

[a0 a1] =Linear_Regression(x,y)

x_model = min(x):max(x)
y_model = a0 + a1.*x_model;

plot(x,y, 'x' , x_model,y_model)
%-----------------------------------------------------------------------

Subject: get endpoints of line after least squares fitting

From: John D'Errico

Date: 24 Jan, 2011 10:51:04

Message: 3 of 7

"Aidy" wrote in message <ihji7s$qi9$1@fred.mathworks.com>...
> I forgot to include my code so far :
>
>
> %------------------------------------------------------------------
> function [a0 a1] =Linear_Regression(x,y)
>
> n = length(x);
>
> a1 = (n*sum(x.*y) - sum(x)*sum(y)) /(n*sum(x.^2)-(sum(x))^2);
>
> a0 = mean(y) -a1*mean(x);
>
> end

I'm sorry. You wrote the WRONG formula here.

a1a0 = polyfit(x,y,1);
a1 = a1a0(1);
a0 = a1a0(2);

Learn to use the tools in matlab. Why? because the
expression that you wrote has numerical problems.
It might even survive use on image based data. But
sometime in the future, you will get burnt for using
that piece of numerical crap.

If you insist on NOT using polyfit, then learn to do
it properly. This line of code is a proper least squares
fit to your line:

a1a0 = [x(:),ones(length(x),1)]\y(:);

John

Subject: get endpoints of line after least squares fitting

From: John D'Errico

Date: 24 Jan, 2011 11:03:03

Message: 4 of 7

"Aidy" wrote in message <ihjhuf$86m$1@fred.mathworks.com>...
> hi guys,
>
> I have some points on an image in pixels (u,v). I fit a line through it using least squares fit.
> But having trouble in determining the starting and endpoints of the line.
>
> can you guys suggest any possible way I should go about it?

What do you mean by the starting and end points
of a line. A line is infinitely long. Last time I checked,
that meant it had no start or end.

Perhaps you meant the first and last points of your
data set? Surely you can find them. If the data is sorted
in increasing order, then the start point might be x(1),
the end point might be x(end). Or if not sorted, then
would not min(x) and max(x) suffice?

Or if you mean something else, perhaps you might
explain yourself better? Perhaps you intend to find
where the line crosses the boundaries of the image
domain. You might have said that if that was what
you wanted. If so, given the equation of the line, and
the known size and shape of the image box, surely this
is a simple computation?

1. Where does the line cross the lower bound of the box
in x, i.e., the left edge in x? Can you evaluate the value
of that line function at that point?

2. Where does it cross the right side of the box? Can you
evaluate the line at that point in x?

3. Now, given the expression for y as a function of x and
a0, a1, can you find the value of x such that y attains the
known lower and upper bounds of the box?

Given those 4 points, write some logic that chooses from
them to identify where the line crosses the box. Again,
trivial once you sit down and start writing, but I won't do
it for you.

John

Subject: get endpoints of line after least squares fitting

From: Aidy

Date: 24 Jan, 2011 18:59:05

Message: 5 of 7

Yes, I meant starting and end points of dataset. Im not asking anyone to do it....just asking for some advice.

My issue is that i will have horizontal and vertical lines.

For example, a vertical line will have same y coordinate , and horizontal, same x coordinate. I not sure if using min(x), max(x) would suffice.


--aiden

Subject: get endpoints of line after least squares fitting

From: Bruno Luong

Date: 24 Jan, 2011 19:41:04

Message: 6 of 7

"Aidy" wrote in message <ihki5p$88i$1@fred.mathworks.com>...
> Yes, I meant starting and end points of dataset. Im not asking anyone to do it....just asking for some advice.
>
> My issue is that i will have horizontal and vertical lines.
>
> For example, a vertical line will have same y coordinate , and horizontal, same x coordinate. I not sure if using min(x), max(x) would suffice.

How do you perform the least-squares fitting? Just to be sure whereas you have a method symmetric to u and v or not.

Bruno

Subject: get endpoints of line after least squares fitting

From: John D'Errico

Date: 24 Jan, 2011 19:45:04

Message: 7 of 7

"Aidy" wrote in message <ihki5p$88i$1@fred.mathworks.com>...
> Yes, I meant starting and end points of dataset. Im not asking anyone to do it....just asking for some advice.
>
> My issue is that i will have horizontal and vertical lines.
>
> For example, a vertical line will have same y coordinate , and horizontal, same x coordinate. I not sure if using min(x), max(x) would suffice.
>

You need to appreciate that a perfectly vertical line will
fail when you try the regression.

Infinite slopes tend to cause problems.

If this is an issue, then perhaps you need to use an
orthogonal regression line. You will also need to learn
to work with a line in a general parametric point-slope
form.

     P(t) = P0 + P1*t

where P0 and P1 are vectors of length 2.

John

Tags for this Thread

No tags are associated with 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