1
votes

I'm looking for help doing a (simple?) least squares line fit to a set of points in Matlab.

I have an image with a set of points that I'm trying to fit a line to, minimizing the distance from each point to the line (least squares fit). Seems to work fine with openCV/fitline, but we're doing our research on two platforms - the other being Matlab, and Matlab/polyfit doesn't do the same thing as opencv/fitline.

Per Fitting a line - MatLab disagrees with OpenCV, it seems that Polyfit minimizes the Y distance to the line, not the least squares (perpendicular) distance.

1
least squares regression is by definition minimising the y distance. If you want to minimize the perpendicular distance you try PCA. But what are you trying to do with the line in the end? You might find that the Matlab solution is actually what you're after.Dan
This might be helpful: cerebralmastication.com/2010/09/…Dan
@Dan, Nonlinear PCA, to be precise. :)divanov
@divanov I'm not so sure about that - can you link to a reference? I think just regular PCA will do exactly what the OP describes OpenCV to do...Dan
@Dan, polyfit fits data with a polynomial curve, so to get the same thing with PCA polynomial PCA has to be used.divanov

1 Answers

0
votes

You can write your own distance function and use lsqnonneg.