tesseract  4.0.0-1-g2a2b
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_t xbuckets, uint8_t ybuckets, uint8_t 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 // Returns an INT_FEATURE_STRUCT corresponding to the given index.
48 // This is the inverse of the Index member.
51  index / theta_buckets_ % y_buckets_,
52  index % theta_buckets_);
53 }
54 
55 // Bulk calls to Index. Maps the given array of features to a vector of
56 // int32_t indices in the same order as the input.
58  int num_features,
59  GenericVector<int>* mapped_features) const {
60  mapped_features->truncate(0);
61  for (int f = 0; f < num_features; ++f)
62  mapped_features->push_back(Index(features[f]));
63 }
64 
65 // Bulk calls to Index. Maps the given array of features to a vector of
66 // sorted int32_t indices.
68  const INT_FEATURE_STRUCT* features, int num_features,
69  GenericVector<int>* sorted_features) const {
70  sorted_features->truncate(0);
71  for (int f = 0; f < num_features; ++f)
72  sorted_features->push_back(Index(features[f]));
73  sorted_features->sort();
74 }
75 
76 // Returns a feature space index for the given x,y position in a display
77 // window, or -1 if the feature is a miss.
78 int IntFeatureSpace::XYToFeatureIndex(int x, int y) const {
79  // Round the x,y position to a feature. Search for a valid theta.
80  INT_FEATURE_STRUCT feature(x, y, 0);
81  int index = -1;
82  for (int theta = 0; theta <= UINT8_MAX && index < 0; ++theta) {
83  feature.Theta = theta;
84  index = Index(feature);
85  }
86  if (index < 0) {
87  tprintf("(%d,%d) does not exist in feature space!\n", x, y);
88  return -1;
89  }
90  feature = PositionFromIndex(index);
91  tprintf("Click at (%d, %d) ->(%d, %d), ->(%d, %d)\n",
92  x, y, feature.X, feature.Y, x - feature.X, y - feature.Y);
93  // Get the relative position of x,y from the rounded feature.
94  x -= feature.X;
95  y -= feature.Y;
96  if (x != 0 || y != 0) {
97  double angle = atan2(static_cast<double>(y), static_cast<double>(x)) + M_PI;
98  angle *= kIntFeatureExtent / (2.0 * M_PI);
99  feature.Theta = static_cast<uint8_t>(angle + 0.5);
100  index = Index(feature);
101  if (index < 0) {
102  tprintf("Feature failed to map to a valid index:");
103  feature.print();
104  return -1;
105  }
106  feature = PositionFromIndex(index);
107  }
108  feature.print();
109  return index;
110 }
111 
112 // Returns an INT_FEATURE_STRUCT corresponding to the given bucket coords.
114  int y,
115  int theta) const {
116  INT_FEATURE_STRUCT pos(
120  return pos;
121 }
122 
123 } // namespace tesseract.
const int kIntFeatureExtent
INT_FEATURE_STRUCT PositionFromBuckets(int x, int y, int theta) const
void Init(uint8_t xbuckets, uint8_t ybuckets, uint8_t thetabuckets)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
int push_back(T object)
void print() const
Definition: intproto.h:145
bool Serialize(FILE *fp) const
int Index(const INT_FEATURE_STRUCT &f) const
void truncate(int size)
int DivRounded(int a, int b)
Definition: helpers.h:162
void IndexAndSortFeatures(const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *sorted_features) const
INT_FEATURE_STRUCT PositionFromIndex(int index) const
void IndexFeatures(const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *mapped_features) const
int XYToFeatureIndex(int x, int y) const