tesseract  5.0.0
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 tesseract {
17 
18 class LLSQTest : public testing::Test {
19 protected:
20  void SetUp() override {
21  std::locale::global(std::locale(""));
22  }
23 
24 public:
25  void TearDown() override {}
26 
27  void ExpectCorrectLine(const LLSQ &llsq, double m, double c, double rms, double pearson,
28  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, FCOORD correct_vector,
57  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), FCOORD(half_root_2, half_root_2), 1e-6);
75  llsq.remove(2.0, 2.0);
76  llsq.add(1.0, 2.0);
77  llsq.add(10.0, 1.0);
78  llsq.add(-8.0, 1.0);
79  // The point at 1,2 pulls the result away from what would otherwise be a
80  // perfect fit to a horizontal line by 0.25 unit, with rms error of 0.433.
81  ExpectCorrectLine(llsq, 0.0, 1.25, 0.433, 0.0, 1e-2);
82  ExpectCorrectVector(llsq, FCOORD(1.0f, 1.25f), FCOORD(1.0f, 0.0f), 1e-3);
83  llsq.add(1.0, 2.0, 10.0);
84  // With a heavy weight, the point at 1,2 pulls the line nearer.
85  ExpectCorrectLine(llsq, 0.0, 1.786, 0.41, 0.0, 1e-2);
86  ExpectCorrectVector(llsq, FCOORD(1.0f, 1.786f), FCOORD(1.0f, 0.0f), 1e-3);
87 }
88 
89 // Tests a simple baseline-style normalization with a rotation.
90 TEST_F(LLSQTest, Vectors) {
91  LLSQ llsq;
92  llsq.add(1.0, 1.0);
93  llsq.add(1.0, -1.0);
94  ExpectCorrectVector(llsq, FCOORD(1.0f, 0.0f), FCOORD(0.0f, 1.0f), 1e-6);
95  llsq.add(0.9, -2.0);
96  llsq.add(1.1, -3.0);
97  llsq.add(0.9, 2.0);
98  llsq.add(1.10001, 3.0);
99  ExpectCorrectVector(llsq, FCOORD(1.0f, 0.0f), FCOORD(0.0f, 1.0f), 1e-3);
100 }
101 
102 // Verify that rms_orth() actually calculates:
103 // sqrt( sum (!nvec * (x_i - x_avg))^2 / n)
104 TEST_F(LLSQTest, RmsOrthWorksAsIntended) {
105  std::vector<FCOORD> pts;
106  pts.emplace_back(0.56, 0.95);
107  pts.emplace_back(0.09, 0.09);
108  pts.emplace_back(0.13, 0.77);
109  pts.emplace_back(0.16, 0.83);
110  pts.emplace_back(0.45, 0.79);
111  VerifyRmsOrth(pts, FCOORD(1, 0));
112  VerifyRmsOrth(pts, FCOORD(1, 1));
113  VerifyRmsOrth(pts, FCOORD(1, 2));
114  VerifyRmsOrth(pts, FCOORD(2, 1));
115 }
116 
117 } // namespace tesseract
TEST_F(EuroText, FastLatinOCR)
void add(double x, double y)
Definition: linlsq.cpp:49
void remove(double x, double y)
Definition: linlsq.cpp:82
double pearson() const
Definition: linlsq.cpp:152
double rms_orth(const FCOORD &dir) const
Definition: linlsq.cpp:195
double m() const
Definition: linlsq.cpp:100
double c(double m) const
Definition: linlsq.cpp:116
FCOORD mean_point() const
Definition: linlsq.cpp:166
double rms(double m, double c) const
Definition: linlsq.cpp:130
FCOORD vector_fit() const
Definition: linlsq.cpp:250
bool normalise()
Convert to unit vec.
Definition: points.cpp:32
float y() const
Definition: points.h:209
float x() const
Definition: points.h:206
void ExpectCorrectLine(const LLSQ &llsq, double m, double c, double rms, double pearson, double tolerance)
Definition: linlsq_test.cc:27
void VerifyRmsOrth(const std::vector< FCOORD > &pts, const FCOORD &orth)
Definition: linlsq_test.cc:41
void ExpectCorrectVector(const LLSQ &llsq, FCOORD correct_mean_pt, FCOORD correct_vector, float tolerance)
Definition: linlsq_test.cc:56
FCOORD PtsMean(const std::vector< FCOORD > &pts)
Definition: linlsq_test.cc:34
void TearDown() override
Definition: linlsq_test.cc:25
void SetUp() override
Definition: linlsq_test.cc:20