1 // Copyright 2010 Google Inc. All Rights Reserved.
2 // Author: rays@google.com (Ray Smith)
3 ///////////////////////////////////////////////////////////////////////
4 // File:        intfeaturespace.h
5 // Description: Indexed feature space based on INT_FEATURE_STRUCT.
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 ///////////////////////////////////////////////////////////////////////
18 
19 #ifndef TESSERACT_CLASSIFY_INTFEATURESPACE_H_
20 #define TESSERACT_CLASSIFY_INTFEATURESPACE_H_
21 
22 #include "intproto.h"
23 
24 // Extent of x,y,theta in the input feature space. [0,255].
25 const int kIntFeatureExtent = 256;
26 // Extent of x,y,theta dimensions in the quantized feature space.
27 const int kBoostXYBuckets = 16;
28 const int kBoostDirBuckets = 16;
29 
30 namespace tesseract {
31 
32 class IndexMap;
33 
34 // Down-sampling quantization of the INT_FEATURE_STRUCT feature space and
35 // conversion to a single scalar index value, used as a binary feature space.
36 class TESS_API IntFeatureSpace {
37 public:
38   IntFeatureSpace();
39   // Default copy constructors and assignment OK!
40 
41   // Setup the feature space with the given dimensions.
42   void Init(uint8_t xbuckets, uint8_t ybuckets, uint8_t thetabuckets);
43 
44   // Serializes the feature space definition to the given file.
45   // Returns false on error.
46   bool Serialize(FILE *fp) const;
47 
48   // Returns the total size of the feature space.
Size()49   int Size() const {
50     return static_cast<int>(x_buckets_) * y_buckets_ * theta_buckets_;
51   }
52   // Returns an INT_FEATURE_STRUCT corresponding to the given index.
53   // This is the inverse of the Index member.
54   INT_FEATURE_STRUCT PositionFromIndex(int index) const;
55 
56   // Returns a 1-dimensional index corresponding to the given feature value.
57   // Range is [0, Size()-1]. Inverse of PositionFromIndex member.
Index(const INT_FEATURE_STRUCT & f)58   int Index(const INT_FEATURE_STRUCT &f) const {
59     return (XBucket(f.X) * y_buckets_ + YBucket(f.Y)) * theta_buckets_ + ThetaBucket(f.Theta);
60   }
61   // Bulk calls to Index. Maps the given array of features to a vector of
62   // int32_t indices in the same order as the input.
63   void IndexFeatures(const INT_FEATURE_STRUCT *features, int num_features,
64                      std::vector<int> *mapped_features) const;
65   // Bulk calls to Index. Maps the given array of features to a vector of
66   // sorted int32_t indices.
67   void IndexAndSortFeatures(const INT_FEATURE_STRUCT *features, int num_features,
68                             std::vector<int> *sorted_features) const;
69   // Returns a feature space index for the given x,y position in a display
70   // window, or -1 if the feature is a miss.
71   int XYToFeatureIndex(int x, int y) const;
72 
73 protected:
74   // Converters to generate indices for individual feature dimensions.
XBucket(int x)75   int XBucket(int x) const {
76     int bucket = x * x_buckets_ / kIntFeatureExtent;
77     return ClipToRange(bucket, 0, static_cast<int>(x_buckets_) - 1);
78   }
YBucket(int y)79   int YBucket(int y) const {
80     int bucket = y * y_buckets_ / kIntFeatureExtent;
81     return ClipToRange(bucket, 0, static_cast<int>(y_buckets_) - 1);
82   }
83   // Use DivRounded for theta so that exactly vertical and horizontal are in
84   // the middle of a bucket. The Modulo takes care of the wrap-around.
ThetaBucket(int theta)85   int ThetaBucket(int theta) const {
86     int bucket = DivRounded(theta * theta_buckets_, kIntFeatureExtent);
87     return Modulo(bucket, theta_buckets_);
88   }
89   // Returns an INT_FEATURE_STRUCT corresponding to the given buckets.
90   INT_FEATURE_STRUCT PositionFromBuckets(int x, int y, int theta) const;
91 
92   // Feature space definition - serialized.
93   uint8_t x_buckets_;
94   uint8_t y_buckets_;
95   uint8_t theta_buckets_;
96 };
97 
98 } // namespace tesseract.
99 
100 #endif // TESSERACT_CLASSIFY_INTFEATURESPACE_H_
101