Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
 
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 ( )
inline

Definition at line 28 of file linlsq.h.

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

Member Function Documentation

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

Definition at line 50 of file linlsq.cpp.

50  { // add an element
51  total_weight++; // count elements
52  sigx += x; // update accumulators
53  sigy += y;
54  sigxx += x * x;
55  sigxy += x * y;
56  sigyy += y * y;
57 }
void LLSQ::add ( double  x,
double  y,
double  weight 
)

Definition at line 59 of file linlsq.cpp.

59  {
60  total_weight += weight;
61  sigx += x * weight; // update accumulators
62  sigy += y * weight;
63  sigxx += x * x * weight;
64  sigxy += x * y * weight;
65  sigyy += y * y * weight;
66 }
void LLSQ::add ( const LLSQ other)

Definition at line 68 of file linlsq.cpp.

68  {
69  total_weight += other.total_weight;
70  sigx += other.sigx; // update accumulators
71  sigy += other.sigy;
72  sigxx += other.sigxx;
73  sigxy += other.sigxy;
74  sigyy += other.sigyy;
75 }
double LLSQ::c ( double  m) const

Definition at line 118 of file linlsq.cpp.

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

Definition at line 34 of file linlsq.cpp.

34  { // initialize
35  total_weight = 0.0; // no elements
36  sigx = 0.0; // update accumulators
37  sigy = 0.0;
38  sigxx = 0.0;
39  sigxy = 0.0;
40  sigyy = 0.0;
41 }
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  }
double LLSQ::covariance ( ) const
inline

Definition at line 60 of file linlsq.h.

60  {
61  if (total_weight > 0.0)
62  return (sigxy - sigx * sigy / total_weight) / total_weight;
63  else
64  return 0.0;
65  }
double LLSQ::m ( ) const

Definition at line 102 of file linlsq.cpp.

102  { // get gradient
103  double covar = covariance();
104  double x_var = x_variance();
105  if (x_var != 0.0)
106  return covar / x_var;
107  else
108  return 0.0; // too little
109 }
double covariance() const
Definition: linlsq.h:60
double x_variance() const
Definition: linlsq.h:66
FCOORD LLSQ::mean_point ( ) const

Definition at line 168 of file linlsq.cpp.

168  {
169  if (total_weight > 0.0) {
170  return FCOORD(sigx / total_weight, sigy / total_weight);
171  } else {
172  return FCOORD(0.0f, 0.0f);
173  }
174 }
#define f(xc, yc)
Definition: imgscale.cpp:39
Definition: points.h:189
double LLSQ::pearson ( ) const

Definition at line 155 of file linlsq.cpp.

155  { // get correlation
156  double r = 0.0; // Correlation is 0 if insufficent data.
157 
158  double covar = covariance();
159  if (covar != 0.0) {
160  double var_product = x_variance() * y_variance();
161  if (var_product > 0.0)
162  r = covar / sqrt(var_product);
163  }
164  return r;
165 }
double y_variance() const
Definition: linlsq.h:72
double covariance() const
Definition: linlsq.h:60
double x_variance() const
Definition: linlsq.h:66
void LLSQ::remove ( double  x,
double  y 
)

Definition at line 84 of file linlsq.cpp.

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

Definition at line 132 of file linlsq.cpp.

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

Definition at line 182 of file linlsq.cpp.

182  {
183  double x_var = x_variance();
184  double y_var = y_variance();
185  double covar = covariance();
186  FCOORD result;
187  if (x_var >= y_var) {
188  if (x_var == 0.0)
189  return FCOORD(0.0f, 0.0f);
190  result.set_x(x_var / sqrt(x_var * x_var + covar * covar));
191  result.set_y(sqrt(1.0 - result.x() * result.x()));
192  } else {
193  result.set_y(y_var / sqrt(y_var * y_var + covar * covar));
194  result.set_x(sqrt(1.0 - result.y() * result.y()));
195  }
196  if (covar < 0.0)
197  result.set_y(-result.y());
198  return result;
199 }
void set_x(float xin)
rewrite function
Definition: points.h:216
double y_variance() const
Definition: linlsq.h:72
#define f(xc, yc)
Definition: imgscale.cpp:39
double covariance() const
Definition: linlsq.h:60
double x_variance() const
Definition: linlsq.h:66
Definition: points.h:189
float y() const
Definition: points.h:212
void set_y(float yin)
rewrite function
Definition: points.h:220
float x() const
Definition: points.h:209
double LLSQ::x_variance ( ) const
inline

Definition at line 66 of file linlsq.h.

66  {
67  if (total_weight > 0.0)
68  return (sigxx - sigx * sigx / total_weight) / total_weight;
69  else
70  return 0.0;
71  }
double LLSQ::y_variance ( ) const
inline

Definition at line 72 of file linlsq.h.

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

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