Tesseract  3.02
tesseract-ocr/classify/intfeaturespace.h
Go to the documentation of this file.
00001 // Copyright 2010 Google Inc. All Rights Reserved.
00002 // Author: rays@google.com (Ray Smith)
00004 // File:        intfeaturespace.h
00005 // Description: Indexed feature space based on INT_FEATURE_STRUCT.
00006 // Created:     Wed Mar 24 10:55:30 PDT 2010
00007 //
00008 // Licensed under the Apache License, Version 2.0 (the "License");
00009 // you may not use this file except in compliance with the License.
00010 // You may obtain a copy of the License at
00011 // http://www.apache.org/licenses/LICENSE-2.0
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 //
00019 
00020 #ifndef TESSERACT_CLASSIFY_INTFEATURESPACE_H__
00021 #define TESSERACT_CLASSIFY_INTFEATURESPACE_H__
00022 
00023 #include "genericvector.h"
00024 #include "intproto.h"
00025 
00026 // Extent of x,y,theta in the input feature space. [0,255].
00027 const int kIntFeatureExtent = 256;
00028 // Extent of x,y,theta dimensions in the quantized feature space.
00029 const int kBoostXYBuckets = 16;
00030 const int kBoostDirBuckets = 16;
00031 
00032 namespace tesseract {
00033 
00034 class IndexMap;
00035 
00036 // Down-sampling quantization of the INT_FEATURE_STRUCT feature space and
00037 // conversion to a single scalar index value, used as a binary feature space.
00038 class IntFeatureSpace {
00039  public:
00040   IntFeatureSpace();
00041   // Default copy constructors and assignment OK!
00042 
00043   // Setup the feature space with the given dimensions.
00044   void Init(uinT8 xbuckets, uinT8 ybuckets, uinT8 thetabuckets);
00045 
00046   // Serializes the feature space definition to the given file.
00047   // Returns false on error.
00048   bool Serialize(FILE* fp) const;
00049 
00050   // DeSerializes the feature space definition from the given file.
00051   // If swap is true, the data is big/little-endian swapped.
00052   // Returns false on error.
00053   bool DeSerialize(bool swap, FILE* fp);
00054 
00055   // Returns the total size of the feature space.
00056   int Size() const {
00057     return static_cast<int>(x_buckets_) * y_buckets_ * theta_buckets_;
00058   }
00059   // Returns an INT_FEATURE_STRUCT corresponding to the given index.
00060   // This is the inverse of the Index member.
00061   INT_FEATURE_STRUCT PositionFromIndex(int index) const;
00062 
00063   // Returns a 1-dimensional index corresponding to the given feature value.
00064   // Range is [0, Size()-1]. Inverse of PositionFromIndex member.
00065   int Index(const INT_FEATURE_STRUCT& f) const {
00066     return (XBucket(f.X) * y_buckets_ + YBucket(f.Y)) * theta_buckets_ +
00067         ThetaBucket(f.Theta);
00068   }
00069   // Bulk calls to Index. Maps the given array of features to a vector of
00070   // inT32 indices in the same order as the input.
00071   void IndexFeatures(const INT_FEATURE_STRUCT* features, int num_features,
00072                      GenericVector<int>* mapped_features) const;
00073   // Bulk calls to Index. Maps the given array of features to a vector of
00074   // sorted inT32 indices.
00075   void IndexAndSortFeatures(const INT_FEATURE_STRUCT* features,
00076                             int num_features,
00077                             GenericVector<int>* sorted_features) const;
00078   // Returns a feature space index for the given x,y position in a display
00079   // window, or -1 if the feature is a miss.
00080   int XYToFeatureIndex(int x, int y) const;
00081 
00082  protected:
00083   // Converters to generate indices for individual feature dimensions.
00084   int XBucket(int x) const {
00085     int bucket = x * x_buckets_ / kIntFeatureExtent;
00086     return ClipToRange(bucket, 0, static_cast<int>(x_buckets_) - 1);
00087   }
00088   int YBucket(int y) const {
00089     int bucket = y * y_buckets_ / kIntFeatureExtent;
00090     return ClipToRange(bucket, 0, static_cast<int>(y_buckets_) - 1);
00091   }
00092   // Use DivRounded for theta so that exactly vertical and horizontal are in
00093   // the middle of a bucket. The Modulo takes care of the wrap-around.
00094   int ThetaBucket(int theta) const {
00095     int bucket = DivRounded(theta * theta_buckets_, kIntFeatureExtent);
00096     return Modulo(bucket, theta_buckets_);
00097   }
00098   // Returns an INT_FEATURE_STRUCT corresponding to the given buckets.
00099   INT_FEATURE_STRUCT PositionFromBuckets(int x, int y, int theta) const;
00100 
00101   // Feature space definition - serialized.
00102   uinT8 x_buckets_;
00103   uinT8 y_buckets_;
00104   uinT8 theta_buckets_;
00105 };
00106 
00107 }  // namespace tesseract.
00108 
00109 
00110 #endif  // TESSERACT_CLASSIFY_INTFEATURESPACE_H__