tesseract  4.00.00dev
intfeaturespace.cpp
Go to the documentation of this file.
1 // Copyright 2010 Google Inc. All Rights Reserved.
2 // Author: rays@google.com (Ray Smith)
4 // File: intfeaturespace.cpp
5 // Description: Indexed feature space based on INT_FEATURE_STRUCT.
6 // Created: Wed Mar 24 11:21:27 PDT 2010
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 
20 #include "intfeaturespace.h"
21 #include "intfx.h"
22 
23 namespace tesseract {
24 
26  : x_buckets_(0), y_buckets_(0), theta_buckets_(0) {
27 }
28 
29 void IntFeatureSpace::Init(uinT8 xbuckets, uinT8 ybuckets, uinT8 thetabuckets) {
30  x_buckets_ = xbuckets;
31  y_buckets_ = ybuckets;
32  theta_buckets_ = thetabuckets;
33 }
34 
35 // Serializes the feature space definition to the given file.
36 // Returns false on error.
37 bool IntFeatureSpace::Serialize(FILE* fp) const {
38  if (fwrite(&x_buckets_, sizeof(x_buckets_), 1, fp) != 1)
39  return false;
40  if (fwrite(&y_buckets_, sizeof(y_buckets_), 1, fp) != 1)
41  return false;
42  if (fwrite(&theta_buckets_, sizeof(theta_buckets_), 1, fp) != 1)
43  return false;
44  return true;
45 }
46 
47 // DeSerializes the feature space definition from the given file.
48 // If swap is true, the data is big/little-endian swapped.
49 // Returns false on error.
50 bool IntFeatureSpace::DeSerialize(bool swap, FILE* fp) {
51  if (fread(&x_buckets_, sizeof(x_buckets_), 1, fp) != 1)
52  return false;
53  if (fread(&y_buckets_, sizeof(y_buckets_), 1, fp) != 1)
54  return false;
55  if (fread(&theta_buckets_, sizeof(theta_buckets_), 1, fp) != 1)
56  return false;
57  return true;
58 }
59 
60 // Returns an INT_FEATURE_STRUCT corresponding to the given index.
61 // This is the inverse of the Index member.
64  index / theta_buckets_ % y_buckets_,
65  index % theta_buckets_);
66 }
67 
68 // Bulk calls to Index. Maps the given array of features to a vector of
69 // inT32 indices in the same order as the input.
71  int num_features,
72  GenericVector<int>* mapped_features) const {
73  mapped_features->truncate(0);
74  for (int f = 0; f < num_features; ++f)
75  mapped_features->push_back(Index(features[f]));
76 }
77 
78 // Bulk calls to Index. Maps the given array of features to a vector of
79 // sorted inT32 indices.
81  const INT_FEATURE_STRUCT* features, int num_features,
82  GenericVector<int>* sorted_features) const {
83  sorted_features->truncate(0);
84  for (int f = 0; f < num_features; ++f)
85  sorted_features->push_back(Index(features[f]));
86  sorted_features->sort();
87 }
88 
89 // Returns a feature space index for the given x,y position in a display
90 // window, or -1 if the feature is a miss.
91 int IntFeatureSpace::XYToFeatureIndex(int x, int y) const {
92  // Round the x,y position to a feature. Search for a valid theta.
93  INT_FEATURE_STRUCT feature(x, y, 0);
94  int index = -1;
95  for (int theta = 0; theta <= MAX_UINT8 && index < 0; ++theta) {
96  feature.Theta = theta;
97  index = Index(feature);
98  }
99  if (index < 0) {
100  tprintf("(%d,%d) does not exist in feature space!\n", x, y);
101  return -1;
102  }
103  feature = PositionFromIndex(index);
104  tprintf("Click at (%d, %d) ->(%d, %d), ->(%d, %d)\n",
105  x, y, feature.X, feature.Y, x - feature.X, y - feature.Y);
106  // Get the relative position of x,y from the rounded feature.
107  x -= feature.X;
108  y -= feature.Y;
109  if (x != 0 || y != 0) {
110  double angle = atan2(static_cast<double>(y), static_cast<double>(x)) + PI;
111  angle *= kIntFeatureExtent / (2.0 * PI);
112  feature.Theta = static_cast<uinT8>(angle + 0.5);
113  index = Index(feature);
114  if (index < 0) {
115  tprintf("Feature failed to map to a valid index:");
116  feature.print();
117  return -1;
118  }
119  feature = PositionFromIndex(index);
120  }
121  feature.print();
122  return index;
123 }
124 
125 // Returns an INT_FEATURE_STRUCT corresponding to the given bucket coords.
127  int y,
128  int theta) const {
129  INT_FEATURE_STRUCT pos(
133  return pos;
134 }
135 
136 } // namespace tesseract.
INT_FEATURE_STRUCT PositionFromIndex(int index) const
void IndexFeatures(const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *mapped_features) const
bool Serialize(FILE *fp) const
#define MAX_UINT8
Definition: host.h:63
void print() const
Definition: intproto.h:148
int push_back(T object)
#define tprintf(...)
Definition: tprintf.h:31
int Index(const INT_FEATURE_STRUCT &f) const
void truncate(int size)
void Init(uinT8 xbuckets, uinT8 ybuckets, uinT8 thetabuckets)
int XYToFeatureIndex(int x, int y) const
#define PI
Definition: const.h:19
void IndexAndSortFeatures(const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *sorted_features) const
const int kIntFeatureExtent
bool DeSerialize(bool swap, FILE *fp)
uint8_t uinT8
Definition: host.h:35
INT_FEATURE_STRUCT PositionFromBuckets(int x, int y, int theta) const
int DivRounded(int a, int b)
Definition: helpers.h:173
const char features[]
Definition: feature_tests.c:2