tesseract  5.0.0-alpha-619-ge9db
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 
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_t xbuckets, uint8_t ybuckets, uint8_t 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  // Returns the total size of the feature space.
51  int Size() const {
52  return static_cast<int>(x_buckets_) * y_buckets_ * theta_buckets_;
53  }
54  // Returns an INT_FEATURE_STRUCT corresponding to the given index.
55  // This is the inverse of the Index member.
56  INT_FEATURE_STRUCT PositionFromIndex(int index) const;
57 
58  // Returns a 1-dimensional index corresponding to the given feature value.
59  // Range is [0, Size()-1]. Inverse of PositionFromIndex member.
60  int Index(const INT_FEATURE_STRUCT& f) const {
61  return (XBucket(f.X) * y_buckets_ + YBucket(f.Y)) * theta_buckets_ +
62  ThetaBucket(f.Theta);
63  }
64  // Bulk calls to Index. Maps the given array of features to a vector of
65  // int32_t indices in the same order as the input.
66  void IndexFeatures(const INT_FEATURE_STRUCT* features, int num_features,
67  GenericVector<int>* mapped_features) const;
68  // Bulk calls to Index. Maps the given array of features to a vector of
69  // sorted int32_t indices.
70  void IndexAndSortFeatures(const INT_FEATURE_STRUCT* features,
71  int num_features,
72  GenericVector<int>* sorted_features) const;
73  // Returns a feature space index for the given x,y position in a display
74  // window, or -1 if the feature is a miss.
75  int XYToFeatureIndex(int x, int y) const;
76 
77  protected:
78  // Converters to generate indices for individual feature dimensions.
79  int XBucket(int x) const {
80  int bucket = x * x_buckets_ / kIntFeatureExtent;
81  return ClipToRange(bucket, 0, static_cast<int>(x_buckets_) - 1);
82  }
83  int YBucket(int y) const {
84  int bucket = y * y_buckets_ / kIntFeatureExtent;
85  return ClipToRange(bucket, 0, static_cast<int>(y_buckets_) - 1);
86  }
87  // Use DivRounded for theta so that exactly vertical and horizontal are in
88  // the middle of a bucket. The Modulo takes care of the wrap-around.
89  int ThetaBucket(int theta) const {
90  int bucket = DivRounded(theta * theta_buckets_, kIntFeatureExtent);
91  return Modulo(bucket, theta_buckets_);
92  }
93  // Returns an INT_FEATURE_STRUCT corresponding to the given buckets.
94  INT_FEATURE_STRUCT PositionFromBuckets(int x, int y, int theta) const;
95 
96  // Feature space definition - serialized.
97  uint8_t x_buckets_;
98  uint8_t y_buckets_;
99  uint8_t theta_buckets_;
100 };
101 
102 } // namespace tesseract.
103 
104 #endif // TESSERACT_CLASSIFY_INTFEATURESPACE_H_
ClipToRange
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:106
tesseract::IntFeatureSpace::x_buckets_
uint8_t x_buckets_
Definition: intfeaturespace.h:97
tesseract::IntFeatureSpace::XBucket
int XBucket(int x) const
Definition: intfeaturespace.h:79
tesseract::IntFeatureSpace::ThetaBucket
int ThetaBucket(int theta) const
Definition: intfeaturespace.h:89
tesseract::IntFeatureSpace::IndexAndSortFeatures
void IndexAndSortFeatures(const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *sorted_features) const
Definition: intfeaturespace.cpp:68
INT_FEATURE_STRUCT::Theta
uint8_t Theta
Definition: intproto.h:141
tesseract::IntFeatureSpace::y_buckets_
uint8_t y_buckets_
Definition: intfeaturespace.h:98
tesseract::IntFeatureSpace::Size
int Size() const
Definition: intfeaturespace.h:51
tesseract::IntFeatureSpace::Init
void Init(uint8_t xbuckets, uint8_t ybuckets, uint8_t thetabuckets)
Definition: intfeaturespace.cpp:30
tesseract::IntFeatureSpace::IntFeatureSpace
IntFeatureSpace()
Definition: intfeaturespace.cpp:26
kBoostDirBuckets
const int kBoostDirBuckets
Definition: intfeaturespace.h:30
tesseract::IntFeatureSpace::YBucket
int YBucket(int y) const
Definition: intfeaturespace.h:83
genericvector.h
kBoostXYBuckets
const int kBoostXYBuckets
Definition: intfeaturespace.h:29
tesseract::IntFeatureSpace::PositionFromBuckets
INT_FEATURE_STRUCT PositionFromBuckets(int x, int y, int theta) const
Definition: intfeaturespace.cpp:114
INT_FEATURE_STRUCT::Y
uint8_t Y
Definition: intproto.h:140
tesseract
Definition: baseapi.h:65
DivRounded
int DivRounded(int a, int b)
Definition: helpers.h:165
tesseract::IntFeatureSpace::theta_buckets_
uint8_t theta_buckets_
Definition: intfeaturespace.h:99
tesseract::IntFeatureSpace::PositionFromIndex
INT_FEATURE_STRUCT PositionFromIndex(int index) const
Definition: intfeaturespace.cpp:50
GenericVector< int >
tesseract::IntFeatureSpace
Definition: intfeaturespace.h:38
INT_FEATURE_STRUCT
Definition: intproto.h:131
Modulo
int Modulo(int a, int b)
Definition: helpers.h:156
intproto.h
tesseract::IntFeatureSpace::Serialize
bool Serialize(FILE *fp) const
Definition: intfeaturespace.cpp:38
INT_FEATURE_STRUCT::X
uint8_t X
Definition: intproto.h:139
tesseract::IntFeatureSpace::XYToFeatureIndex
int XYToFeatureIndex(int x, int y) const
Definition: intfeaturespace.cpp:79
kIntFeatureExtent
const int kIntFeatureExtent
Definition: intfeaturespace.h:27
tesseract::IntFeatureSpace::Index
int Index(const INT_FEATURE_STRUCT &f) const
Definition: intfeaturespace.h:60
tesseract::IntFeatureSpace::IndexFeatures
void IndexFeatures(const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *mapped_features) const
Definition: intfeaturespace.cpp:58