Skip to content

fitLine

geometryfunctionOpenCV 5.0.0
import { fitLine } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

ARGUMENTSpoints, line, distType, param
FUNCTIONfitLine
RETURN TYPEvoid
Call structure. A void return can still write to destination arguments. The parameter descriptions define inputs, outputs and ownership.

Fits a line to a 2D or 3D point set.

The function fitLine fits a line to a 2D or 3D point set by minimizing \sum_i \rho(r_i) where r_i is a distance between the i^{th} point, the line and \rho(r) is a distance function, one of the following:

  • DIST_L2
\rho (r) = r^2/2  \quad \text{(the simplest and the fastest least-squares method)}
  • DIST_L1
\rho (r) = r
  • DIST_L12
\rho (r) = 2  \cdot ( \sqrt{1 + \frac{r^2}{2}} - 1)
  • DIST_FAIR
\rho \left (r \right ) = C^2  \cdot \left (  \frac{r}{C} -  \log{\left(1 + \frac{r}{C}\right)} \right )  \quad \text{where} \quad C=1.3998
  • DIST_WELSCH
\rho \left (r \right ) =  \frac{C^2}{2} \cdot \left ( 1 -  \exp{\left(-\left(\frac{r}{C}\right)^2\right)} \right )  \quad \text{where} \quad C=2.9846
  • DIST_HUBER
\rho (r) =  \fork{r^2/2}{if \(r < C\)}{C \cdot (r-C/2)}{otherwise} \quad \text{where} \quad C=1.345

The algorithm is based on the M-estimator ( https://en.wikipedia.org/wiki/M-estimator ) technique that iteratively fits the line using the weighted least-squares algorithm. After each iteration the weights w_i are adjusted to be inversely proportional to \rho(r_i) .

fitLine(points: Mat, line: Mat, distType: number, param: number, reps: number, aeps: number): void;
points

Input vector of 2D or 3D points, stored in std::vector<> or Mat.

line

Output destination, filled by the native operation. Output line parameters. In case of 2D fitting, it should be a vector of 4 elements (like Vec4f) - (vx, vy, x0, y0), where (vx, vy) is a normalized vector collinear to the line and (x0, y0) is a point on the line. In case of 3D fitting, it should be a vector of 6 elements (like Vec6f) - (vx, vy, vz, x0, y0, z0), where (vx, vy, vz) is a normalized vector collinear to the line and (x0, y0, z0) is a point on the line.

distType

Distance used by the M-estimator, see #DistanceTypes

param

Numerical parameter ( C ) for some types of distances. If it is 0, an optimal value is chosen.

reps

Sufficient accuracy for the radius (distance between the coordinate origin and the line).

aeps

Sufficient accuracy for the angle. 0.01 would be a good default value for reps and aeps.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.