All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 "efio.h"
25 #include "featdefs.h"
26 #include "mfoutline.h"
27 #include "ocrfeatures.h"
28 
29 #include <stdio.h>
30 
31 /*----------------------------------------------------------------------------
32  Public Code
33 ----------------------------------------------------------------------------*/
34 /*---------------------------------------------------------------------------*/
35 namespace tesseract {
48  LIST Outlines;
49  LIST RemainingOutlines;
50  MFOUTLINE Outline;
51  FEATURE_SET FeatureSet;
52  FLOAT32 XScale, YScale;
53 
54  FeatureSet = NewFeatureSet (MAX_OUTLINE_FEATURES);
55  if (Blob == NULL)
56  return (FeatureSet);
57 
58  Outlines = ConvertBlob (Blob);
59 
60  NormalizeOutlines(Outlines, &XScale, &YScale);
61  RemainingOutlines = Outlines;
62  iterate(RemainingOutlines) {
63  Outline = (MFOUTLINE) first_node (RemainingOutlines);
64  ConvertToOutlineFeatures(Outline, FeatureSet);
65  }
67  NormalizeOutlineX(FeatureSet);
68  FreeOutlines(Outlines);
69  return (FeatureSet);
70 } /* ExtractOutlineFeatures */
71 } // namespace tesseract
72 
73 /*----------------------------------------------------------------------------
74  Private Code
75 ----------------------------------------------------------------------------*/
76 /*---------------------------------------------------------------------------*/
94  FPOINT *End,
95  FEATURE_SET FeatureSet) {
96  FEATURE Feature;
97 
98  Feature = NewFeature(&OutlineFeatDesc);
99  Feature->Params[OutlineFeatDir] = NormalizedAngleFrom(Start, End, 1.0);
100  Feature->Params[OutlineFeatX] = AverageOf(Start->x, End->x);
101  Feature->Params[OutlineFeatY] = AverageOf(Start->y, End->y);
102  Feature->Params[OutlineFeatLength] = DistanceBetween(*Start, *End);
103  AddFeature(FeatureSet, Feature);
104 
105 } /* AddOutlineFeatureToSet */
106 
107 
108 /*---------------------------------------------------------------------------*/
122 void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet) {
123  MFOUTLINE Next;
124  MFOUTLINE First;
125  FPOINT FeatureStart;
126  FPOINT FeatureEnd;
127 
128  if (DegenerateOutline (Outline))
129  return;
130 
131  First = Outline;
132  Next = First;
133  do {
134  FeatureStart = PointAt(Next)->Point;
135  Next = NextPointAfter(Next);
136 
137  /* note that an edge is hidden if the ending point of the edge is
138  marked as hidden. This situation happens because the order of
139  the outlines is reversed when they are converted from the old
140  format. In the old format, a hidden edge is marked by the
141  starting point for that edge. */
142  if (!PointAt(Next)->Hidden) {
143  FeatureEnd = PointAt(Next)->Point;
144  AddOutlineFeatureToSet(&FeatureStart, &FeatureEnd, FeatureSet);
145  }
146  }
147  while (Next != First);
148 } /* ConvertToOutlineFeatures */
149 
150 
151 /*---------------------------------------------------------------------------*/
163 void NormalizeOutlineX(FEATURE_SET FeatureSet) {
164  int i;
165  FEATURE Feature;
166  FLOAT32 Length;
167  FLOAT32 TotalX = 0.0;
168  FLOAT32 TotalWeight = 0.0;
169  FLOAT32 Origin;
170 
171  if (FeatureSet->NumFeatures <= 0)
172  return;
173 
174  for (i = 0; i < FeatureSet->NumFeatures; i++) {
175  Feature = FeatureSet->Features[i];
176  Length = Feature->Params[OutlineFeatLength];
177  TotalX += Feature->Params[OutlineFeatX] * Length;
178  TotalWeight += Length;
179  }
180  Origin = TotalX / TotalWeight;
181 
182  for (i = 0; i < FeatureSet->NumFeatures; i++) {
183  Feature = FeatureSet->Features[i];
184  Feature->Params[OutlineFeatX] -= Origin;
185  }
186 } /* NormalizeOutlineX */
Definition: blobs.h:261
float FLOAT32
Definition: host.h:111
#define AverageOf(A, B)
Definition: mfoutline.h:60
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:96
FEATURE_SET NewFeatureSet(int NumFeatures)
#define MAX_OUTLINE_FEATURES
Definition: outfeat.h:35
FLOAT32 NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, FLOAT32 FullScale)
Definition: fpoint.cpp:48
FEATURE Features[1]
Definition: ocrfeatures.h:72
void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:122
Definition: fpoint.h:29
void NormalizeOutlineX(FEATURE_SET FeatureSet)
Definition: outfeat.cpp:163
LIST ConvertBlob(TBLOB *blob)
Definition: mfoutline.cpp:39
#define PointAt(O)
Definition: mfoutline.h:67
void FreeOutlines(LIST Outlines)
Definition: mfoutline.cpp:178
FLOAT32 DistanceBetween(FPOINT A, FPOINT B)
Definition: fpoint.cpp:30
FLOAT32 y
Definition: fpoint.h:31
#define first_node(l)
Definition: oldlist.h:139
#define iterate(l)
Definition: oldlist.h:159
#define DegenerateOutline(O)
Definition: mfoutline.h:66
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:44
FEATURE_SET ExtractOutlineFeatures(TBLOB *Blob)
Definition: outfeat.cpp:47
#define NextPointAfter(E)
Definition: mfoutline.h:68
#define NULL
Definition: host.h:144
const FEATURE_DESC_STRUCT OutlineFeatDesc
LIST MFOUTLINE
Definition: mfoutline.h:33
void AddOutlineFeatureToSet(FPOINT *Start, FPOINT *End, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:93
FLOAT32 x
Definition: fpoint.h:31
void NormalizeOutlines(LIST Outlines, FLOAT32 *XScale, FLOAT32 *YScale)
Definition: mfoutline.cpp:300