Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 40 of file feature_chebyshev.cpp.

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

Definition at line 44 of file feature_chebyshev.cpp.

44  {
45 }

Member Function Documentation

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

Definition at line 54 of file feature_chebyshev.cpp.

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

Definition at line 88 of file feature_chebyshev.cpp.

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

Implements tesseract::FeatureBase.

Definition at line 49 of file feature_chebyshev.cpp.

49  {
50  return char_samp;
51 }
bool tesseract::FeatureChebyshev::ComputeFeatures ( CharSamp samp,
float *  features 
)
virtual

Implements tesseract::FeatureBase.

Definition at line 83 of file feature_chebyshev.cpp.

83  {
84  return ComputeChebyshevCoefficients(char_samp, features);
85 }
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: