mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2024-12-13 07:59:04 +08:00
524a61452d
Squashed commit from https://github.com/tesseract-ocr/tesseract/tree/more-doxygen closes #14 Commits:6317305
doxygen9f42f69
doxygen0fc4d52
doxygen37b4b55
fix typobded8f1
some more doxy020eb00
slight tweak524666d
doxygenify2a36a3e
doxygenify229d218
doxygenify7fd28ae
doxygenifya8c64bc
doxygenifyf5d21b6
fix5d8ede8
doxygenifya58a4e0
language_model.cppfa85709
lm_pain_points.cpp lm_state.cpp6418da3
merge06190ba
Merge branch 'old_doxygen_merge' into more-doxygen84acf08
Merge branch 'master' into more-doxygen50fe1ff
pagewalk.cpp cube_reco_context.cpp2982583
change to relative192a24a
applybox.cpp, take one8eeb053
delete docs for obsolete params52e4c77
modernise classify/ocrfeatures.cpp2a1cba6
modernise cutil/emalloc.cpp773e006
silence doxygen warningaeb1731
silence doxygen warningf18387f
silence doxygen; new params are unused?15ad6bd
doxygenify cutil/efio.cppc8b5dad
doxygenify cutil/danerror.cpp784450f
the globals and exceptions parts are obsolete; remove8bca324
doxygen classify/normfeat.cpp9bcbe16
doxygen classify/normmatch.cppaa9a971
doxygen ccmain/cube_control.cppc083ff2
doxygen ccmain/cube_reco_context.cppf842850
params changed5c94f12
doxygen ccmain/cubeclassifier.cpp15ba750
case sensitivef5c71d4
case sensitivef85655b
doxygen classify/intproto.cpp4bbc7aa
partial doxygen classify/mfx.cppdbb6041
partial doxygen classify/intproto.cpp2aa72db
finish doxygen classify/intproto.cpp0b8de99
doxygen training/mftraining.cpp0b5b35c
partial doxygen ccstruct/coutln.cppb81c766
partial doxygen ccstruct/coutln.cpp40fc415
finished? doxygen ccstruct/coutln.cpp6e4165c
doxygen classify/clusttool.cpp0267dec
doxygen classify/cutoffs.cpp7f0c70c
doxygen classify/fpoint.cpp512f3bd
ignore ~ files5668a52
doxygen classify/intmatcher.cpp84788d4
doxygen classify/kdtree.cpp29f36ca
doxygen classify/mfoutline.cpp40b94b1
silence doxygen warnings6c511b9
doxygen classify/mfx.cppf9b4080
doxygen classify/outfeat.cppaa1df05
doxygen classify/picofeat.cppcc5f466
doxygen training/cntraining.cppcce044f
doxygen training/commontraining.cpp167e216
missing param9498383
renamed params37eeac2
renamed paramd87b5dd
casec8ee174
renamed paramsb858db8
typo4c2a838
h2 context?81a2c0c
fix some param names; add some missing params, no docsbcf8a4c
add some missing params, no docsaf77f86
add some missing params, no docs; fix some param names01df24e
fix some params6161056
fix some params68508b6
fix some params285aeb6
doxygen complains here no matter what529bcfa
rm some missing params, typoscd21226
rm some missing params, add some new ones48a4bc2
fix paramsc844628
missing param312ce37
missing param; rename oneec2fdec
missing param05e15e0
missing paramsd515858
change "<" to < to make doxygen happyb476a28
wrong place
262 lines
8.9 KiB
C++
262 lines
8.9 KiB
C++
/******************************************************************************
|
|
** Filename: features.c
|
|
** Purpose: Generic definition of a feature.
|
|
** Author: Dan Johnson
|
|
** History: Mon May 21 10:49:04 1990, DSJ, Created.
|
|
**
|
|
** (c) Copyright Hewlett-Packard Company, 1988.
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
** you may not use this file except in compliance with the License.
|
|
** You may obtain a copy of the License at
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
** See the License for the specific language governing permissions and
|
|
** limitations under the License.
|
|
******************************************************************************/
|
|
/*----------------------------------------------------------------------------
|
|
Include Files and Type Defines
|
|
----------------------------------------------------------------------------*/
|
|
#include "ocrfeatures.h"
|
|
#include "emalloc.h"
|
|
#include "callcpp.h"
|
|
#include "danerror.h"
|
|
#include "freelist.h"
|
|
#include "scanutils.h"
|
|
|
|
#include <assert.h>
|
|
#include <math.h>
|
|
|
|
/*----------------------------------------------------------------------------
|
|
Public Code
|
|
----------------------------------------------------------------------------*/
|
|
/**
|
|
* Add a feature to a feature set. If the feature set is
|
|
* already full, FALSE is returned to indicate that the
|
|
* feature could not be added to the set; otherwise, TRUE is
|
|
* returned.
|
|
* @param FeatureSet set of features to add Feature to
|
|
* @param Feature feature to be added to FeatureSet
|
|
* @return TRUE if feature added to set, FALSE if set is already full.
|
|
* @note History: Tue May 22 17:22:23 1990, DSJ, Created.
|
|
*/
|
|
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
|
|
if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
|
|
FreeFeature(Feature);
|
|
return FALSE;
|
|
}
|
|
|
|
FeatureSet->Features[FeatureSet->NumFeatures++] = Feature;
|
|
return TRUE;
|
|
} /* AddFeature */
|
|
|
|
/**
|
|
* Release the memory consumed by the specified feature.
|
|
* @param Feature feature to be deallocated.
|
|
* @return none
|
|
* @note History: Mon May 21 13:33:27 1990, DSJ, Created.
|
|
*/
|
|
void FreeFeature(FEATURE Feature) {
|
|
if (Feature) {
|
|
free_struct (Feature, sizeof (FEATURE_STRUCT)
|
|
+ sizeof (FLOAT32) * (Feature->Type->NumParams - 1),
|
|
"sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
|
|
}
|
|
|
|
} /* FreeFeature */
|
|
|
|
|
|
/**
|
|
* Release the memory consumed by the specified feature
|
|
* set. This routine also frees the memory consumed by the
|
|
* features contained in the set.
|
|
* @param FeatureSet set of features to be freed
|
|
* @return none
|
|
* @note History: Mon May 21 13:59:46 1990, DSJ, Created.
|
|
*/
|
|
void FreeFeatureSet(FEATURE_SET FeatureSet) {
|
|
int i;
|
|
|
|
if (FeatureSet) {
|
|
for (i = 0; i < FeatureSet->NumFeatures; i++)
|
|
FreeFeature(FeatureSet->Features[i]);
|
|
memfree(FeatureSet);
|
|
}
|
|
} /* FreeFeatureSet */
|
|
|
|
|
|
/**
|
|
* Allocate and return a new feature of the specified
|
|
* type.
|
|
* @param FeatureDesc description of feature to be created.
|
|
* @return New #FEATURE.
|
|
* @note History: Mon May 21 14:06:42 1990, DSJ, Created.
|
|
*/
|
|
FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) {
|
|
FEATURE Feature;
|
|
|
|
Feature = (FEATURE) alloc_struct (sizeof (FEATURE_STRUCT) +
|
|
(FeatureDesc->NumParams - 1) *
|
|
sizeof (FLOAT32),
|
|
"sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
|
|
Feature->Type = FeatureDesc;
|
|
return (Feature);
|
|
|
|
} /* NewFeature */
|
|
|
|
|
|
/**
|
|
* Allocate and return a new feature set large enough to
|
|
* hold the specified number of features.
|
|
* @param NumFeatures maximum # of features to be put in feature set
|
|
* @return New #FEATURE_SET.
|
|
* @note History: Mon May 21 14:22:40 1990, DSJ, Created.
|
|
*/
|
|
FEATURE_SET NewFeatureSet(int NumFeatures) {
|
|
FEATURE_SET FeatureSet;
|
|
|
|
FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) +
|
|
(NumFeatures - 1) * sizeof (FEATURE));
|
|
FeatureSet->MaxNumFeatures = NumFeatures;
|
|
FeatureSet->NumFeatures = 0;
|
|
return (FeatureSet);
|
|
|
|
} /* NewFeatureSet */
|
|
|
|
|
|
/**
|
|
* Create a new feature of the specified type and read in
|
|
* the value of its parameters from File. The extra penalty
|
|
* for the feature is also computed by calling the appropriate
|
|
* function for the specified feature type. The correct text
|
|
* representation for a feature is a list of N floats where
|
|
* N is the number of parameters in the feature.
|
|
* @param File open text file to read feature from
|
|
* @param FeatureDesc specifies type of feature to read from File
|
|
* @return New #FEATURE read from File.
|
|
* @note Exceptions: #ILLEGAL_FEATURE_PARAM if text file doesn't match expected format
|
|
* @note History: Wed May 23 08:53:16 1990, DSJ, Created.
|
|
*/
|
|
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
|
|
FEATURE Feature;
|
|
int i;
|
|
|
|
Feature = NewFeature (FeatureDesc);
|
|
for (i = 0; i < Feature->Type->NumParams; i++) {
|
|
if (tfscanf(File, "%f", &(Feature->Params[i])) != 1)
|
|
DoError (ILLEGAL_FEATURE_PARAM, "Illegal feature parameter spec");
|
|
#ifndef _WIN32
|
|
assert (!isnan(Feature->Params[i]));
|
|
#endif
|
|
}
|
|
return (Feature);
|
|
} /* ReadFeature */
|
|
|
|
|
|
/**
|
|
* Create a new feature set of the specified type and read in
|
|
* the features from File. The correct text representation
|
|
* for a feature set is an integer which specifies the number (N)
|
|
* of features in a set followed by a list of N feature
|
|
* descriptions.
|
|
* @param File open text file to read new feature set from
|
|
* @param FeatureDesc specifies type of feature to read from File
|
|
* @return New feature set read from File.
|
|
* @note History: Wed May 23 09:17:31 1990, DSJ, Created.
|
|
*/
|
|
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
|
|
FEATURE_SET FeatureSet;
|
|
int NumFeatures;
|
|
int i;
|
|
|
|
if (tfscanf(File, "%d", &NumFeatures) != 1 || NumFeatures < 0)
|
|
DoError(ILLEGAL_NUM_FEATURES, "Illegal number of features in set");
|
|
|
|
FeatureSet = NewFeatureSet(NumFeatures);
|
|
for (i = 0; i < NumFeatures; i++)
|
|
AddFeature(FeatureSet, ReadFeature (File, FeatureDesc));
|
|
|
|
return (FeatureSet);
|
|
} /* ReadFeatureSet */
|
|
|
|
|
|
/**
|
|
* Appends a textual representation of Feature to str.
|
|
* This representation is simply a list of the N parameters
|
|
* of the feature, terminated with a newline. It is assumed
|
|
* that the ExtraPenalty field can be reconstructed from the
|
|
* parameters of the feature. It is also assumed that the
|
|
* feature type information is specified or assumed elsewhere.
|
|
* @param Feature feature to write out to str
|
|
* @param str string to write Feature to
|
|
* @return none
|
|
* @note History: Wed May 23 09:28:18 1990, DSJ, Created.
|
|
*/
|
|
void WriteFeature(FEATURE Feature, STRING* str) {
|
|
for (int i = 0; i < Feature->Type->NumParams; i++) {
|
|
#ifndef WIN32
|
|
assert(!isnan(Feature->Params[i]));
|
|
#endif
|
|
str->add_str_double(" ", Feature->Params[i]);
|
|
}
|
|
*str += "\n";
|
|
} /* WriteFeature */
|
|
|
|
|
|
/**
|
|
* Write a textual representation of FeatureSet to File.
|
|
* This representation is an integer specifying the number of
|
|
* features in the set, followed by a newline, followed by
|
|
* text representations for each feature in the set.
|
|
* @param FeatureSet feature set to write to File
|
|
* @param str string to write Feature to
|
|
* @return none
|
|
* @note History: Wed May 23 10:06:03 1990, DSJ, Created.
|
|
*/
|
|
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) {
|
|
if (FeatureSet) {
|
|
str->add_str_int("", FeatureSet->NumFeatures);
|
|
*str += "\n";
|
|
for (int i = 0; i < FeatureSet->NumFeatures; i++) {
|
|
WriteFeature(FeatureSet->Features[i], str);
|
|
}
|
|
}
|
|
} /* WriteFeatureSet */
|
|
|
|
|
|
/**
|
|
* Write a textual representation of FeatureDesc to File
|
|
* in the old format (i.e. the format used by the clusterer).
|
|
*
|
|
* This format is:
|
|
* @verbatim
|
|
* Number of Params
|
|
* Description of Param 1
|
|
* ...
|
|
* @endverbatim
|
|
* @param File open text file to write FeatureDesc to
|
|
* @param FeatureDesc feature descriptor to write to File
|
|
* @return none
|
|
* @note History: Fri May 25 15:27:18 1990, DSJ, Created.
|
|
*/
|
|
void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
|
|
int i;
|
|
|
|
fprintf (File, "%d\n", FeatureDesc->NumParams);
|
|
for (i = 0; i < FeatureDesc->NumParams; i++) {
|
|
if (FeatureDesc->ParamDesc[i].Circular)
|
|
fprintf (File, "circular ");
|
|
else
|
|
fprintf (File, "linear ");
|
|
|
|
if (FeatureDesc->ParamDesc[i].NonEssential)
|
|
fprintf (File, "non-essential ");
|
|
else
|
|
fprintf (File, "essential ");
|
|
|
|
fprintf (File, "%f %f\n",
|
|
FeatureDesc->ParamDesc[i].Min, FeatureDesc->ParamDesc[i].Max);
|
|
}
|
|
} /* WriteOldParamDesc */
|