Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
linlsq.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: linlsq.h (Formerly llsq.h)
3  * Description: Linear Least squares fitting code.
4  * Author: Ray Smith
5  * Created: Thu Sep 12 08:44:51 BST 1991
6  *
7  * (C) Copyright 1991, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #ifndef TESSERACT_CCSTRUCT_LINLSQ_H_
21 #define TESSERACT_CCSTRUCT_LINLSQ_H_
22 
23 #include "points.h"
24 #include "params.h"
25 
26 class LLSQ {
27  public:
28  LLSQ() { // constructor
29  clear(); // set to zeros
30  }
31  void clear(); // initialize
32 
33  // Adds an element with a weight of 1.
34  void add(double x, double y);
35  // Adds an element with a specified weight.
36  void add(double x, double y, double weight);
37  // Adds a whole LLSQ.
38  void add(const LLSQ& other);
39  // Deletes an element with a weight of 1.
40  void remove(double x, double y);
41  inT32 count() const { // no of elements
42  return static_cast<int>(total_weight + 0.5);
43  }
44 
45  double m() const; // get gradient
46  double c(double m) const; // get constant
47  double rms(double m, double c) const; // get error
48  double pearson() const; // get correlation coefficient.
49 
50  // Returns the x,y means as an FCOORD.
51  FCOORD mean_point() const;
52  // Returns the direction of the fitted line as a unit vector, using the
53  // least mean squared perpendicular distance. The line runs through the
54  // mean_point, i.e. a point p on the line is given by:
55  // p = mean_point() + lambda * vector_fit() for some real number lambda.
56  // Note that the result (0<=x<=1, -1<=y<=-1) is directionally ambiguous
57  // and may be negated without changing its meaning.
58  FCOORD vector_fit() const;
59  // Returns the covariance.
60  double covariance() const {
61  if (total_weight > 0.0)
62  return (sigxy - sigx * sigy / total_weight) / total_weight;
63  else
64  return 0.0;
65  }
66  double x_variance() const {
67  if (total_weight > 0.0)
68  return (sigxx - sigx * sigx / total_weight) / total_weight;
69  else
70  return 0.0;
71  }
72  double y_variance() const {
73  if (total_weight > 0.0)
74  return (sigyy - sigy * sigy / total_weight) / total_weight;
75  else
76  return 0.0;
77  }
78 
79  private:
80  double total_weight; // no of elements or sum of weights.
81  double sigx; // sum of x
82  double sigy; // sum of y
83  double sigxx; // sum x squared
84  double sigxy; // sum of xy
85  double sigyy; // sum y squared
86 };
87 
88 #endif // TESSERACT_CCSTRUCT_LINLSQ_H_