tesseract  4.00.00dev
intfeaturespace.h
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.h
5 // Description: Indexed feature space based on INT_FEATURE_STRUCT.
6 // Created: Wed Mar 24 10:55:30 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 #ifndef TESSERACT_CLASSIFY_INTFEATURESPACE_H_
21 #define TESSERACT_CLASSIFY_INTFEATURESPACE_H_
22 
23 #include "genericvector.h"
24 #include "intproto.h"
25 
26 // Extent of x,y,theta in the input feature space. [0,255].
27 const int kIntFeatureExtent = 256;
28 // Extent of x,y,theta dimensions in the quantized feature space.
29 const int kBoostXYBuckets = 16;
30 const int kBoostDirBuckets = 16;
31 
32 namespace tesseract {
33 
34 class IndexMap;
35 
36 // Down-sampling quantization of the INT_FEATURE_STRUCT feature space and
37 // conversion to a single scalar index value, used as a binary feature space.
39  public:
41  // Default copy constructors and assignment OK!
42 
43  // Setup the feature space with the given dimensions.
44  void Init(uinT8 xbuckets, uinT8 ybuckets, uinT8 thetabuckets);
45 
46  // Serializes the feature space definition to the given file.
47  // Returns false on error.
48  bool Serialize(FILE* fp) const;
49 
50  // DeSerializes the feature space definition from the given file.
51  // If swap is true, the data is big/little-endian swapped.
52  // Returns false on error.
53  bool DeSerialize(bool swap, FILE* fp);
54 
55  // Returns the total size of the feature space.
56  int Size() const {
57  return static_cast<int>(x_buckets_) * y_buckets_ * theta_buckets_;
58  }
59  // Returns an INT_FEATURE_STRUCT corresponding to the given index.
60  // This is the inverse of the Index member.
61  INT_FEATURE_STRUCT PositionFromIndex(int index) const;
62 
63  // Returns a 1-dimensional index corresponding to the given feature value.
64  // Range is [0, Size()-1]. Inverse of PositionFromIndex member.
65  int Index(const INT_FEATURE_STRUCT& f) const {
66  return (XBucket(f.X) * y_buckets_ + YBucket(f.Y)) * theta_buckets_ +
67  ThetaBucket(f.Theta);
68  }
69  // Bulk calls to Index. Maps the given array of features to a vector of
70  // inT32 indices in the same order as the input.
71  void IndexFeatures(const INT_FEATURE_STRUCT* features, int num_features,
72  GenericVector<int>* mapped_features) const;
73  // Bulk calls to Index. Maps the given array of features to a vector of
74  // sorted inT32 indices.
75  void IndexAndSortFeatures(const INT_FEATURE_STRUCT* features,
76  int num_features,
77  GenericVector<int>* sorted_features) const;
78  // Returns a feature space index for the given x,y position in a display
79  // window, or -1 if the feature is a miss.
80  int XYToFeatureIndex(int x, int y) const;
81 
82  protected:
83  // Converters to generate indices for individual feature dimensions.
84  int XBucket(int x) const {
85  int bucket = x * x_buckets_ / kIntFeatureExtent;
86  return ClipToRange(bucket, 0, static_cast<int>(x_buckets_) - 1);
87  }
88  int YBucket(int y) const {
89  int bucket = y * y_buckets_ / kIntFeatureExtent;
90  return ClipToRange(bucket, 0, static_cast<int>(y_buckets_) - 1);
91  }
92  // Use DivRounded for theta so that exactly vertical and horizontal are in
93  // the middle of a bucket. The Modulo takes care of the wrap-around.
94  int ThetaBucket(int theta) const {
95  int bucket = DivRounded(theta * theta_buckets_, kIntFeatureExtent);
96  return Modulo(bucket, theta_buckets_);
97  }
98  // Returns an INT_FEATURE_STRUCT corresponding to the given buckets.
99  INT_FEATURE_STRUCT PositionFromBuckets(int x, int y, int theta) const;
100 
101  // Feature space definition - serialized.
105 };
106 
107 } // namespace tesseract.
108 
109 #endif // TESSERACT_CLASSIFY_INTFEATURESPACE_H_
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
int ThetaBucket(int theta) const
int Index(const INT_FEATURE_STRUCT &f) const
void Init(uinT8 xbuckets, uinT8 ybuckets, uinT8 thetabuckets)
int XYToFeatureIndex(int x, int y) const
void IndexAndSortFeatures(const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *sorted_features) const
const int kBoostDirBuckets
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
const int kIntFeatureExtent
bool DeSerialize(bool swap, FILE *fp)
const int kBoostXYBuckets
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
int Modulo(int a, int b)
Definition: helpers.h:164