1

I would like to find the line equation for the best approximation of a line passing as close as possible to 4 points which are almost forming a line.

The points are the left and right corners of each eye in a facial recognition software, so they are almost in line from a visual perspective, but they are not, obviously, on a line.

What I am looking for is the best approximation and the line does NOT need to pass through any of the points.

One idea I had was to make a line equation for each eye, using the corners, but this doesn't work since eyes may be slanted in random ways and also lighting dramatically affects the precision on the eye corners.

(bonus if you have a python solution :D)

Thomas
  • 127

3 Answers3

1

For minimizing actual distances to the line (rather than the vertical distances in Argerami's answer) the method of finding a best fit is known as total least squares: https://en.wikipedia.org/wiki/Total_least_squares.

asd
  • 873
0

You want your line to be $y=ax+b$. One very common measure of "close" is by minimizing the squares of the vertical distances. That is, if your points are $(x_1,y_1),\ldots,(x_n,y_n)$, you want to minimize $$ f(a,b)=\sum_{j=1}^n (y_j-(ax_j+b))^2. $$ If you differentiate and equate the partial derivatives to zero, you get the linear system $$ a\sum_{j=1}^nx_j^2+b\sum_{j=1}^nx_j=\sum_{j=1}^nx_jy_j, $$ $$ a\sum_{j=1}^nx_j+nb=\sum_{j=1}^ny_j. $$ Solving, you get $$ a=\frac{\displaystyle n\sum_{j=1}^nx_jy_j-\sum_{j=1}^nx_j\sum_{j=1}^ny_j}{\displaystyle n\sum_{j=1}^nx_j^2-\left(\sum_{j=1}^nx_j\right)^2}, $$ $$ b=\frac{\displaystyle \sum_{j=1}^nx_j^2\sum_{j=1}^ny_j-\sum_{j=1}^nx_j\sum_{j=1}^nx_jy_j}{\displaystyle n\sum_{j=1}^nx_j^2-\left(\sum_{j=1}^nx_j\right)^2}, $$

Martin Argerami
  • 205,756
0

In my answer here (linear least squares minimizing distance from points to rays - is it possible?) I give the method I worked out over 30 years ago for finding the line which minimized the sum of the squares of the distances to a set of points.

marty cohen
  • 107,799