Estimates the parameters of a line from a set of 2D points. More...
#include <FitLine.hpp>
Public Types | |
typedef super::Vector2T | Vector2T |
2D column vector with value type T. | |
typedef super::Vector3T | Vector3T |
3D column vector with value type T. | |
typedef super::VectorXT | VectorXT |
Column vector of arbitrary size with value type T. | |
typedef super::Matrix2T | Matrix2T |
2 x 2 matrix with value type T. | |
typedef super::MatrixXT | MatrixXT |
Matrix of arbitrary size with value type T. | |
enum | Error { DATA_POINTS_TOO_SIMILAR, INFINITY_NOT_AVAILABLE, NAN_NOT_AVAILABLE, NOT_ENOUGH_POINTS, UNKNOWN_ERROR, WRONG_POINT_SIZE } |
typedef Eigen::Array< T, Eigen::Dynamic, 1 > | ArrayXT |
General-purpose array of arbitrary size with value type T. | |
typedef Eigen::Matrix< T, 4, 1 > | Vector4T |
4D column vector with value type T. | |
typedef Eigen::Matrix< T, 3, 3 > | Matrix3T |
3 x 3 matrix with value type T. | |
Public Member Functions | |
FitLine () | |
Constructs an empty FitLine object. | |
FitLine (const std::vector< Coordinate< T > > &points) | |
Constructs a FitLine object. | |
FitLine (const std::vector< Vector2T, Eigen::aligned_allocator< Vector2T > > &points) | |
Constructs a FitLine object. | |
FitLine (const std::vector< Vector3T > &points) | |
Constructs an FitLine object. | |
virtual | ~FitLine () |
Destructs the FitLine object. | |
void | compute () |
Runs the fitting algorithm. | |
T | getRMS () const |
Returns the root mean square error. | |
unsigned | getNumberPointsRequired () const |
Returns the minimum number of data points required to compute a model. | |
const Coordinate< T > & | getDirectionVector () const |
Returns the direction vector of the line. | |
const Coordinate< T > & | getNormalVector () const |
Returns the normal vector of the line. | |
const Coordinate< T > & | getPointOnLineSegment () const |
Returns the center of the line segment. | |
T | getSlope () const |
Returns the slope of the line. | |
T | getYIntercept () const |
Returns y-intercept of the line. | |
T | getXIntercept () const |
Returns the x-intercept of the line. | |
T | getDistanceFromOrigin () const |
Returns the shortest distance from the line to the origin. | |
T | getDistanceTo (const Coordinate< T > &point) const |
Returns the shortest distance from the line to the given point. | |
void | setPoints (const std::vector< Coordinate< T > > &) |
Sets the point set. | |
void | setPoints (const std::vector< Vector2T, Eigen::aligned_allocator< Vector2T > > &) |
Sets the point set. | |
void | setPoints (const std::vector< Vector3T > &) |
Sets the point set. | |
Public Attributes | |
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef T | value_type |
Internally used value type (should be a floating point type). | |
Protected Attributes | |
MatrixXT | m_points |
Estimates the parameters of a line from a set of 2D points.
T | scalar type of the coordinates |
This class allows estimating the slope, the intercepts, the normal, et cetera of a line from a given set of points. The points may be erroneous since a least square fitting is done. If \( (x_i, y_i)^T \) are the data points, if \( m \) is the slope and if \( b \) is the y-intercept, then FitLine tries to minimize the sum of the squared residuals as shown below:
\[ (m, b) := \arg\min_{(m, b)} \sum_{i=1}^N (x_i m + b - y_i)^2 \]
Here is an more elaborate example to see how to use the class:
#include <iostream> #include <TRTK/FitLine.hpp> #include <TRTK/Tools.hpp> using namespace std; using namespace TRTK; int main() { vector<Coordinate<double> > points_on_line; // Construct some points lying on a line and add some noise. double slope = 0.7; double y_intercept = -3; for (int i = -10; i < 10; ++i) { using Tools::randn; double x = i + randn(0.0, 0.1); double y = i * slope + y_intercept + randn(0.0, 0.1); Coordinate<double> point(x, y); points_on_line.push_back(point); } // Estimate the line parameters. FitLine<double> fitLine(points_on_line); fitLine.compute(); cout << "Slope: " << fitLine.getSlope() << endl; cout << "Y-intercept: " << fitLine.getYIntercept() << endl; cout << "Direction Vector: " << fitLine.getDirectionVector() << endl; cout << "Distance from origin: " << fitLine.getDistanceFromOrigin() << endl; return 0; }
Output:
Slope: 0.695251 Y-intercept: -3.02231 Direction Vector: (-0.82106, -0.570842) Distance from origin: 2.4815
Definition at line 120 of file FitLine.hpp.
enum TRTK::Fit::Error [inherited] |
TRTK::FitLine< T >::FitLine | ( | ) |
Constructs an empty FitLine object.
T | scalar type of the coordinates |
Definition at line 181 of file FitLine.hpp.
TRTK::FitLine< T >::FitLine | ( | const std::vector< Coordinate< T > > & | points ) |
Constructs a FitLine object.
T | scalar type of the coordinates |
[in] | points | Points lying on the line. The points can be 2D or 3D homogeneous coordinates. |
Definition at line 202 of file FitLine.hpp.
TRTK::FitLine< T >::FitLine | ( | const std::vector< Vector2T, Eigen::aligned_allocator< Vector2T > > & | points ) |
Constructs a FitLine object.
T | scalar type of the coordinates |
[in] | points | Points lying on the line. |
Definition at line 223 of file FitLine.hpp.
TRTK::FitLine< T >::FitLine | ( | const std::vector< Vector3T > & | points ) |
Constructs an FitLine object.
T | scalar type of the coordinates |
[in] | points | Points lying on the line. The points are assumed to be homogeneous coordinates, whose third coordinate entry is equal to one. |
Definition at line 246 of file FitLine.hpp.
TRTK::FitLine< T >::~FitLine | ( | ) | [virtual] |
Destructs the FitLine object.
T | scalar type of the coordinates |
Definition at line 265 of file FitLine.hpp.
void TRTK::FitLine< T >::compute | ( | ) | [virtual] |
Runs the fitting algorithm.
T | scalar type of the coordinates |
There must be at least 2 known points on the line to perform the fitting.
ErrorObj | If there are not enough points to fit the circle, an error object is thrown and its error code is set to WRONG_POINT_SIZE . |
Implements TRTK::Fit2D< T >.
Definition at line 284 of file FitLine.hpp.
const Coordinate< T > & TRTK::FitLine< T >::getDirectionVector | ( | ) | const |
Returns the direction vector of the line.
T | scalar type of the coordinates |
Definition at line 510 of file FitLine.hpp.
T TRTK::FitLine< T >::getDistanceFromOrigin | ( | ) | const |
Returns the shortest distance from the line to the origin.
T | scalar type of the coordinates |
Definition at line 522 of file FitLine.hpp.
T TRTK::FitLine< T >::getDistanceTo | ( | const Coordinate< T > & | point ) | const [virtual] |
Returns the shortest distance from the line to the given point.
T | scalar type of the coordinates |
[in] | point | A 2D coordinate. |
Implements TRTK::Fit2D< T >.
Definition at line 542 of file FitLine.hpp.
const Coordinate< T > & TRTK::FitLine< T >::getNormalVector | ( | ) | const |
Returns the normal vector of the line.
T | scalar type of the coordinates |
Definition at line 569 of file FitLine.hpp.
const Coordinate< T > & TRTK::FitLine< T >::getPointOnLineSegment | ( | ) | const |
Returns the center of the line segment.
T | scalar type of the coordinates |
Definition at line 498 of file FitLine.hpp.
T TRTK::FitLine< T >::getRMS | ( | ) | const [virtual] |
Returns the root mean square error.
T | scalar type of the coordinates |
Implements TRTK::Fit2D< T >.
Definition at line 581 of file FitLine.hpp.
T TRTK::FitLine< T >::getSlope | ( | ) | const |
Returns the slope of the line.
T | scalar type of the coordinates |
numeric_limits<T>::infinity()
. Definition at line 620 of file FitLine.hpp.
T TRTK::FitLine< T >::getXIntercept | ( | ) | const |
Returns the x-intercept of the line.
T | scalar type of the coordinates |
numeric_limits<T>::quiet_NaN()
. Definition at line 635 of file FitLine.hpp.
T TRTK::FitLine< T >::getYIntercept | ( | ) | const |
Returns y-intercept of the line.
T | scalar type of the coordinates |
numeric_limits<T>::quiet_NaN()
. Definition at line 650 of file FitLine.hpp.
void TRTK::Fit2D< T >::setPoints | ( | const std::vector< Vector2T, Eigen::aligned_allocator< Vector2T > > & | points ) | [inherited] |
void TRTK::Fit2D< T >::setPoints | ( | const std::vector< Vector3T > & | points ) | [inherited] |
Sets the point set.
T | scalar type of the coordinates |
[in] | points | 3D homogeneous coordinates. |
void TRTK::Fit2D< T >::setPoints | ( | const std::vector< Coordinate< T > > & | points ) | [virtual, inherited] |
Sets the point set.
T | scalar type of the coordinates |
[in] | points | The points can either be plain 2D or 3D homogeneous coordinates. |
ErrorObj | If there are any coordinates other than 2D or 3D coordinates, an error object is thrown and its error code is set to WRONG_POINT_SIZE . |
Implements TRTK::Fit< T >.
Documentation generated by Doxygen