tesseract  5.0.0-alpha-619-ge9db
linlsq_test.cc
Go to the documentation of this file.
1 // (C) Copyright 2017, Google Inc.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 // http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software
7 // distributed under the License is distributed on an "AS IS" BASIS,
8 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 // See the License for the specific language governing permissions and
10 // limitations under the License.
11 
12 #include "linlsq.h"
13 
14 #include "include_gunit.h"
15 
16 namespace {
17 
18 class LLSQTest : public testing::Test {
19  protected:
20  void SetUp() {
21  std::locale::global(std::locale(""));
22  }
23 
24  public:
25  void TearDown() {}
26 
27  void ExpectCorrectLine(const LLSQ& llsq, double m, double c, double rms,
28  double pearson, double tolerance) {
29  EXPECT_NEAR(m, llsq.m(), tolerance);
30  EXPECT_NEAR(c, llsq.c(llsq.m()), tolerance);
31  EXPECT_NEAR(rms, llsq.rms(llsq.m(), llsq.c(llsq.m())), tolerance);
32  EXPECT_NEAR(pearson, llsq.pearson(), tolerance);
33  }
34  FCOORD PtsMean(const std::vector<FCOORD>& pts) {
35  FCOORD total(0, 0);
36  for (const auto& p : pts) {
37  total += p;
38  }
39  return (pts.size() > 0) ? total / pts.size() : total;
40  }
41  void VerifyRmsOrth(const std::vector<FCOORD>& pts, const FCOORD& orth) {
42  LLSQ llsq;
43  FCOORD xavg = PtsMean(pts);
44  FCOORD nvec = !orth;
45  nvec.normalise();
46  double expected_answer = 0;
47  for (const auto& p : pts) {
48  llsq.add(p.x(), p.y());
49  double dot = nvec % (p - xavg);
50  expected_answer += dot * dot;
51  }
52  expected_answer /= pts.size();
53  expected_answer = sqrt(expected_answer);
54  EXPECT_NEAR(expected_answer, llsq.rms_orth(orth), 0.0001);
55  }
56  void ExpectCorrectVector(const LLSQ& llsq, FCOORD correct_mean_pt,
57  FCOORD correct_vector, float tolerance) {
58  FCOORD mean_pt = llsq.mean_point();
59  FCOORD vector = llsq.vector_fit();
60  EXPECT_NEAR(correct_mean_pt.x(), mean_pt.x(), tolerance);
61  EXPECT_NEAR(correct_mean_pt.y(), mean_pt.y(), tolerance);
62  EXPECT_NEAR(correct_vector.x(), vector.x(), tolerance);
63  EXPECT_NEAR(correct_vector.y(), vector.y(), tolerance);
64  }
65 };
66 
67 // Tests a simple baseline-style normalization.
68 TEST_F(LLSQTest, BasicLines) {
69  LLSQ llsq;
70  llsq.add(1.0, 1.0);
71  llsq.add(2.0, 2.0);
72  ExpectCorrectLine(llsq, 1.0, 0.0, 0.0, 1.0, 1e-6);
73  float half_root_2 = sqrt(2.0) / 2.0f;
74  ExpectCorrectVector(llsq, FCOORD(1.5f, 1.5f),
75  FCOORD(half_root_2, half_root_2), 1e-6);
76  llsq.remove(2.0, 2.0);
77  llsq.add(1.0, 2.0);
78  llsq.add(10.0, 1.0);
79  llsq.add(-8.0, 1.0);
80  // The point at 1,2 pulls the result away from what would otherwise be a
81  // perfect fit to a horizontal line by 0.25 unit, with rms error of 0.433.
82  ExpectCorrectLine(llsq, 0.0, 1.25, 0.433, 0.0, 1e-2);
83  ExpectCorrectVector(llsq, FCOORD(1.0f, 1.25f), FCOORD(1.0f, 0.0f), 1e-3);
84  llsq.add(1.0, 2.0, 10.0);
85  // With a heavy weight, the point at 1,2 pulls the line nearer.
86  ExpectCorrectLine(llsq, 0.0, 1.786, 0.41, 0.0, 1e-2);
87  ExpectCorrectVector(llsq, FCOORD(1.0f, 1.786f), FCOORD(1.0f, 0.0f), 1e-3);
88 }
89 
90 // Tests a simple baseline-style normalization with a rotation.
91 TEST_F(LLSQTest, Vectors) {
92  LLSQ llsq;
93  llsq.add(1.0, 1.0);
94  llsq.add(1.0, -1.0);
95  ExpectCorrectVector(llsq, FCOORD(1.0f, 0.0f), FCOORD(0.0f, 1.0f), 1e-6);
96  llsq.add(0.9, -2.0);
97  llsq.add(1.1, -3.0);
98  llsq.add(0.9, 2.0);
99  llsq.add(1.10001, 3.0);
100  ExpectCorrectVector(llsq, FCOORD(1.0f, 0.0f), FCOORD(0.0f, 1.0f), 1e-3);
101 }
102 
103 // Verify that rms_orth() actually calculates:
104 // sqrt( sum (!nvec * (x_i - x_avg))^2 / n)
105 TEST_F(LLSQTest, RmsOrthWorksAsIntended) {
106  std::vector<FCOORD> pts;
107  pts.push_back(FCOORD(0.56, 0.95));
108  pts.push_back(FCOORD(0.09, 0.09));
109  pts.push_back(FCOORD(0.13, 0.77));
110  pts.push_back(FCOORD(0.16, 0.83));
111  pts.push_back(FCOORD(0.45, 0.79));
112  VerifyRmsOrth(pts, FCOORD(1, 0));
113  VerifyRmsOrth(pts, FCOORD(1, 1));
114  VerifyRmsOrth(pts, FCOORD(1, 2));
115  VerifyRmsOrth(pts, FCOORD(2, 1));
116 }
117 
118 } // namespace.
LLSQ::add
void add(double x, double y)
Definition: linlsq.cpp:45
LLSQ
Definition: linlsq.h:27
LLSQ::vector_fit
FCOORD vector_fit() const
Definition: linlsq.cpp:243
LLSQ::mean_point
FCOORD mean_point() const
Definition: linlsq.cpp:158
FCOORD::y
float y() const
Definition: points.h:209
LLSQ::c
double c(double m) const
Definition: linlsq.cpp:110
LLSQ::remove
void remove(double x, double y)
Definition: linlsq.cpp:78
FCOORD::x
float x() const
Definition: points.h:206
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
LLSQ::m
double m() const
Definition: linlsq.cpp:95
LLSQ::pearson
double pearson() const
Definition: linlsq.cpp:145
FCOORD
Definition: points.h:187
LLSQ::rms
double rms(double m, double c) const
Definition: linlsq.cpp:123
LLSQ::rms_orth
double rms_orth(const FCOORD &dir) const
Definition: linlsq.cpp:187
linlsq.h
FCOORD::normalise
bool normalise()
Convert to unit vec.