All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::FeatureChebyshev Class Reference

#include <feature_chebyshev.h>

Inheritance diagram for tesseract::FeatureChebyshev:
tesseract::FeatureBase

Public Member Functions

 FeatureChebyshev (TuningParams *params)
 
virtual ~FeatureChebyshev ()
 
virtual CharSampComputeFeatureBitmap (CharSamp *samp)
 
virtual bool ComputeFeatures (CharSamp *samp, float *features)
 
virtual int FeatureCnt ()
 
- Public Member Functions inherited from tesseract::FeatureBase
 FeatureBase (TuningParams *params)
 
virtual ~FeatureBase ()
 

Protected Member Functions

void ChebyshevCoefficients (const vector< float > &input, int coeff_cnt, float *coeff)
 
bool ComputeChebyshevCoefficients (CharSamp *samp, float *features)
 

Static Protected Attributes

static const int kChebychevCoefficientCnt = 40
 

Additional Inherited Members

- Protected Attributes inherited from tesseract::FeatureBase
TuningParamsparams_
 

Detailed Description

Definition at line 33 of file feature_chebyshev.h.

Constructor & Destructor Documentation

tesseract::FeatureChebyshev::FeatureChebyshev ( TuningParams params)
explicit

Definition at line 34 of file feature_chebyshev.cpp.

35  : FeatureBase(params) {
36 }
FeatureBase(TuningParams *params)
Definition: feature_base.h:36
tesseract::FeatureChebyshev::~FeatureChebyshev ( )
virtual

Definition at line 38 of file feature_chebyshev.cpp.

38  {
39 }

Member Function Documentation

void tesseract::FeatureChebyshev::ChebyshevCoefficients ( const vector< float > &  input,
int  coeff_cnt,
float *  coeff 
)
protected

Definition at line 48 of file feature_chebyshev.cpp.

49  {
50  // re-sample function
51  int input_range = (input.size() - 1);
52  vector<float> resamp(coeff_cnt);
53  for (int samp_idx = 0; samp_idx < coeff_cnt; samp_idx++) {
54  // compute sampling position
55  float samp_pos = input_range *
56  (1 + cos(M_PI * (samp_idx + 0.5) / coeff_cnt)) / 2;
57  // interpolate
58  int samp_start = static_cast<int>(samp_pos);
59  int samp_end = static_cast<int>(samp_pos + 0.5);
60  float func_delta = input[samp_end] - input[samp_start];
61  resamp[samp_idx] = input[samp_start] +
62  ((samp_pos - samp_start) * func_delta);
63  }
64  // compute the coefficients
65  float normalizer = 2.0 / coeff_cnt;
66  for (int coeff_idx = 0; coeff_idx < coeff_cnt; coeff_idx++, coeff++) {
67  double sum = 0.0;
68  for (int samp_idx = 0; samp_idx < coeff_cnt; samp_idx++) {
69  sum += resamp[samp_idx] * cos(M_PI * coeff_idx * (samp_idx + 0.5) /
70  coeff_cnt);
71  }
72  (*coeff) = (normalizer * sum);
73  }
74 }
bool tesseract::FeatureChebyshev::ComputeChebyshevCoefficients ( CharSamp samp,
float *  features 
)
protected

Definition at line 82 of file feature_chebyshev.cpp.

83  {
84  if (char_samp->NormBottom() <= 0) {
85  return false;
86  }
87  unsigned char *raw_data = char_samp->RawData();
88  int stride = char_samp->Stride();
89  // compute the height of the word
90  int word_hgt = (255 * (char_samp->Top() + char_samp->Height()) /
91  char_samp->NormBottom());
92  // compute left & right profiles
93  vector<float> left_profile(word_hgt, 0.0);
94  vector<float> right_profile(word_hgt, 0.0);
95  unsigned char *line_data = raw_data;
96  for (int y = 0; y < char_samp->Height(); y++, line_data += stride) {
97  int min_x = char_samp->Width();
98  int max_x = -1;
99  for (int x = 0; x < char_samp->Width(); x++) {
100  if (line_data[x] == 0) {
101  UpdateRange(x, &min_x, &max_x);
102  }
103  }
104  left_profile[char_samp->Top() + y] =
105  1.0 * (min_x == char_samp->Width() ? 0 : (min_x + 1)) /
106  char_samp->Width();
107  right_profile[char_samp->Top() + y] =
108  1.0 * (max_x == -1 ? 0 : char_samp->Width() - max_x) /
109  char_samp->Width();
110  }
111 
112  // compute top and bottom profiles
113  vector<float> top_profile(char_samp->Width(), 0);
114  vector<float> bottom_profile(char_samp->Width(), 0);
115  for (int x = 0; x < char_samp->Width(); x++) {
116  int min_y = word_hgt;
117  int max_y = -1;
118  line_data = raw_data;
119  for (int y = 0; y < char_samp->Height(); y++, line_data += stride) {
120  if (line_data[x] == 0) {
121  UpdateRange(y + char_samp->Top(), &min_y, &max_y);
122  }
123  }
124  top_profile[x] = 1.0 * (min_y == word_hgt ? 0 : (min_y + 1)) / word_hgt;
125  bottom_profile[x] = 1.0 * (max_y == -1 ? 0 : (word_hgt - max_y)) / word_hgt;
126  }
127 
128  // compute the chebyshev coefficients of each profile
129  ChebyshevCoefficients(left_profile, kChebychevCoefficientCnt, features);
131  features + kChebychevCoefficientCnt);
133  features + (2 * kChebychevCoefficientCnt));
135  features + (3 * kChebychevCoefficientCnt));
136  return true;
137 }
static const int kChebychevCoefficientCnt
void ChebyshevCoefficients(const vector< float > &input, int coeff_cnt, float *coeff)
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
Definition: helpers.h:125
CharSamp * tesseract::FeatureChebyshev::ComputeFeatureBitmap ( CharSamp samp)
virtual

Implements tesseract::FeatureBase.

Definition at line 43 of file feature_chebyshev.cpp.

43  {
44  return char_samp;
45 }
bool tesseract::FeatureChebyshev::ComputeFeatures ( CharSamp samp,
float *  features 
)
virtual

Implements tesseract::FeatureBase.

Definition at line 77 of file feature_chebyshev.cpp.

77  {
78  return ComputeChebyshevCoefficients(char_samp, features);
79 }
bool ComputeChebyshevCoefficients(CharSamp *samp, float *features)
virtual int tesseract::FeatureChebyshev::FeatureCnt ( )
inlinevirtual

Implements tesseract::FeatureBase.

Definition at line 43 of file feature_chebyshev.h.

43  {
44  return (4 * kChebychevCoefficientCnt);
45  }
static const int kChebychevCoefficientCnt

Member Data Documentation

const int tesseract::FeatureChebyshev::kChebychevCoefficientCnt = 40
staticprotected

Definition at line 48 of file feature_chebyshev.h.


The documentation for this class was generated from the following files: