tesseract  4.0.0-1-g2a2b
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_t 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 28 of file linlsq.h.

Constructor & Destructor Documentation

◆ LLSQ()

LLSQ::LLSQ ( )
inline

Definition at line 30 of file linlsq.h.

30  { // constructor
31  clear(); // set to zeros
32  }
void clear()
Definition: linlsq.cpp:32

Member Function Documentation

◆ add() [1/3]

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

Definition at line 48 of file linlsq.cpp.

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

◆ add() [2/3]

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

Definition at line 57 of file linlsq.cpp.

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

◆ add() [3/3]

void LLSQ::add ( const LLSQ other)

Definition at line 66 of file linlsq.cpp.

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

◆ c()

double LLSQ::c ( double  m) const

Definition at line 116 of file linlsq.cpp.

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

◆ clear()

void LLSQ::clear ( )

Definition at line 32 of file linlsq.cpp.

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

◆ count()

int32_t LLSQ::count ( ) const
inline

Definition at line 43 of file linlsq.h.

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

◆ covariance()

double LLSQ::covariance ( ) const
inline

Definition at line 75 of file linlsq.h.

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

◆ m()

double LLSQ::m ( ) const

Definition at line 100 of file linlsq.cpp.

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

◆ mean_point()

FCOORD LLSQ::mean_point ( ) const

Definition at line 166 of file linlsq.cpp.

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

◆ pearson()

double LLSQ::pearson ( ) const

Definition at line 153 of file linlsq.cpp.

153  { // get correlation
154  double r = 0.0; // Correlation is 0 if insufficient data.
155 
156  double covar = covariance();
157  if (covar != 0.0) {
158  double var_product = x_variance() * y_variance();
159  if (var_product > 0.0)
160  r = covar / std::sqrt(var_product);
161  }
162  return r;
163 }
double covariance() const
Definition: linlsq.h:75
double y_variance() const
Definition: linlsq.h:87
double x_variance() const
Definition: linlsq.h:81

◆ remove()

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

Definition at line 82 of file linlsq.cpp.

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

◆ rms()

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

Definition at line 130 of file linlsq.cpp.

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

◆ rms_orth()

double LLSQ::rms_orth ( const FCOORD dir) const

Definition at line 195 of file linlsq.cpp.

195  {
196  FCOORD v = !dir;
197  v.normalise();
198  return std::sqrt(v.x() * v.x() * x_variance() +
199  2 * v.x() * v.y() * covariance() +
200  v.y() * v.y() * y_variance());
201 }
bool normalise()
Convert to unit vec.
double covariance() const
Definition: linlsq.h:75
double y_variance() const
Definition: linlsq.h:87
Definition: points.h:189
double x_variance() const
Definition: linlsq.h:81
float x() const
Definition: points.h:208
float y() const
Definition: points.h:211

◆ vector_fit()

FCOORD LLSQ::vector_fit ( ) const

Definition at line 251 of file linlsq.cpp.

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

◆ x_variance()

double LLSQ::x_variance ( ) const
inline

Definition at line 81 of file linlsq.h.

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

◆ y_variance()

double LLSQ::y_variance ( ) const
inline

Definition at line 87 of file linlsq.h.

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

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