Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTutoMeanSquareFitting.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 */
30
31#include "vpTutoMeanSquareFitting.h"
32
33#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
34#ifndef DOXYGEN_SHOULD_SKIP_THIS
35namespace tutorial
36{
37#ifdef ENABLE_VISP_NAMESPACE
38using namespace VISP_NAMESPACE_NAME;
39#endif
40
41vpTutoMeanSquareFitting::vpTutoMeanSquareFitting(const unsigned int &degree, const unsigned int &height, const unsigned int &width)
42 : m_degree(degree)
43 , m_height(static_cast<unsigned int>(height))
44 , m_width(static_cast<unsigned int>(width))
45 , m_model(degree, height, width)
46 , m_isFitted(false)
47{ }
48
50void vpTutoMeanSquareFitting::fit(const std::vector<vpImagePoint> &pts)
51{
52 vpMatrix A; // The matrix that contains the u^i
53 vpMatrix X; // The matrix we want to estimate, that contains the polynomial coefficients.
54 vpMatrix b; // The matrix that contains the v values
55
56 // Fill the matrices that form the system we want to solve
57 vpTutoParabolaModel::fillSystem(m_degree, m_height, m_width, pts, A, b);
58
59 // Compute the parabola coefficients using the least-mean-square method.
60 X = A.pseudoInverse() * b;
61 m_model = vpTutoParabolaModel(X, m_height, m_width);
62 m_isFitted = true;
63}
65
66double vpTutoMeanSquareFitting::evaluate(const std::vector<vpImagePoint> &pts)
67{
68 if (!m_isFitted) {
69 throw(vpException(vpException::notInitialized, "fit() has not been called."));
70 }
71 unsigned int nbPts = static_cast<unsigned int>(pts.size());
72
73 // Compute the mean absolute error
74 double meanSquareError = 0.f;
75 for (unsigned int i = 0; i < nbPts; ++i) {
76 double squareError = evaluate(pts[i]);
77 meanSquareError += squareError;
78 }
79 meanSquareError /= static_cast<double>(nbPts);
80 return std::sqrt(meanSquareError);
81}
82
83double vpTutoMeanSquareFitting::evaluate(const vpImagePoint &pt)
84{
85 if (!m_isFitted) {
86 throw(vpException(vpException::notInitialized, "fit() has not been called."));
87 }
88 double u = pt.get_u();
89 double v = pt.get_v();
90 double v_model = model(u);
91 double error = v - v_model;
92 double squareError = error * error;
93 return squareError;
94}
95
96double vpTutoMeanSquareFitting::model(const double &u)
97{
98 if (!m_isFitted) {
99 throw(vpException(vpException::notInitialized, "fit() has not been called."));
100 }
101 double v = m_model.eval(u);
102 return v;
103}
104}
105#endif
106#else
107void dummy_vpTutoMeanSquareFitting() { }
108#endif
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition vpException.h:74
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
double get_u() const
double get_v() const
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
vpMatrix pseudoInverse(double svThreshold=1e-6) const