File: linlsq_test.cc

package info (click to toggle)
tesseract 5.5.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,508 kB
  • sloc: cpp: 154,570; makefile: 1,519; java: 1,143; ansic: 852; sh: 763; python: 51
file content (117 lines) | stat: -rw-r--r-- 4,139 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// (C) Copyright 2017, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "linlsq.h"

#include "include_gunit.h"

namespace tesseract {

class LLSQTest : public testing::Test {
protected:
  void SetUp() override {
    std::locale::global(std::locale(""));
  }

public:
  void TearDown() override {}

  void ExpectCorrectLine(const LLSQ &llsq, double m, double c, double rms, double pearson,
                         double tolerance) {
    EXPECT_NEAR(m, llsq.m(), tolerance);
    EXPECT_NEAR(c, llsq.c(llsq.m()), tolerance);
    EXPECT_NEAR(rms, llsq.rms(llsq.m(), llsq.c(llsq.m())), tolerance);
    EXPECT_NEAR(pearson, llsq.pearson(), tolerance);
  }
  FCOORD PtsMean(const std::vector<FCOORD> &pts) {
    FCOORD total(0, 0);
    for (const auto &p : pts) {
      total += p;
    }
    return (pts.size() > 0) ? total / pts.size() : total;
  }
  void VerifyRmsOrth(const std::vector<FCOORD> &pts, const FCOORD &orth) {
    LLSQ llsq;
    FCOORD xavg = PtsMean(pts);
    FCOORD nvec = !orth;
    nvec.normalise();
    double expected_answer = 0;
    for (const auto &p : pts) {
      llsq.add(p.x(), p.y());
      double dot = nvec % (p - xavg);
      expected_answer += dot * dot;
    }
    expected_answer /= pts.size();
    expected_answer = sqrt(expected_answer);
    EXPECT_NEAR(expected_answer, llsq.rms_orth(orth), 0.0001);
  }
  void ExpectCorrectVector(const LLSQ &llsq, FCOORD correct_mean_pt, FCOORD correct_vector,
                           float tolerance) {
    FCOORD mean_pt = llsq.mean_point();
    FCOORD vector = llsq.vector_fit();
    EXPECT_NEAR(correct_mean_pt.x(), mean_pt.x(), tolerance);
    EXPECT_NEAR(correct_mean_pt.y(), mean_pt.y(), tolerance);
    EXPECT_NEAR(correct_vector.x(), vector.x(), tolerance);
    EXPECT_NEAR(correct_vector.y(), vector.y(), tolerance);
  }
};

// Tests a simple baseline-style normalization.
TEST_F(LLSQTest, BasicLines) {
  LLSQ llsq;
  llsq.add(1.0, 1.0);
  llsq.add(2.0, 2.0);
  ExpectCorrectLine(llsq, 1.0, 0.0, 0.0, 1.0, 1e-6);
  float half_root_2 = sqrt(2.0) / 2.0f;
  ExpectCorrectVector(llsq, FCOORD(1.5f, 1.5f), FCOORD(half_root_2, half_root_2), 1e-6);
  llsq.remove(2.0, 2.0);
  llsq.add(1.0, 2.0);
  llsq.add(10.0, 1.0);
  llsq.add(-8.0, 1.0);
  // The point at 1,2 pulls the result away from what would otherwise be a
  // perfect fit to a horizontal line by 0.25 unit, with rms error of 0.433.
  ExpectCorrectLine(llsq, 0.0, 1.25, 0.433, 0.0, 1e-2);
  ExpectCorrectVector(llsq, FCOORD(1.0f, 1.25f), FCOORD(1.0f, 0.0f), 1e-3);
  llsq.add(1.0, 2.0, 10.0);
  // With a heavy weight, the point at 1,2 pulls the line nearer.
  ExpectCorrectLine(llsq, 0.0, 1.786, 0.41, 0.0, 1e-2);
  ExpectCorrectVector(llsq, FCOORD(1.0f, 1.786f), FCOORD(1.0f, 0.0f), 1e-3);
}

// Tests a simple baseline-style normalization with a rotation.
TEST_F(LLSQTest, Vectors) {
  LLSQ llsq;
  llsq.add(1.0, 1.0);
  llsq.add(1.0, -1.0);
  ExpectCorrectVector(llsq, FCOORD(1.0f, 0.0f), FCOORD(0.0f, 1.0f), 1e-6);
  llsq.add(0.9, -2.0);
  llsq.add(1.1, -3.0);
  llsq.add(0.9, 2.0);
  llsq.add(1.10001, 3.0);
  ExpectCorrectVector(llsq, FCOORD(1.0f, 0.0f), FCOORD(0.0f, 1.0f), 1e-3);
}

// Verify that rms_orth() actually calculates:
//   sqrt( sum (!nvec * (x_i - x_avg))^2 / n)
TEST_F(LLSQTest, RmsOrthWorksAsIntended) {
  std::vector<FCOORD> pts;
  pts.emplace_back(0.56, 0.95);
  pts.emplace_back(0.09, 0.09);
  pts.emplace_back(0.13, 0.77);
  pts.emplace_back(0.16, 0.83);
  pts.emplace_back(0.45, 0.79);
  VerifyRmsOrth(pts, FCOORD(1, 0));
  VerifyRmsOrth(pts, FCOORD(1, 1));
  VerifyRmsOrth(pts, FCOORD(1, 2));
  VerifyRmsOrth(pts, FCOORD(2, 1));
}

} // namespace tesseract