tesseract  5.0.0
matrix_test.cc
Go to the documentation of this file.
1 // File: matrix_test.cc
3 // Author: rays@google.com (Ray Smith)
4 //
5 // Copyright 2016 Google Inc. All Rights Reserved.
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 
17 #include "matrix.h"
18 #include "include_gunit.h"
19 
20 namespace tesseract {
21 
22 class MatrixTest : public ::testing::Test {
23 protected:
24  void SetUp() override {
25  std::locale::global(std::locale(""));
26  }
27 
28  // Fills src_ with data so it can pretend to be a tensor thus:
29  // dims_=[5, 4, 3, 2]
30  // array_=[0, 1, 2, ....119]
31  // tensor=[[[[0, 1][2, 3][4, 5]]
32  // [[6, 7][8, 9][10, 11]]
33  // [[12, 13][14, 15][16, 17]]
34  // [[18, 19][20, 21][22, 23]]]
35  // [[[24, 25]...
37  src_.Resize(1, kInputSize_, 0);
38  for (int i = 0; i < kInputSize_; ++i) {
39  src_.put(0, i, i);
40  }
41  for (int i = 0; i < kNumDims_; ++i) {
42  dims_[i] = 5 - i;
43  }
44  }
45  // Number of dimensions in src_.
46  static const int kNumDims_ = 4;
47  // Number of elements in src_.
48  static const int kInputSize_ = 120;
49  // Size of each dimension in src_;
51  // Input array filled with [0,kInputSize).
53 };
54 
55 // Tests that the RotatingTranspose function does the right thing for various
56 // transformations.
57 // dims=[5, 4, 3, 2]->[5, 2, 4, 3]
58 TEST_F(MatrixTest, RotatingTranspose_3_1) {
60  src_.RotatingTranspose(dims_, kNumDims_, 3, 1, &m);
61  m.ResizeNoInit(kInputSize_ / 3, 3);
62  // Verify that the result is:
63  // output tensor=[[[[0, 2, 4][6, 8, 10][12, 14, 16][18, 20, 22]]
64  // [[1, 3, 5][7, 9, 11][13, 15, 17][19, 21, 23]]]
65  // [[[24, 26, 28]...
66  EXPECT_EQ(0, m(0, 0));
67  EXPECT_EQ(2, m(0, 1));
68  EXPECT_EQ(4, m(0, 2));
69  EXPECT_EQ(6, m(1, 0));
70  EXPECT_EQ(1, m(4, 0));
71  EXPECT_EQ(24, m(8, 0));
72  EXPECT_EQ(26, m(8, 1));
73  EXPECT_EQ(25, m(12, 0));
74 }
75 
76 // dims=[5, 4, 3, 2]->[3, 5, 4, 2]
77 TEST_F(MatrixTest, RotatingTranspose_2_0) {
79  src_.RotatingTranspose(dims_, kNumDims_, 2, 0, &m);
80  m.ResizeNoInit(kInputSize_ / 2, 2);
81  // Verify that the result is:
82  // output tensor=[[[[0, 1][6, 7][12, 13][18, 19]]
83  // [[24, 25][30, 31][36, 37][42, 43]]
84  // [[48, 49][54, 55][60, 61][66, 67]]
85  // [[72, 73][78, 79][84, 85][90, 91]]
86  // [[96, 97][102, 103][108, 109][114, 115]]]
87  // [[[2,3]...
88  EXPECT_EQ(0, m(0, 0));
89  EXPECT_EQ(1, m(0, 1));
90  EXPECT_EQ(6, m(1, 0));
91  EXPECT_EQ(7, m(1, 1));
92  EXPECT_EQ(24, m(4, 0));
93  EXPECT_EQ(25, m(4, 1));
94  EXPECT_EQ(30, m(5, 0));
95  EXPECT_EQ(2, m(20, 0));
96 }
97 
98 // dims=[5, 4, 3, 2]->[5, 3, 2, 4]
99 TEST_F(MatrixTest, RotatingTranspose_1_3) {
101  src_.RotatingTranspose(dims_, kNumDims_, 1, 3, &m);
102  m.ResizeNoInit(kInputSize_ / 4, 4);
103  // Verify that the result is:
104  // output tensor=[[[[0, 6, 12, 18][1, 7, 13, 19]]
105  // [[2, 8, 14, 20][3, 9, 15, 21]]
106  // [[4, 10, 16, 22][5, 11, 17, 23]]]
107  // [[[24, 30, 36, 42]...
108  EXPECT_EQ(0, m(0, 0));
109  EXPECT_EQ(6, m(0, 1));
110  EXPECT_EQ(1, m(1, 0));
111  EXPECT_EQ(2, m(2, 0));
112  EXPECT_EQ(3, m(3, 0));
113  EXPECT_EQ(4, m(4, 0));
114  EXPECT_EQ(5, m(5, 0));
115  EXPECT_EQ(24, m(6, 0));
116  EXPECT_EQ(30, m(6, 1));
117 }
118 
119 // dims=[5, 4, 3, 2]->[4, 3, 5, 2]
120 TEST_F(MatrixTest, RotatingTranspose_0_2) {
122  src_.RotatingTranspose(dims_, kNumDims_, 0, 2, &m);
123  m.ResizeNoInit(kInputSize_ / 2, 2);
124  // Verify that the result is:
125  // output tensor=[[[[0, 1][24, 25][48, 49][72, 73][96, 97]]
126  // [[2, 3][26, 27][50, 51][74, 75][98, 99]]
127  // [[4, 5][28, 29][52, 53][76, 77][100, 101]]]
128  // [[[6, 7]...
129  EXPECT_EQ(0, m(0, 0));
130  EXPECT_EQ(1, m(0, 1));
131  EXPECT_EQ(24, m(1, 0));
132  EXPECT_EQ(25, m(1, 1));
133  EXPECT_EQ(96, m(4, 0));
134  EXPECT_EQ(97, m(4, 1));
135  EXPECT_EQ(2, m(5, 0));
136  EXPECT_EQ(6, m(15, 0));
137 }
138 
139 } // namespace tesseract
TEST_F(EuroText, FastLatinOCR)
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:110
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:94
void RotatingTranspose(const int *dims, int num_dims, int src_dim, int dest_dim, GENERIC_2D_ARRAY< T > *result) const
Definition: matrix.h:468
void put(ICOORD pos, const T &thing)
Definition: matrix.h:260
void SetUp() override
Definition: matrix_test.cc:24
int dims_[kNumDims_]
Definition: matrix_test.cc:50
GENERIC_2D_ARRAY< int > src_
Definition: matrix_test.cc:52
static const int kNumDims_
Definition: matrix_test.cc:46
static const int kInputSize_
Definition: matrix_test.cc:48