f



get endpoints of line after least squares fitting

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
0
Aidy
1/24/2011 9:49:03 AM
comp.soft-sys.matlab 211266 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

6 Replies
492 Views

Similar Articles

[PageSpeed] 33

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)
%-----------------------------------------------------------------------
0
aidenjobe (38)
1/24/2011 9:54:04 AM
"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
0
woodchips (7944)
1/24/2011 10:51:04 AM
"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
0
woodchips (7944)
1/24/2011 11:03:03 AM
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
0
Aidy
1/24/2011 6:59:05 PM
"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
0
Bruno
1/24/2011 7:41:04 PM
"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
0
John
1/24/2011 7:45:04 PM
Reply: