tesseract  4.00.00dev
LLSQ Class Reference

#include <linlsq.h>

Public Member Functions

 LLSQ ()
 
void clear ()
 
void add (double x, double y)
 
void add (double x, double y, double weight)
 
void add (const LLSQ &other)
 
void remove (double x, double y)
 
inT32 count () const
 
double m () const
 
double c (double m) const
 
double rms (double m, double c) const
 
double pearson () const
 
FCOORD mean_point () const
 
double rms_orth (const FCOORD &dir) const
 
FCOORD vector_fit () const
 
double covariance () const
 
double x_variance () const
 
double y_variance () const
 

Detailed Description

Definition at line 26 of file linlsq.h.

Constructor & Destructor Documentation

◆ LLSQ()

LLSQ::LLSQ ( )
inline

Definition at line 28 of file linlsq.h.

28  { // constructor
29  clear(); // set to zeros
30  }
void clear()
Definition: linlsq.cpp:33

Member Function Documentation

◆ add() [1/3]

void LLSQ::add ( double  x,
double  y 
)

Definition at line 49 of file linlsq.cpp.

49  { // add an element
50  total_weight++; // count elements
51  sigx += x; // update accumulators
52  sigy += y;
53  sigxx += x * x;
54  sigxy += x * y;
55  sigyy += y * y;
56 }

◆ add() [2/3]

void LLSQ::add ( double  x,
double  y,
double  weight 
)

Definition at line 58 of file linlsq.cpp.

58  {
59  total_weight += weight;
60  sigx += x * weight; // update accumulators
61  sigy += y * weight;
62  sigxx += x * x * weight;
63  sigxy += x * y * weight;
64  sigyy += y * y * weight;
65 }

◆ add() [3/3]

void LLSQ::add ( const LLSQ other)

Definition at line 67 of file linlsq.cpp.

67  {
68  total_weight += other.total_weight;
69  sigx += other.sigx; // update accumulators
70  sigy += other.sigy;
71  sigxx += other.sigxx;
72  sigxy += other.sigxy;
73  sigyy += other.sigyy;
74 }

◆ c()

double LLSQ::c ( double  m) const

Definition at line 117 of file linlsq.cpp.

117  { // get constant
118  if (total_weight > 0.0)
119  return (sigy - m * sigx) / total_weight;
120  else
121  return 0; // too little
122 }
double m() const
Definition: linlsq.cpp:101

◆ clear()

void LLSQ::clear ( )

Definition at line 33 of file linlsq.cpp.

33  { // initialize
34  total_weight = 0.0; // no elements
35  sigx = 0.0; // update accumulators
36  sigy = 0.0;
37  sigxx = 0.0;
38  sigxy = 0.0;
39  sigyy = 0.0;
40 }

◆ count()

inT32 LLSQ::count ( ) const
inline

Definition at line 41 of file linlsq.h.

41  { // no of elements
42  return static_cast<int>(total_weight + 0.5);
43  }

◆ covariance()

double LLSQ::covariance ( ) const
inline

Definition at line 73 of file linlsq.h.

73  {
74  if (total_weight > 0.0)
75  return (sigxy - sigx * sigy / total_weight) / total_weight;
76  else
77  return 0.0;
78  }

◆ m()

double LLSQ::m ( ) const

Definition at line 101 of file linlsq.cpp.

101  { // get gradient
102  double covar = covariance();
103  double x_var = x_variance();
104  if (x_var != 0.0)
105  return covar / x_var;
106  else
107  return 0.0; // too little
108 }
double x_variance() const
Definition: linlsq.h:79
double covariance() const
Definition: linlsq.h:73

◆ mean_point()

FCOORD LLSQ::mean_point ( ) const

Definition at line 167 of file linlsq.cpp.

167  {
168  if (total_weight > 0.0) {
169  return FCOORD(sigx / total_weight, sigy / total_weight);
170  } else {
171  return FCOORD(0.0f, 0.0f);
172  }
173 }
Definition: points.h:189

◆ pearson()

double LLSQ::pearson ( ) const

Definition at line 154 of file linlsq.cpp.

154  { // get correlation
155  double r = 0.0; // Correlation is 0 if insufficent data.
156 
157  double covar = covariance();
158  if (covar != 0.0) {
159  double var_product = x_variance() * y_variance();
160  if (var_product > 0.0)
161  r = covar / sqrt(var_product);
162  }
163  return r;
164 }
double x_variance() const
Definition: linlsq.h:79
double covariance() const
Definition: linlsq.h:73
double y_variance() const
Definition: linlsq.h:85

◆ remove()

void LLSQ::remove ( double  x,
double  y 
)

Definition at line 83 of file linlsq.cpp.

83  { // delete an element
84  if (total_weight <= 0.0) // illegal
85  EMPTY_LLSQ.error("LLSQ::remove", ABORT, NULL);
86  total_weight--; // count elements
87  sigx -= x; // update accumulators
88  sigy -= y;
89  sigxx -= x * x;
90  sigxy -= x * y;
91  sigyy -= y * y;
92 }
const ERRCODE EMPTY_LLSQ
Definition: linlsq.cpp:25
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
Definition: errcode.h:30

◆ rms()

double LLSQ::rms ( double  m,
double  c 
) const

Definition at line 131 of file linlsq.cpp.

131  { // get error
132  double error; // total error
133 
134  if (total_weight > 0) {
135  error = sigyy + m * (m * sigxx + 2 * (c * sigx - sigxy)) + c *
136  (total_weight * c - 2 * sigy);
137  if (error >= 0)
138  error = sqrt(error / total_weight); // sqrt of mean
139  else
140  error = 0;
141  } else {
142  error = 0; // too little
143  }
144  return error;
145 }
double m() const
Definition: linlsq.cpp:101
double c(double m) const
Definition: linlsq.cpp:117

◆ rms_orth()

double LLSQ::rms_orth ( const FCOORD dir) const

Definition at line 196 of file linlsq.cpp.

196  {
197  FCOORD v = !dir;
198  v.normalise();
199  return sqrt(v.x() * v.x() * x_variance() +
200  2 * v.x() * v.y() * covariance() +
201  v.y() * v.y() * y_variance());
202 }
double x_variance() const
Definition: linlsq.h:79
Definition: points.h:189
double covariance() const
Definition: linlsq.h:73
bool normalise()
Convert to unit vec.
float y() const
Definition: points.h:212
double y_variance() const
Definition: linlsq.h:85
double v[max]
float x() const
Definition: points.h:209

◆ vector_fit()

FCOORD LLSQ::vector_fit ( ) const

Definition at line 252 of file linlsq.cpp.

252  {
253  double x_var = x_variance();
254  double y_var = y_variance();
255  double covar = covariance();
256  double theta = 0.5 * atan2(2.0 * covar, x_var - y_var);
257  FCOORD result(cos(theta), sin(theta));
258  return result;
259 }
double x_variance() const
Definition: linlsq.h:79
Definition: points.h:189
double covariance() const
Definition: linlsq.h:73
double y_variance() const
Definition: linlsq.h:85

◆ x_variance()

double LLSQ::x_variance ( ) const
inline

Definition at line 79 of file linlsq.h.

79  {
80  if (total_weight > 0.0)
81  return (sigxx - sigx * sigx / total_weight) / total_weight;
82  else
83  return 0.0;
84  }

◆ y_variance()

double LLSQ::y_variance ( ) const
inline

Definition at line 85 of file linlsq.h.

85  {
86  if (total_weight > 0.0)
87  return (sigyy - sigy * sigy / total_weight) / total_weight;
88  else
89  return 0.0;
90  }

The documentation for this class was generated from the following files: