00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00015
00016 #ifndef TESSERACT_TRAINING_TRAININGSAMPLE_H__
00017 #define TESSERACT_TRAINING_TRAININGSAMPLE_H__
00018
00019 #include "elst.h"
00020 #include "featdefs.h"
00021 #include "intfx.h"
00022 #include "intmatcher.h"
00023 #include "matrix.h"
00024 #include "mf.h"
00025 #include "picofeat.h"
00026 #include "shapetable.h"
00027 #include "unicharset.h"
00028
00029 struct Pix;
00030
00031 namespace tesseract {
00032
00033 class IntFeatureMap;
00034 class IntFeatureSpace;
00035 class ShapeTable;
00036
00037
00038 static const int kNumCNParams = 4;
00039
00040 static const int kSampleYShiftSize = 5;
00041
00042 static const int kSampleScaleSize = 3;
00043
00044
00045
00046
00047
00048
00049
00050 static const int kSampleRandomSize = kSampleYShiftSize * kSampleScaleSize - 2;
00051
00052
00053 class TrainingSample : public ELIST_LINK {
00054 public:
00055 TrainingSample()
00056 : class_id_(INVALID_UNICHAR_ID), font_id_(0), page_num_(0),
00057 num_features_(0), num_micro_features_(0),
00058 features_(NULL), micro_features_(NULL), weight_(1.0),
00059 max_dist_(0.0), sample_index_(0),
00060 features_are_indexed_(false), features_are_mapped_(false),
00061 is_error_(false) {
00062 }
00063 ~TrainingSample();
00064
00065
00066
00067 static TrainingSample* CopyFromFeatures(const INT_FX_RESULT_STRUCT& fx_info,
00068 const INT_FEATURE_STRUCT* features,
00069 int num_features);
00070
00071
00072
00073 TrainingSample* RandomizedCopy(int index) const;
00074
00075 TrainingSample* Copy() const;
00076
00077
00078
00079
00080
00081 bool Serialize(FILE* fp) const;
00082
00083
00084 static TrainingSample* DeSerializeCreate(bool swap, FILE* fp);
00085
00086
00087 bool DeSerialize(bool swap, FILE* fp);
00088
00089
00090 void ExtractCharDesc(int feature_type, int micro_type,
00091 int cn_type, int geo_type,
00092 CHAR_DESC_STRUCT* char_desc);
00093
00094
00095
00096 void IndexFeatures(const IntFeatureSpace& feature_space);
00097
00098
00099 void MapFeatures(const IntFeatureMap& feature_map);
00100
00101
00102 Pix* RenderToPix(const UNICHARSET* unicharset) const;
00103
00104 void DisplayFeatures(ScrollView::Color color, ScrollView* window) const;
00105
00106
00107
00108
00109
00110 Pix* GetSamplePix(int padding, Pix* page_pix) const;
00111
00112
00113 UNICHAR_ID class_id() const {
00114 return class_id_;
00115 }
00116 void set_class_id(int id) {
00117 class_id_ = id;
00118 }
00119 int font_id() const {
00120 return font_id_;
00121 }
00122 void set_font_id(int id) {
00123 font_id_ = id;
00124 }
00125 int page_num() const {
00126 return page_num_;
00127 }
00128 void set_page_num(int page) {
00129 page_num_ = page;
00130 }
00131 const TBOX& bounding_box() const {
00132 return bounding_box_;
00133 }
00134 void set_bounding_box(const TBOX& box) {
00135 bounding_box_ = box;
00136 }
00137 int num_features() const {
00138 return num_features_;
00139 }
00140 const INT_FEATURE_STRUCT* features() const {
00141 return features_;
00142 }
00143 int num_micro_features() const {
00144 return num_micro_features_;
00145 }
00146 const MicroFeature* micro_features() const {
00147 return micro_features_;
00148 }
00149 float cn_feature(int index) const {
00150 return cn_feature_[index];
00151 }
00152 int geo_feature(int index) const {
00153 return geo_feature_[index];
00154 }
00155 double weight() const {
00156 return weight_;
00157 }
00158 void set_weight(double value) {
00159 weight_ = value;
00160 }
00161 double max_dist() const {
00162 return max_dist_;
00163 }
00164 void set_max_dist(double value) {
00165 max_dist_ = value;
00166 }
00167 int sample_index() const {
00168 return sample_index_;
00169 }
00170 void set_sample_index(int value) {
00171 sample_index_ = value;
00172 }
00173 bool features_are_mapped() const {
00174 return features_are_mapped_;
00175 }
00176 const GenericVector<int>& mapped_features() const {
00177 ASSERT_HOST(features_are_mapped_);
00178 return mapped_features_;
00179 }
00180 const GenericVector<int>& indexed_features() const {
00181 ASSERT_HOST(features_are_indexed_);
00182 return mapped_features_;
00183 }
00184 bool is_error() const {
00185 return is_error_;
00186 }
00187 void set_is_error(bool value) {
00188 is_error_ = value;
00189 }
00190
00191 private:
00192
00193
00194 UNICHAR_ID class_id_;
00195
00196
00197 int font_id_;
00198
00199 int page_num_;
00200
00201 TBOX bounding_box_;
00202
00203 int num_features_;
00204
00205 int num_micro_features_;
00206
00207 INT_FEATURE_STRUCT* features_;
00208
00209 MicroFeature* micro_features_;
00210
00211 float cn_feature_[kNumCNParams];
00212
00213
00214 int geo_feature_[GeoCount];
00215
00216
00217
00218 double weight_;
00219
00220
00221 double max_dist_;
00222
00223 int sample_index_;
00224
00225 GenericVector<int> mapped_features_;
00226 bool features_are_indexed_;
00227 bool features_are_mapped_;
00228
00229 bool is_error_;
00230
00231
00232 static const int kYShiftValues[kSampleYShiftSize];
00233 static const double kScaleValues[kSampleScaleSize];
00234 };
00235
00236 ELISTIZEH(TrainingSample)
00237
00238 }
00239
00240 #endif // TESSERACT_TRAINING_TRAININGSAMPLE_H__