tesseract  4.0.0-1-g2a2b
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  ** History: 11/13/90, 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 #include "outfeat.h"
22 
23 #include "classify.h"
24 #include "featdefs.h"
25 #include "mfoutline.h"
26 #include "ocrfeatures.h"
27 
28 #include <cstdio>
29 
30 /*----------------------------------------------------------------------------
31  Public Code
32 ----------------------------------------------------------------------------*/
33 /*---------------------------------------------------------------------------*/
34 namespace tesseract {
43  LIST Outlines;
44  LIST RemainingOutlines;
45  MFOUTLINE Outline;
46  FEATURE_SET FeatureSet;
47  float XScale, YScale;
48 
49  FeatureSet = NewFeatureSet (MAX_OUTLINE_FEATURES);
50  if (Blob == nullptr)
51  return (FeatureSet);
52 
53  Outlines = ConvertBlob (Blob);
54 
55  NormalizeOutlines(Outlines, &XScale, &YScale);
56  RemainingOutlines = Outlines;
57  iterate(RemainingOutlines) {
58  Outline = (MFOUTLINE) first_node (RemainingOutlines);
59  ConvertToOutlineFeatures(Outline, FeatureSet);
60  }
62  NormalizeOutlineX(FeatureSet);
63  FreeOutlines(Outlines);
64  return (FeatureSet);
65 } /* ExtractOutlineFeatures */
66 } // namespace tesseract
67 
68 /*----------------------------------------------------------------------------
69  Private Code
70 ----------------------------------------------------------------------------*/
71 /*---------------------------------------------------------------------------*/
87  FPOINT *End,
88  FEATURE_SET FeatureSet) {
89  FEATURE Feature;
90 
91  Feature = NewFeature(&OutlineFeatDesc);
92  Feature->Params[OutlineFeatDir] = NormalizedAngleFrom(Start, End, 1.0);
93  Feature->Params[OutlineFeatX] = AverageOf(Start->x, End->x);
94  Feature->Params[OutlineFeatY] = AverageOf(Start->y, End->y);
95  Feature->Params[OutlineFeatLength] = DistanceBetween(*Start, *End);
96  AddFeature(FeatureSet, Feature);
97 
98 } /* AddOutlineFeatureToSet */
99 
100 
101 /*---------------------------------------------------------------------------*/
111 void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet) {
112  MFOUTLINE Next;
113  MFOUTLINE First;
114  FPOINT FeatureStart;
115  FPOINT FeatureEnd;
116 
117  if (DegenerateOutline (Outline))
118  return;
119 
120  First = Outline;
121  Next = First;
122  do {
123  FeatureStart = PointAt(Next)->Point;
124  Next = NextPointAfter(Next);
125 
126  /* note that an edge is hidden if the ending point of the edge is
127  marked as hidden. This situation happens because the order of
128  the outlines is reversed when they are converted from the old
129  format. In the old format, a hidden edge is marked by the
130  starting point for that edge. */
131  if (!PointAt(Next)->Hidden) {
132  FeatureEnd = PointAt(Next)->Point;
133  AddOutlineFeatureToSet(&FeatureStart, &FeatureEnd, FeatureSet);
134  }
135  }
136  while (Next != First);
137 } /* ConvertToOutlineFeatures */
138 
139 
140 /*---------------------------------------------------------------------------*/
150 void NormalizeOutlineX(FEATURE_SET FeatureSet) {
151  int i;
152  FEATURE Feature;
153  float Length;
154  float TotalX = 0.0;
155  float TotalWeight = 0.0;
156  float Origin;
157 
158  if (FeatureSet->NumFeatures <= 0)
159  return;
160 
161  for (i = 0; i < FeatureSet->NumFeatures; i++) {
162  Feature = FeatureSet->Features[i];
163  Length = Feature->Params[OutlineFeatLength];
164  TotalX += Feature->Params[OutlineFeatX] * Length;
165  TotalWeight += Length;
166  }
167  Origin = TotalX / TotalWeight;
168 
169  for (i = 0; i < FeatureSet->NumFeatures; i++) {
170  Feature = FeatureSet->Features[i];
171  Feature->Params[OutlineFeatX] -= Origin;
172  }
173 } /* NormalizeOutlineX */
void NormalizeOutlineX(FEATURE_SET FeatureSet)
Definition: outfeat.cpp:150
void NormalizeOutlines(LIST Outlines, float *XScale, float *YScale)
Definition: mfoutline.cpp:285
FEATURE_SET NewFeatureSet(int NumFeatures)
Definition: ocrfeatures.cpp:97
void FreeOutlines(LIST Outlines)
Definition: mfoutline.cpp:172
#define DegenerateOutline(O)
Definition: mfoutline.h:67
float DistanceBetween(FPOINT A, FPOINT B)
Definition: fpoint.cpp:28
float NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, float FullScale)
Definition: fpoint.cpp:44
Definition: fpoint.h:30
#define PointAt(O)
Definition: mfoutline.h:68
float Params[1]
Definition: ocrfeatures.h:62
FEATURE Features[1]
Definition: ocrfeatures.h:69
const FEATURE_DESC_STRUCT OutlineFeatDesc
LIST ConvertBlob(TBLOB *blob)
Definition: mfoutline.cpp:39
uint16_t NumFeatures
Definition: ocrfeatures.h:67
float y
Definition: fpoint.h:31
void AddOutlineFeatureToSet(FPOINT *Start, FPOINT *End, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:86
#define first_node(l)
Definition: oldlist.h:141
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:81
#define AverageOf(A, B)
Definition: mfoutline.h:61
FEATURE_SET ExtractOutlineFeatures(TBLOB *Blob)
Definition: outfeat.cpp:42
#define NextPointAfter(E)
Definition: mfoutline.h:69
#define iterate(l)
Definition: oldlist.h:161
Definition: blobs.h:268
void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:111
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:41
float x
Definition: fpoint.h:31
#define MAX_OUTLINE_FEATURES
Definition: outfeat.h:35
LIST MFOUTLINE
Definition: mfoutline.h:34