1 /********************************************************************** 2 * File: charsample.h (Formerly charsample.h) 3 * Description: Class to contain character samples and match scores 4 * to be used for adaption 5 * Author: Chris Newton 6 * Created: Thu Oct 7 13:40:37 BST 1993 7 * 8 * (C) Copyright 1993, Hewlett-Packard Ltd. 9 ** Licensed under the Apache License, Version 2.0 (the "License"); 10 ** you may not use this file except in compliance with the License. 11 ** You may obtain a copy of the License at 12 ** http://www.apache.org/licenses/LICENSE-2.0 13 ** Unless required by applicable law or agreed to in writing, software 14 ** distributed under the License is distributed on an "AS IS" BASIS, 15 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 ** See the License for the specific language governing permissions and 17 ** limitations under the License. 18 * 19 **********************************************************************/ 20 21 #ifndef CHARSAMPLE_H 22 #define CHARSAMPLE_H 23 24 #include "elst.h" 25 #include "pageres.h" 26 #include "memry.h" 27 #include "notdll.h" 28 29 #define BAD_SCORE MAX_INT32 30 #define FIRST_CHAR '!' 31 #define LAST_CHAR '~' 32 33 namespace tesseract { 34 class Tesseract; // Fwd decl. 35 } 36 37 enum ClusterType 38 { UNKNOWN, BLOB_CLUSTER, IMAGE_CLUSTER }; 39 40 class CHAR_SAMPLE; //forward decl 41 42 ELISTIZEH (CHAR_SAMPLE) 43 class CHAR_SAMPLES; //forward decl 44 45 ELISTIZEH (CHAR_SAMPLES) 46 class CHAR_PROTO; //forward decl 47 48 class CHAR_SAMPLE:public ELIST_LINK 49 { 50 public: 51 CHAR_SAMPLE(); // empty constructor 52 53 CHAR_SAMPLE( // simple constructor 54 PBLOB *blob, 55 DENORM *denorm, 56 char c 57 ); 58 59 CHAR_SAMPLE( // simple constructor 60 IMAGE *image, 61 char c 62 ); 63 ~CHAR_SAMPLE()64 ~CHAR_SAMPLE () { 65 // We own the image, so it has to be deleted. 66 if (sample_image != NULL) 67 delete sample_image; 68 } 69 70 float match_sample(CHAR_SAMPLE *test_sample, BOOL8 updating, 71 tesseract::Tesseract* tess); 72 n_matches()73 inT32 n_matches() { 74 return n_samples_matched; 75 } 76 image()77 IMAGE *image() { 78 return sample_image; 79 } 80 blob()81 PBLOB *blob() { 82 return sample_blob; 83 } 84 denorm()85 DENORM *denorm() { 86 return sample_denorm; 87 } 88 89 double mean_score(); 90 91 double variance(); 92 character()93 char character() { 94 return ch; 95 } 96 97 void print(FILE *f); 98 99 void reset_match_statistics(); 100 101 NEWDELETE2 (CHAR_SAMPLE) private: 102 IMAGE * sample_image; 103 PBLOB *sample_blob; 104 DENORM *sample_denorm; 105 inT32 n_samples_matched; 106 double total_match_scores; 107 double sumsq_match_scores; 108 char ch; 109 }; 110 111 class CHAR_SAMPLES:public ELIST_LINK 112 { 113 public: 114 CHAR_SAMPLES(); //empty constructor 115 116 CHAR_SAMPLES(CHAR_SAMPLE *sample); 117 ~CHAR_SAMPLES()118 ~CHAR_SAMPLES () { //destructor 119 } 120 n_samples()121 inT32 n_samples() { 122 return samples.length (); 123 } 124 125 void add_sample(CHAR_SAMPLE *sample, tesseract::Tesseract*); 126 127 void build_prototype(); 128 129 void rebuild_prototype(inT32 new_xsize, inT32 new_ysize); 130 131 void add_sample_to_prototype(CHAR_SAMPLE *sample); 132 prototype()133 CHAR_PROTO *prototype() { 134 return proto; 135 } 136 137 void find_best_sample(); 138 139 float match_score(CHAR_SAMPLE *sample, tesseract::Tesseract* tess); 140 141 float nn_match_score(CHAR_SAMPLE *sample, tesseract::Tesseract* tess); 142 character()143 char character() { 144 return ch; 145 } 146 147 void assign_to_char(); 148 149 void print(FILE *f); 150 151 NEWDELETE2 (CHAR_SAMPLES) private: 152 ClusterType type; 153 char ch; 154 CHAR_PROTO *proto; 155 CHAR_SAMPLE *best_sample; 156 CHAR_SAMPLE_LIST samples; 157 }; 158 159 class CHAR_PROTO 160 { 161 public: 162 CHAR_PROTO(); // empty constructor 163 164 CHAR_PROTO(inT32 x_size, 165 inT32 y_size, 166 inT32 n_samples, 167 float initial_value, 168 char c); 169 170 CHAR_PROTO( // simple constructor 171 CHAR_SAMPLE *sample); 172 173 ~CHAR_PROTO (); 174 175 float match_sample(CHAR_SAMPLE *test_sample); 176 177 float match(CHAR_PROTO *test_proto); 178 n_samples()179 inT32 n_samples() { 180 return nsamples; 181 } 182 x_size()183 inT32 x_size() { 184 return xsize; 185 } 186 y_size()187 inT32 y_size() { 188 return ysize; 189 } 190 data()191 float **data() { 192 return proto; 193 } character()194 char character() { 195 return ch; 196 } 197 198 void enlarge_prototype(inT32 new_xsize, inT32 new_ysize); 199 200 void add_sample(CHAR_SAMPLE *sample); 201 202 IMAGE *make_image(); 203 204 void print(FILE *f); 205 206 NEWDELETE2 (CHAR_PROTO) private: 207 inT32 xsize; 208 inT32 ysize; 209 float *proto_data; 210 float **proto; 211 inT32 nsamples; 212 char ch; 213 }; 214 #endif 215