Skip to content

fitLine returns wrong line for nearly-horizontal points #15083

@GPhilo

Description

@GPhilo
System information (version)
  • OpenCV => 3.4.3
  • Operating System / Platform => Windows 10 1903
  • Compiler => MSVC 1916
Detailed description

Fitting a line passing through almost-horizontal points via cv::fitLine results in a wrong line parameters vector.

Steps to reproduce
#include <vector>
#include <iostream>
#include <opencv2/imgproc.hpp>

int main()
{
    std::vector<cv::Point> points = { 
        {432,654},
        {370,656},
        {390,656},
        {410,656},
        {348,658},
    };

    cv::Vec4f lineParam;
    cv::fitLine(points, lineParam, cv::DIST_L1, 0, 0.01, 0.01);
    std::cout << lineParam << std::endl;
}

Running the code above prints [0.701592, -0.712579, 390, 656]. The first two values, vx and vy are for a unit vector pointing approximately 45° down from the 0, while the expected result would have it closer to 0 (the points are almost perfectly aligned horizontally).
Values for reps and aeps are taken from https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#fitline


Update:

If I use cv::DIST_L2 instead of cv::DIST_L1 for the distance function, the line is correctly estimated:

[0.999247, -0.038801, 390, 656]

It would seem, then that the issue is related to using the L1 distance.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions