tesseract v5.3.3.20231005
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 "featdefs.h"
19
20#include "picofeat.h" // for PicoFeatureLength
21#include "scanutils.h"
22
23#include <cstdio>
24#include <cstring>
25
26namespace tesseract {
27
28#define PICO_FEATURE_LENGTH 0.05
29
30/*-----------------------------------------------------------------------------
31 Global Data Definitions and Declarations
32-----------------------------------------------------------------------------*/
33const char *const kMicroFeatureType = "mf";
34const char *const kCNFeatureType = "cn";
35const char *const kIntFeatureType = "if";
36const char *const kGeoFeatureType = "tb";
37
38// Define all of the parameters for the MicroFeature type.
39StartParamDesc(MicroFeatureParams) DefineParam(0, 0, -0.5, 0.5) DefineParam(0, 0, -0.25, 0.75)
40 DefineParam(0, 1, 0.0, 1.0) DefineParam(1, 0, 0.0, 1.0) DefineParam(0, 1, -0.5, 0.5)
41 DefineParam(0, 1, -0.5, 0.5) EndParamDesc
42 // Now define the feature type itself (see features.h for parameters).
43 DefineFeature(MicroFeatureDesc, 5, 1, kMicroFeatureType, MicroFeatureParams)
44
45 // Define all of the parameters for the NormFeat type.
46 StartParamDesc(CharNormParams) DefineParam(0, 0, -0.25, 0.75) DefineParam(0, 1, 0.0, 1.0)
47 DefineParam(0, 0, 0.0, 1.0) DefineParam(0, 0, 0.0, 1.0) EndParamDesc
48 // Now define the feature type itself (see features.h for parameters).
49 DefineFeature(CharNormDesc, 4, 0, kCNFeatureType, CharNormParams)
50
51 // Define all of the parameters for the IntFeature type
52 StartParamDesc(IntFeatParams) DefineParam(0, 0, 0.0, 255.0) DefineParam(0, 0, 0.0, 255.0)
53 DefineParam(1, 0, 0.0, 255.0) EndParamDesc
54 // Now define the feature type itself (see features.h for parameters).
56
57 // Define all of the parameters for the GeoFeature type
58 StartParamDesc(GeoFeatParams) DefineParam(0, 0, 0.0, 255.0) DefineParam(0, 0, 0.0, 255.0)
59 DefineParam(0, 0, 0.0, 255.0) EndParamDesc
60 // Now define the feature type itself (see features.h for parameters).
62
63 // Other features used for training the adaptive classifier, but not used
64 // during normal training, therefore not in the DescDefs array.
65
66 // Define all of the parameters for the PicoFeature type
67 // define knob that can be used to adjust pico-feature length.
69StartParamDesc(PicoFeatParams) DefineParam(0, 0, -0.25, 0.75) DefineParam(1, 0, 0.0, 1.0)
70 DefineParam(0, 0, -0.5, 0.5) EndParamDesc
71 // Now define the feature type itself (see features.h for parameters).
72 DefineFeature(PicoFeatDesc, 2, 1, "pf", PicoFeatParams)
73
74 // Define all of the parameters for the OutlineFeature type.
75 StartParamDesc(OutlineFeatParams) DefineParam(0, 0, -0.5, 0.5) DefineParam(0, 0, -0.25, 0.75)
76 DefineParam(0, 0, 0.0, 1.0) DefineParam(1, 0, 0.0, 1.0) EndParamDesc
77 // Now define the feature type itself (see features.h for parameters).
78 DefineFeature(OutlineFeatDesc, 3, 1, "of", OutlineFeatParams)
79
80 // MUST be kept in-sync with ExtractorDefs in fxdefs.cpp.
81 static const FEATURE_DESC_STRUCT *DescDefs[NUM_FEATURE_TYPES] = {
83
84/*-----------------------------------------------------------------------------
85 Public Code
86-----------------------------------------------------------------------------*/
89 for (int i = 0; i < NUM_FEATURE_TYPES; ++i) {
90 featuredefs->FeatureDesc[i] = DescDefs[i];
91 }
92}
93
94/*---------------------------------------------------------------------------*/
109void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC_STRUCT *CharDesc, std::string &str) {
110 int NumSetsToWrite = 0;
111
112 for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
113 if (CharDesc->FeatureSets[Type]) {
114 NumSetsToWrite++;
115 }
116 }
117
118 str += " " + std::to_string(NumSetsToWrite);
119 str += "\n";
120 for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
121 if (CharDesc->FeatureSets[Type]) {
122 str += FeatureDefs.FeatureDesc[Type]->ShortName;
123 str += " ";
124 WriteFeatureSet(CharDesc->FeatureSets[Type], str);
125 }
126 }
127} /* WriteCharDescription */
128
129// Return whether all of the fields of the given feature set
130// are well defined (not inf or nan).
131bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC_STRUCT *CharDesc) {
132 bool anything_written = false;
133 bool well_formed = true;
134 for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
135 if (CharDesc->FeatureSets[Type]) {
136 for (int i = 0; i < CharDesc->FeatureSets[Type]->NumFeatures; i++) {
137 FEATURE feat = CharDesc->FeatureSets[Type]->Features[i];
138 for (int p = 0; p < feat->Type->NumParams; p++) {
139 if (std::isnan(feat->Params[p]) || std::isinf(feat->Params[p])) {
140 well_formed = false;
141 } else {
142 anything_written = true;
143 }
144 }
145 }
146 } else {
147 return false;
148 }
149 }
150 return anything_written && well_formed;
151} /* ValidCharDescription */
152
153/*---------------------------------------------------------------------------*/
173 int NumSetsToRead;
174 char ShortName[FEAT_NAME_SIZE];
175 int Type;
176
177 ASSERT_HOST(tfscanf(File, "%d", &NumSetsToRead) == 1);
178 ASSERT_HOST(NumSetsToRead >= 0);
179 ASSERT_HOST(NumSetsToRead <= FeatureDefs.NumFeatureTypes);
180
181 auto CharDesc = new CHAR_DESC_STRUCT(FeatureDefs);
182 for (; NumSetsToRead > 0; NumSetsToRead--) {
183 tfscanf(File, "%s", ShortName);
184 Type = ShortNameToFeatureType(FeatureDefs, ShortName);
185 CharDesc->FeatureSets[Type] = ReadFeatureSet(File, FeatureDefs.FeatureDesc[Type]);
186 }
187 return CharDesc;
188}
189
190/*---------------------------------------------------------------------------*/
203uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName) {
204 for (int i = 0; i < FeatureDefs.NumFeatureTypes; i++) {
205 if (!strcmp((FeatureDefs.FeatureDesc[i]->ShortName), ShortName)) {
206 return static_cast<uint32_t>(i);
207 }
208 }
209 ASSERT_HOST(!"Illegal short name for a feature");
210 return 0;
211}
212
213} // namespace tesseract
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:189
#define ASSERT_HOST(x)
Definition: errcode.h:54
#define EndParamDesc
Definition: ocrfeatures.h:108
#define FEAT_NAME_SIZE
Definition: ocrfeatures.h:32
#define PICO_FEATURE_LENGTH
Definition: featdefs.cpp:28
#define NUM_FEATURE_TYPES
Definition: featdefs.h:29
const char * p
const char *const kCNFeatureType
Definition: featdefs.cpp:34
StartParamDesc(MicroFeatureParams) DefineParam(0
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
Definition: featdefs.cpp:203
TESS_API float PicoFeatureLength
EndParamDesc of
Definition: featdefs.cpp:78
CHAR_DESC_STRUCT * ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File)
Definition: featdefs.cpp:172
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC_STRUCT *CharDesc)
Definition: featdefs.cpp:131
const FEATURE_DESC_STRUCT GeoFeatDesc
TESS_API const FEATURE_DESC_STRUCT PicoFeatDesc
const char *const kGeoFeatureType
Definition: featdefs.cpp:36
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC_STRUCT *CharDesc, std::string &str)
Definition: featdefs.cpp:109
const FEATURE_DESC_STRUCT IntFeatDesc
const char *const kIntFeatureType
Definition: featdefs.cpp:35
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:82
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
Definition: featdefs.cpp:87
DefineParam(0, 0, -0.25, 0.75) DefineParam(0
void WriteFeatureSet(FEATURE_SET FeatureSet, std::string &str)
const FEATURE_DESC_STRUCT CharNormDesc
MicroFeatureParams CharNormParams EndParamDesc DefineFeature(IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams) StartParamDesc(GeoFeatParams) DefineParam(0
const char *const kMicroFeatureType
Definition: featdefs.cpp:33
const FEATURE_DESC_STRUCT OutlineFeatDesc
const FEATURE_DESC_STRUCT MicroFeatureDesc
const
Definition: upload.py:413
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:43
std::array< FEATURE_SET_STRUCT *, NUM_FEATURE_TYPES > FeatureSets
Definition: featdefs.h:63
std::vector< float > Params
Definition: ocrfeatures.h:66
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:65