tesseract  5.0.0-alpha-619-ge9db
outfeat.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: outfeat.c
3  ** Purpose: Definition of outline-features.
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 "outfeat.h"
21 
22 #include "classify.h"
23 #include "featdefs.h"
24 #include "mfoutline.h"
25 #include "ocrfeatures.h"
26 
27 #include <cstdio>
28 
29 /*----------------------------------------------------------------------------
30  Public Code
31 ----------------------------------------------------------------------------*/
32 /*---------------------------------------------------------------------------*/
33 namespace tesseract {
42  LIST Outlines;
43  LIST RemainingOutlines;
44  MFOUTLINE Outline;
45  FEATURE_SET FeatureSet;
46  float XScale, YScale;
47 
48  FeatureSet = NewFeatureSet (MAX_OUTLINE_FEATURES);
49  if (Blob == nullptr)
50  return (FeatureSet);
51 
52  Outlines = ConvertBlob (Blob);
53 
54  NormalizeOutlines(Outlines, &XScale, &YScale);
55  RemainingOutlines = Outlines;
56  iterate(RemainingOutlines) {
57  Outline = static_cast<MFOUTLINE>first_node (RemainingOutlines);
58  ConvertToOutlineFeatures(Outline, FeatureSet);
59  }
61  NormalizeOutlineX(FeatureSet);
62  FreeOutlines(Outlines);
63  return (FeatureSet);
64 } /* ExtractOutlineFeatures */
65 } // namespace tesseract
66 
67 /*----------------------------------------------------------------------------
68  Private Code
69 ----------------------------------------------------------------------------*/
70 /*---------------------------------------------------------------------------*/
83 void AddOutlineFeatureToSet(FPOINT *Start,
84  FPOINT *End,
85  FEATURE_SET FeatureSet) {
86  FEATURE Feature;
87 
88  Feature = NewFeature(&OutlineFeatDesc);
89  Feature->Params[OutlineFeatDir] = NormalizedAngleFrom(Start, End, 1.0);
90  Feature->Params[OutlineFeatX] = AverageOf(Start->x, End->x);
91  Feature->Params[OutlineFeatY] = AverageOf(Start->y, End->y);
92  Feature->Params[OutlineFeatLength] = DistanceBetween(*Start, *End);
93  AddFeature(FeatureSet, Feature);
94 
95 } /* AddOutlineFeatureToSet */
96 
97 
98 /*---------------------------------------------------------------------------*/
107 void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet) {
108  MFOUTLINE Next;
109  MFOUTLINE First;
110  FPOINT FeatureStart;
111  FPOINT FeatureEnd;
112 
113  if (DegenerateOutline (Outline))
114  return;
115 
116  First = Outline;
117  Next = First;
118  do {
119  FeatureStart = PointAt(Next)->Point;
120  Next = NextPointAfter(Next);
121 
122  /* note that an edge is hidden if the ending point of the edge is
123  marked as hidden. This situation happens because the order of
124  the outlines is reversed when they are converted from the old
125  format. In the old format, a hidden edge is marked by the
126  starting point for that edge. */
127  if (!PointAt(Next)->Hidden) {
128  FeatureEnd = PointAt(Next)->Point;
129  AddOutlineFeatureToSet(&FeatureStart, &FeatureEnd, FeatureSet);
130  }
131  }
132  while (Next != First);
133 } /* ConvertToOutlineFeatures */
134 
135 
136 /*---------------------------------------------------------------------------*/
145 void NormalizeOutlineX(FEATURE_SET FeatureSet) {
146  int i;
147  FEATURE Feature;
148  float Length;
149  float TotalX = 0.0;
150  float TotalWeight = 0.0;
151  float Origin;
152 
153  if (FeatureSet->NumFeatures <= 0)
154  return;
155 
156  for (i = 0; i < FeatureSet->NumFeatures; i++) {
157  Feature = FeatureSet->Features[i];
158  Length = Feature->Params[OutlineFeatLength];
159  TotalX += Feature->Params[OutlineFeatX] * Length;
160  TotalWeight += Length;
161  }
162  Origin = TotalX / TotalWeight;
163 
164  for (i = 0; i < FeatureSet->NumFeatures; i++) {
165  Feature = FeatureSet->Features[i];
166  Feature->Params[OutlineFeatX] -= Origin;
167  }
168 } /* NormalizeOutlineX */
AddFeature
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:39
tesseract::Classify::classify_norm_method
int classify_norm_method
Definition: classify.h:434
first_node
#define first_node(l)
Definition: oldlist.h:84
AddOutlineFeatureToSet
void AddOutlineFeatureToSet(FPOINT *Start, FPOINT *End, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:82
FPOINT::y
float y
Definition: fpoint.h:44
list_rec
Definition: oldlist.h:73
baseline
Definition: mfoutline.h:62
mfoutline.h
outfeat.h
OutlineFeatLength
Definition: outfeat.h:44
NormalizedAngleFrom
float NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, float FullScale)
Definition: fpoint.cpp:43
FEATURE_STRUCT
Definition: ocrfeatures.h:58
FPOINT
Definition: fpoint.h:28
tesseract::Classify::NormalizeOutlines
void NormalizeOutlines(LIST Outlines, float *XScale, float *YScale)
Definition: mfoutline.cpp:275
FPOINT::x
float x
Definition: fpoint.h:44
OutlineFeatDir
Definition: outfeat.h:45
OutlineFeatDesc
const FEATURE_DESC_STRUCT OutlineFeatDesc
ocrfeatures.h
DistanceBetween
float DistanceBetween(FPOINT A, FPOINT B)
Definition: fpoint.cpp:28
ConvertBlob
LIST ConvertBlob(TBLOB *blob)
Definition: mfoutline.cpp:36
FEATURE_SET_STRUCT::Features
FEATURE Features[1]
Definition: ocrfeatures.h:67
OutlineFeatY
Definition: outfeat.h:43
tesseract::Classify::ExtractOutlineFeatures
FEATURE_SET ExtractOutlineFeatures(TBLOB *Blob)
Definition: outfeat.cpp:54
tesseract
Definition: baseapi.h:65
FEATURE_STRUCT::Params
float Params[1]
Definition: ocrfeatures.h:60
NewFeature
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:77
FEATURE_SET_STRUCT
Definition: ocrfeatures.h:64
FreeOutlines
void FreeOutlines(LIST Outlines)
Definition: mfoutline.cpp:166
NewFeatureSet
FEATURE_SET NewFeatureSet(int NumFeatures)
Definition: ocrfeatures.cpp:93
TBLOB
Definition: blobs.h:282
featdefs.h
iterate
#define iterate(l)
Definition: oldlist.h:92
NormalizeOutlineX
void NormalizeOutlineX(FEATURE_SET FeatureSet)
Definition: outfeat.cpp:144
OutlineFeatX
Definition: outfeat.h:42
classify.h
FEATURE_SET_STRUCT::NumFeatures
uint16_t NumFeatures
Definition: ocrfeatures.h:65
ConvertToOutlineFeatures
void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:106
AverageOf
#define AverageOf(A, B)
Definition: mfoutline.h:67
MAX_OUTLINE_FEATURES
#define MAX_OUTLINE_FEATURES
Definition: outfeat.h:34