tesseract 4.1.1
Loading...
Searching...
No Matches
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].
27const int kIntFeatureExtent = 256;
28// Extent of x,y,theta dimensions in the quantized feature space.
29const int kBoostXYBuckets = 16;
30const int kBoostDirBuckets = 16;
31
32namespace tesseract {
33
34class 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.
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_ +
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_;
100};
101
102} // namespace tesseract.
103
104#endif // TESSERACT_CLASSIFY_INTFEATURESPACE_H_
int DivRounded(int a, int b)
Definition: helpers.h:167
int Modulo(int a, int b)
Definition: helpers.h:158
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:108
const int kIntFeatureExtent
const int kBoostXYBuckets
const int kBoostDirBuckets
void Init(uint8_t xbuckets, uint8_t ybuckets, uint8_t thetabuckets)
void IndexFeatures(const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *mapped_features) const
INT_FEATURE_STRUCT PositionFromIndex(int index) const
int Index(const INT_FEATURE_STRUCT &f) const
int XYToFeatureIndex(int x, int y) const
bool Serialize(FILE *fp) const
INT_FEATURE_STRUCT PositionFromBuckets(int x, int y, int theta) const
int ThetaBucket(int theta) const
void IndexAndSortFeatures(const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *sorted_features) const