tesseract v5.3.3.20231005
tesseract::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 29 of file linlsq.h.

Constructor & Destructor Documentation

◆ LLSQ()

tesseract::LLSQ::LLSQ ( )
inline

Definition at line 31 of file linlsq.h.

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

Member Function Documentation

◆ add() [1/3]

void tesseract::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}

◆ add() [2/3]

void tesseract::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}
const double y

◆ add() [3/3]

void tesseract::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}

◆ c()

double tesseract::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 }
122}
double m() const
Definition: linlsq.cpp:100

◆ clear()

void tesseract::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}

◆ count()

int32_t tesseract::LLSQ::count ( ) const
inline

Definition at line 44 of file linlsq.h.

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

◆ covariance()

double tesseract::LLSQ::covariance ( ) const
inline

Definition at line 76 of file linlsq.h.

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

◆ m()

double tesseract::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 }
108}
double covariance() const
Definition: linlsq.h:76
double x_variance() const
Definition: linlsq.h:83

◆ mean_point()

FCOORD tesseract::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}

◆ pearson()

double tesseract::LLSQ::pearson ( ) const

Definition at line 152 of file linlsq.cpp.

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

◆ remove()

void tesseract::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);
85 }
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}
constexpr ERRCODE EMPTY_LLSQ("Can't delete from an empty LLSQ")
@ ABORT
Definition: errcode.h:31
void error(const char *caller, TessErrorLogCode action, const char *format,...) const __attribute__((format(gnu_printf
Definition: errcode.cpp:40

◆ rms()

double tesseract::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 * (total_weight * c - 2 * sigy);
135 if (error >= 0) {
136 error = std::sqrt(error / total_weight); // sqrt of mean
137 } else {
138 error = 0;
139 }
140 } else {
141 error = 0; // too little
142 }
143 return error;
144}
double c(double m) const
Definition: linlsq.cpp:116

◆ rms_orth()

double tesseract::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(x_variance() * v.x() * v.x() + 2 * covariance() * v.x() * v.y() +
199 y_variance() * v.y() * v.y());
200}

◆ vector_fit()

FCOORD tesseract::LLSQ::vector_fit ( ) const

Definition at line 250 of file linlsq.cpp.

250 {
251 double x_var = x_variance();
252 double y_var = y_variance();
253 double covar = covariance();
254 double theta = 0.5 * atan2(2.0 * covar, x_var - y_var);
255 FCOORD result(cos(theta), sin(theta));
256 return result;
257}

◆ x_variance()

double tesseract::LLSQ::x_variance ( ) const
inline

Definition at line 83 of file linlsq.h.

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

◆ y_variance()

double tesseract::LLSQ::y_variance ( ) const
inline

Definition at line 90 of file linlsq.h.

90 {
91 if (total_weight > 0.0) {
92 return (sigyy - sigy * sigy / total_weight) / total_weight;
93 } else {
94 return 0.0;
95 }
96 }

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