tesseract 4.1.1
Loading...
Searching...
No Matches
featdefs.cpp
Go to the documentation of this file.
1/******************************************************************************
2 ** Filename: featdefs.cpp
3 ** Purpose: Definitions of currently defined feature types.
4 ** Author: Dan Johnson
5 **
6 ** (c) Copyright Hewlett-Packard Company, 1988.
7 ** Licensed under the Apache License, Version 2.0 (the "License");
8 ** you may not use this file except in compliance with the License.
9 ** You may obtain a copy of the License at
10 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 ******************************************************************************/
17/*-----------------------------------------------------------------------------
18 Include Files and Type Defines
19-----------------------------------------------------------------------------*/
20#include "featdefs.h"
21#include <cstring>
22#include <cstdio>
23#include "emalloc.h"
24#include "picofeat.h" // for PicoFeatureLength
25#include "scanutils.h"
26
27#define PICO_FEATURE_LENGTH 0.05
28
29/*-----------------------------------------------------------------------------
30 Global Data Definitions and Declarations
31-----------------------------------------------------------------------------*/
32const char* const kMicroFeatureType = "mf";
33const char* const kCNFeatureType = "cn";
34const char* const kIntFeatureType = "if";
35const char* const kGeoFeatureType = "tb";
36
37// Define all of the parameters for the MicroFeature type.
38StartParamDesc(MicroFeatureParams)
39DefineParam(0, 0, -0.5, 0.5)
40DefineParam(0, 0, -0.25, 0.75)
41DefineParam(0, 1, 0.0, 1.0)
42DefineParam(1, 0, 0.0, 1.0)
43DefineParam (0, 1, -0.5, 0.5)
44DefineParam (0, 1, -0.5, 0.5)
46// Now define the feature type itself (see features.h for parameters).
47DefineFeature(MicroFeatureDesc, 5, 1, kMicroFeatureType, MicroFeatureParams)
48
49// Define all of the parameters for the NormFeat type.
50StartParamDesc (CharNormParams)
51DefineParam(0, 0, -0.25, 0.75)
52DefineParam(0, 1, 0.0, 1.0)
53DefineParam(0, 0, 0.0, 1.0)
54DefineParam(0, 0, 0.0, 1.0)
56// Now define the feature type itself (see features.h for parameters).
57DefineFeature(CharNormDesc, 4, 0, kCNFeatureType, CharNormParams)
58
59// Define all of the parameters for the IntFeature type
60StartParamDesc(IntFeatParams)
61DefineParam(0, 0, 0.0, 255.0)
62DefineParam(0, 0, 0.0, 255.0)
63DefineParam(1, 0, 0.0, 255.0)
65// Now define the feature type itself (see features.h for parameters).
67
68// Define all of the parameters for the GeoFeature type
69StartParamDesc(GeoFeatParams)
70DefineParam(0, 0, 0.0, 255.0)
71DefineParam(0, 0, 0.0, 255.0)
72DefineParam(0, 0, 0.0, 255.0)
74// Now define the feature type itself (see features.h for parameters).
76
77// Other features used for training the adaptive classifier, but not used
78// during normal training, therefore not in the DescDefs array.
79
80// Define all of the parameters for the PicoFeature type
81// define knob that can be used to adjust pico-feature length.
83StartParamDesc(PicoFeatParams)
84DefineParam(0, 0, -0.25, 0.75)
85DefineParam(1, 0, 0.0, 1.0)
86DefineParam(0, 0, -0.5, 0.5)
88// Now define the feature type itself (see features.h for parameters).
89DefineFeature(PicoFeatDesc, 2, 1, "pf", PicoFeatParams)
90
91// Define all of the parameters for the OutlineFeature type.
92StartParamDesc(OutlineFeatParams)
93DefineParam(0, 0, -0.5, 0.5)
94DefineParam(0, 0, -0.25, 0.75)
95DefineParam(0, 0, 0.0, 1.0)
96DefineParam(1, 0, 0.0, 1.0)
98// Now define the feature type itself (see features.h for parameters).
99DefineFeature(OutlineFeatDesc, 3, 1, "of", OutlineFeatParams)
100
101// MUST be kept in-sync with ExtractorDefs in fxdefs.cpp.
102static const FEATURE_DESC_STRUCT *DescDefs[NUM_FEATURE_TYPES] = {
107};
108
109/*-----------------------------------------------------------------------------
110 Public Code
111-----------------------------------------------------------------------------*/
113 featuredefs->NumFeatureTypes = NUM_FEATURE_TYPES;
114 for (int i = 0; i < NUM_FEATURE_TYPES; ++i) {
115 featuredefs->FeatureDesc[i] = DescDefs[i];
116 }
117}
118
119/*---------------------------------------------------------------------------*/
130 if (CharDesc) {
131 for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
132 FreeFeatureSet (CharDesc->FeatureSets[i]);
133 Efree(CharDesc);
134 }
135} /* FreeCharDescription */
136
137
138/*---------------------------------------------------------------------------*/
149 CHAR_DESC CharDesc;
150 CharDesc = static_cast<CHAR_DESC>(Emalloc (sizeof (CHAR_DESC_STRUCT)));
151 CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes;
152
153 for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
154 CharDesc->FeatureSets[i] = nullptr;
155
156 return (CharDesc);
157} /* NewCharDescription */
158
159/*---------------------------------------------------------------------------*/
175 CHAR_DESC CharDesc, STRING* str) {
176 int NumSetsToWrite = 0;
177
178 for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++)
179 if (CharDesc->FeatureSets[Type])
180 NumSetsToWrite++;
181
182 str->add_str_int(" ", NumSetsToWrite);
183 *str += "\n";
184 for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
185 if (CharDesc->FeatureSets[Type]) {
186 *str += FeatureDefs.FeatureDesc[Type]->ShortName;
187 *str += " ";
188 WriteFeatureSet(CharDesc->FeatureSets[Type], str);
189 }
190 }
191} /* WriteCharDescription */
192
193// Return whether all of the fields of the given feature set
194// are well defined (not inf or nan).
196 CHAR_DESC CharDesc) {
197 bool anything_written = false;
198 bool well_formed = true;
199 for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
200 if (CharDesc->FeatureSets[Type]) {
201 for (int i = 0; i < CharDesc->FeatureSets[Type]->NumFeatures; i++) {
202 FEATURE feat = CharDesc->FeatureSets[Type]->Features[i];
203 for (int p = 0; p < feat->Type->NumParams; p++) {
204 if (std::isnan(feat->Params[p]) || std::isinf(feat->Params[p]))
205 well_formed = false;
206 else
207 anything_written = true;
208 }
209 }
210 } else {
211 return false;
212 }
213 }
214 return anything_written && well_formed;
215} /* ValidCharDescription */
216
217/*---------------------------------------------------------------------------*/
237 FILE *File) {
238 int NumSetsToRead;
239 char ShortName[FEAT_NAME_SIZE];
240 CHAR_DESC CharDesc;
241 int Type;
242
243 ASSERT_HOST(tfscanf(File, "%d", &NumSetsToRead) == 1);
244 ASSERT_HOST(NumSetsToRead >= 0);
245 ASSERT_HOST(NumSetsToRead <= FeatureDefs.NumFeatureTypes);
246
247 CharDesc = NewCharDescription(FeatureDefs);
248 for (; NumSetsToRead > 0; NumSetsToRead--) {
249 tfscanf(File, "%s", ShortName);
250 Type = ShortNameToFeatureType(FeatureDefs, ShortName);
251 CharDesc->FeatureSets[Type] =
252 ReadFeatureSet (File, FeatureDefs.FeatureDesc[Type]);
253 }
254 return CharDesc;
255}
256
257/*---------------------------------------------------------------------------*/
270uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs,
271 const char *ShortName) {
272 for (int i = 0; i < FeatureDefs.NumFeatureTypes; i++)
273 if (!strcmp ((FeatureDefs.FeatureDesc[i]->ShortName), ShortName))
274 return static_cast<uint32_t>(i);
275 ASSERT_HOST(!"Illegal short name for a feature");
276 return 0;
277}
#define ASSERT_HOST(x)
Definition: errcode.h:88
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:181
const char *const kIntFeatureType
Definition: featdefs.cpp:34
const char *const kGeoFeatureType
Definition: featdefs.cpp:35
const char *const kCNFeatureType
Definition: featdefs.cpp:33
EndParamDesc of
Definition: featdefs.cpp:99
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
Definition: featdefs.cpp:112
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, STRING *str)
Definition: featdefs.cpp:174
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs)
Definition: featdefs.cpp:148
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
Definition: featdefs.cpp:270
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File)
Definition: featdefs.cpp:236
void FreeCharDescription(CHAR_DESC CharDesc)
Definition: featdefs.cpp:129
const char *const kMicroFeatureType
Definition: featdefs.cpp:32
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc)
Definition: featdefs.cpp:195
#define PICO_FEATURE_LENGTH
Definition: featdefs.cpp:27
TESS_API const FEATURE_DESC_STRUCT PicoFeatDesc
const FEATURE_DESC_STRUCT OutlineFeatDesc
const FEATURE_DESC_STRUCT MicroFeatureDesc
const FEATURE_DESC_STRUCT IntFeatDesc
const FEATURE_DESC_STRUCT GeoFeatDesc
const FEATURE_DESC_STRUCT CharNormDesc
#define NUM_FEATURE_TYPES
Definition: featdefs.h:27
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:62
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
#define EndParamDesc
Definition: ocrfeatures.h:91
#define DefineParam(Circular, NonEssential, Min, Max)
Definition: ocrfeatures.h:82
#define DefineFeature(Name, NL, NC, SN, PN)
Definition: ocrfeatures.h:99
#define FEAT_NAME_SIZE
Definition: ocrfeatures.h:33
#define StartParamDesc(Name)
Definition: ocrfeatures.h:81
TESS_API float PicoFeatureLength
void * Emalloc(int Size)
Definition: emalloc.cpp:31
void Efree(void *ptr)
Definition: emalloc.cpp:45
Definition: strngs.h:45
void add_str_int(const char *str, int number)
Definition: strngs.cpp:377
uint32_t NumFeatureSets
Definition: featdefs.h:40
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:41
int32_t NumFeatureTypes
Definition: featdefs.h:46
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:47
const char * ShortName
Definition: ocrfeatures.h:54
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:60
float Params[1]
Definition: ocrfeatures.h:61
FEATURE Features[1]
Definition: ocrfeatures.h:68
uint16_t NumFeatures
Definition: ocrfeatures.h:66