Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
featdefs.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Filename: featdefs.c
3 ** Purpose: Definitions of currently defined feature types.
4 ** Author: Dan Johnson
5 ** History: Mon May 21 10:26:21 1990, DSJ, Created.
6 **
7 ** (c) Copyright Hewlett-Packard Company, 1988.
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 ******************************************************************************/
18 /*-----------------------------------------------------------------------------
19 Include Files and Type Defines
20 -----------------------------------------------------------------------------*/
21 #ifdef _MSC_VER
22 #include "mathfix.h"
23 #endif
24
25 #include "featdefs.h"
26 #include "emalloc.h"
27 #include "danerror.h"
28 #include "scanutils.h"
29
30 #include <string.h>
31 #include <stdio.h>
32
34 #define ILLEGAL_NUM_SETS 3001
35
36 #define PICO_FEATURE_LENGTH 0.05
37
38 /*-----------------------------------------------------------------------------
39 Global Data Definitions and Declarations
40 -----------------------------------------------------------------------------*/
41 const char* kMicroFeatureType = "mf";
42 const char* kCNFeatureType = "cn";
43 const char* kIntFeatureType = "if";
44 const char* kGeoFeatureType = "tb";
45
46 // Define all of the parameters for the MicroFeature type.
47 StartParamDesc(MicroFeatureParams)
48 DefineParam(0, 0, -0.5, 0.5)
49 DefineParam(0, 0, -0.25, 0.75)
50 DefineParam(0, 1, 0.0, 1.0)
51 DefineParam(1, 0, 0.0, 1.0)
52 DefineParam (0, 1, -0.5, 0.5)
53 DefineParam (0, 1, -0.5, 0.5)
54 EndParamDesc
55 // Now define the feature type itself (see features.h for parameters).
56 DefineFeature(MicroFeatureDesc, 5, 1, kMicroFeatureType, MicroFeatureParams)
57
58 // Define all of the parameters for the NormFeat type.
59 StartParamDesc (CharNormParams)
60 DefineParam(0, 0, -0.25, 0.75)
61 DefineParam(0, 1, 0.0, 1.0)
62 DefineParam(0, 0, 0.0, 1.0)
63 DefineParam(0, 0, 0.0, 1.0)
64 EndParamDesc
65 // Now define the feature type itself (see features.h for parameters).
66 DefineFeature(CharNormDesc, 4, 0, kCNFeatureType, CharNormParams)
67
68 // Define all of the parameters for the IntFeature type
69 StartParamDesc(IntFeatParams)
70 DefineParam(0, 0, 0.0, 255.0)
71 DefineParam(0, 0, 0.0, 255.0)
72 DefineParam(1, 0, 0.0, 255.0)
73 EndParamDesc
74 // Now define the feature type itself (see features.h for parameters).
75 DefineFeature(IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams)
76
77 // Define all of the parameters for the GeoFeature type
78 StartParamDesc(GeoFeatParams)
79 DefineParam(0, 0, 0.0, 255.0)
80 DefineParam(0, 0, 0.0, 255.0)
81 DefineParam(0, 0, 0.0, 255.0)
82 EndParamDesc
83 // Now define the feature type itself (see features.h for parameters).
84 DefineFeature(GeoFeatDesc, 3, 0, kGeoFeatureType, GeoFeatParams)
85
86 // Other features used for training the adaptive classifier, but not used
87 // during normal training, therefore not in the DescDefs array.
88
89 // Define all of the parameters for the PicoFeature type
90 // define knob that can be used to adjust pico-feature length.
91 FLOAT32 PicoFeatureLength = PICO_FEATURE_LENGTH;
92 StartParamDesc(PicoFeatParams)
93 DefineParam(0, 0, -0.25, 0.75)
94 DefineParam(1, 0, 0.0, 1.0)
95 DefineParam(0, 0, -0.5, 0.5)
96 EndParamDesc
97 // Now define the feature type itself (see features.h for parameters).
98 DefineFeature(PicoFeatDesc, 2, 1, "pf", PicoFeatParams)
99
100 // Define all of the parameters for the OutlineFeature type.
101 StartParamDesc(OutlineFeatParams)
102 DefineParam(0, 0, -0.5, 0.5)
103 DefineParam(0, 0, -0.25, 0.75)
104 DefineParam(0, 0, 0.0, 1.0)
105 DefineParam(1, 0, 0.0, 1.0)
106 EndParamDesc
107 // Now define the feature type itself (see features.h for parameters).
108 DefineFeature(OutlineFeatDesc, 3, 1, "of", OutlineFeatParams)
109
110 // MUST be kept in-sync with ExtractorDefs in fxdefs.cpp.
111 static const FEATURE_DESC_STRUCT *DescDefs[NUM_FEATURE_TYPES] = {
112 &MicroFeatureDesc,
113 &CharNormDesc,
114 &IntFeatDesc,
115 &GeoFeatDesc
116 };
117
118 /*-----------------------------------------------------------------------------
119 Public Code
120 -----------------------------------------------------------------------------*/
121 void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs) {
122 featuredefs->NumFeatureTypes = NUM_FEATURE_TYPES;
123 for (int i = 0; i < NUM_FEATURE_TYPES; ++i) {
124 featuredefs->FeatureDesc[i] = DescDefs[i];
125 }
126 }
127
128 /*---------------------------------------------------------------------------*/
141 void FreeCharDescription(CHAR_DESC CharDesc) {
142 int i;
143
144 if (CharDesc) {
145 for (i = 0; i < CharDesc->NumFeatureSets; i++)
146 FreeFeatureSet (CharDesc->FeatureSets[i]);
147 Efree(CharDesc);
148 }
149 } /* FreeCharDescription */
150
151
152 /*---------------------------------------------------------------------------*/
164 CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) {
165 CHAR_DESC CharDesc;
166 int i;
167
168 CharDesc = (CHAR_DESC) Emalloc (sizeof (CHAR_DESC_STRUCT));
169 CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes;
170
171 for (i = 0; i < CharDesc->NumFeatureSets; i++)
172 CharDesc->FeatureSets[i] = NULL;
173
174 return (CharDesc);
175
176 } /* NewCharDescription */
177
178
179 /*---------------------------------------------------------------------------*/
200 void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
201 FILE *File, CHAR_DESC CharDesc) {
202 int Type;
203 int NumSetsToWrite = 0;
204
205 for (Type = 0; Type < CharDesc->NumFeatureSets; Type++)
206 if (CharDesc->FeatureSets[Type])
207 NumSetsToWrite++;
208
209 fprintf (File, " %d\n", NumSetsToWrite);
210 for (Type = 0; Type < CharDesc->NumFeatureSets; Type++)
211 if (CharDesc->FeatureSets[Type]) {
212 fprintf (File, "%s ", (FeatureDefs.FeatureDesc[Type])->ShortName);
213 WriteFeatureSet (File, CharDesc->FeatureSets[Type]);
214 }
215 } /* WriteCharDescription */
216
217 // Return whether all of the fields of the given feature set
218 // are well defined (not inf or nan).
219 bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
220 CHAR_DESC CharDesc) {
221 bool anything_written = false;
222 bool well_formed = true;
223 for (int Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
224 if (CharDesc->FeatureSets[Type]) {
225 for (int i = 0; i < CharDesc->FeatureSets[Type]->NumFeatures; i++) {
226 FEATURE feat = CharDesc->FeatureSets[Type]->Features[i];
227 for (int p = 0; p < feat->Type->NumParams; p++) {
228 if (isnan(feat->Params[p]) || isinf(feat->Params[p]))
229 well_formed = false;
230 else
231 anything_written = true;
232 }
233 }
234 }
235 }
236 return anything_written && well_formed;
237 } /* ValidCharDescription */
238
239 /*---------------------------------------------------------------------------*/
261 CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
262 FILE *File) {
263 int NumSetsToRead;
264 char ShortName[FEAT_NAME_SIZE];
265 CHAR_DESC CharDesc;
266 int Type;
267
268 if (fscanf (File, "%d", &NumSetsToRead) != 1 ||
269 NumSetsToRead < 0 || NumSetsToRead > FeatureDefs.NumFeatureTypes)
270 DoError (ILLEGAL_NUM_SETS, "Illegal number of feature sets");
271
272 CharDesc = NewCharDescription(FeatureDefs);
273 for (; NumSetsToRead > 0; NumSetsToRead--) {
274 fscanf (File, "%s", ShortName);
275 Type = ShortNameToFeatureType(FeatureDefs, ShortName);
276 CharDesc->FeatureSets[Type] =
277 ReadFeatureSet (File, FeatureDefs.FeatureDesc[Type]);
278 }
279 return (CharDesc);
280
281 } // ReadCharDescription
282
283
284 /*---------------------------------------------------------------------------*/
300 int ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs,
301 const char *ShortName) {
302 int i;
303
304 for (i = 0; i < FeatureDefs.NumFeatureTypes; i++)
305 if (!strcmp ((FeatureDefs.FeatureDesc[i]->ShortName), ShortName))
306 return (i);
307 DoError (ILLEGAL_SHORT_NAME, "Illegal short name for a feature");
308 return 0;
309
310 } // ShortNameToFeatureType