tesseract  3.04.00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  ******************************************************************************/
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 
34 /*---------------------------------------------------------------------------*/
35 namespace tesseract {
37 /*
38  ** Parameters:
39  ** Blob blob to extract pico-features from
40  ** LineStats statistics on text row blob is in
41  ** Globals: none
42  ** Operation: Convert each segment in the outline to a feature
43  ** and return the features.
44  ** Return: Outline-features for Blob.
45  ** Exceptions: none
46  ** History: 11/13/90, DSJ, Created.
47  ** 05/24/91, DSJ, Updated for either char or baseline normalize.
48  */
49  LIST Outlines;
50  LIST RemainingOutlines;
51  MFOUTLINE Outline;
52  FEATURE_SET FeatureSet;
53  FLOAT32 XScale, YScale;
54 
55  FeatureSet = NewFeatureSet (MAX_OUTLINE_FEATURES);
56  if (Blob == NULL)
57  return (FeatureSet);
58 
59  Outlines = ConvertBlob (Blob);
60 
61  NormalizeOutlines(Outlines, &XScale, &YScale);
62  RemainingOutlines = Outlines;
63  iterate(RemainingOutlines) {
64  Outline = (MFOUTLINE) first_node (RemainingOutlines);
65  ConvertToOutlineFeatures(Outline, FeatureSet);
66  }
68  NormalizeOutlineX(FeatureSet);
69  FreeOutlines(Outlines);
70  return (FeatureSet);
71 } /* ExtractOutlineFeatures */
72 } // namespace tesseract
73 
77 /*---------------------------------------------------------------------------*/
79  FPOINT *End,
80  FEATURE_SET FeatureSet) {
81 /*
82  ** Parameters:
83  ** Start starting point of outline-feature
84  ** End ending point of outline-feature
85  ** FeatureSet set to add outline-feature to
86  ** Globals: none
87  ** Operation: This routine computes the midpoint between Start and
88  ** End to obtain the x,y position of the outline-feature. It
89  ** also computes the direction from Start to End as the
90  ** direction of the outline-feature and the distance from
91  ** Start to End as the length of the outline-feature.
92  ** This feature is then
93  ** inserted into the next feature slot in FeatureSet.
94  ** Return: none (results are placed in FeatureSet)
95  ** Exceptions: none
96  ** History: 11/13/90, DSJ, Created.
97  */
98  FEATURE Feature;
99 
100  Feature = NewFeature(&OutlineFeatDesc);
101  Feature->Params[OutlineFeatDir] = NormalizedAngleFrom(Start, End, 1.0);
102  Feature->Params[OutlineFeatX] = AverageOf(Start->x, End->x);
103  Feature->Params[OutlineFeatY] = AverageOf(Start->y, End->y);
104  Feature->Params[OutlineFeatLength] = DistanceBetween(*Start, *End);
105  AddFeature(FeatureSet, Feature);
106 
107 } /* AddOutlineFeatureToSet */
108 
109 
110 /*---------------------------------------------------------------------------*/
111 void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet) {
112 /*
113  ** Parameters:
114  ** Outline outline to extract outline-features from
115  ** FeatureSet set of features to add outline-features to
116  ** Globals: none
117  ** Operation:
118  ** This routine steps converts each section in the specified
119  ** outline to a feature described by its x,y position, length
120  ** and angle.
121  ** Return: none (results are returned in FeatureSet)
122  ** Exceptions: none
123  ** History: 11/13/90, DSJ, Created.
124  ** 5/24/91, DSJ, Added hidden edge capability.
125  */
126  MFOUTLINE Next;
127  MFOUTLINE First;
128  FPOINT FeatureStart;
129  FPOINT FeatureEnd;
130 
131  if (DegenerateOutline (Outline))
132  return;
133 
134  First = Outline;
135  Next = First;
136  do {
137  FeatureStart = PointAt(Next)->Point;
138  Next = NextPointAfter(Next);
139 
140  /* note that an edge is hidden if the ending point of the edge is
141  marked as hidden. This situation happens because the order of
142  the outlines is reversed when they are converted from the old
143  format. In the old format, a hidden edge is marked by the
144  starting point for that edge. */
145  if (!PointAt(Next)->Hidden) {
146  FeatureEnd = PointAt(Next)->Point;
147  AddOutlineFeatureToSet(&FeatureStart, &FeatureEnd, FeatureSet);
148  }
149  }
150  while (Next != First);
151 } /* ConvertToOutlineFeatures */
152 
153 
154 /*---------------------------------------------------------------------------*/
155 void NormalizeOutlineX(FEATURE_SET FeatureSet) {
156 /*
157  ** Parameters:
158  ** FeatureSet outline-features to be normalized
159  ** Globals: none
160  ** Operation: This routine computes the weighted average x position
161  ** over all of the outline-features in FeatureSet and then
162  ** renormalizes the outline-features to force this average
163  ** to be the x origin (i.e. x=0).
164  ** Return: none (FeatureSet is changed)
165  ** Exceptions: none
166  ** History: 11/13/90, DSJ, Created.
167  */
168  int i;
169  FEATURE Feature;
170  FLOAT32 Length;
171  FLOAT32 TotalX = 0.0;
172  FLOAT32 TotalWeight = 0.0;
173  FLOAT32 Origin;
174 
175  if (FeatureSet->NumFeatures <= 0)
176  return;
177 
178  for (i = 0; i < FeatureSet->NumFeatures; i++) {
179  Feature = FeatureSet->Features[i];
180  Length = Feature->Params[OutlineFeatLength];
181  TotalX += Feature->Params[OutlineFeatX] * Length;
182  TotalWeight += Length;
183  }
184  Origin = TotalX / TotalWeight;
185 
186  for (i = 0; i < FeatureSet->NumFeatures; i++) {
187  Feature = FeatureSet->Features[i];
188  Feature->Params[OutlineFeatX] -= Origin;
189  }
190 } /* NormalizeOutlineX */
Definition: blobs.h:261
#define first_node(l)
Definition: oldlist.h:139
float FLOAT32
Definition: host.h:111
#define AverageOf(A, B)
Definition: mfoutline.h:60
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
FEATURE_SET NewFeatureSet(int NumFeatures)
#define MAX_OUTLINE_FEATURES
Definition: outfeat.h:35
FLOAT32 NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, FLOAT32 FullScale)
Definition: fpoint.cpp:39
#define iterate(l)
Definition: oldlist.h:159
FEATURE Features[1]
Definition: ocrfeatures.h:72
void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:111
Definition: fpoint.h:29
void NormalizeOutlineX(FEATURE_SET FeatureSet)
Definition: outfeat.cpp:155
LIST ConvertBlob(TBLOB *blob)
Definition: mfoutline.cpp:39
#define PointAt(O)
Definition: mfoutline.h:67
void FreeOutlines(LIST Outlines)
Definition: mfoutline.cpp:175
FLOAT32 DistanceBetween(FPOINT A, FPOINT B)
Definition: fpoint.cpp:31
FLOAT32 y
Definition: fpoint.h:31
#define DegenerateOutline(O)
Definition: mfoutline.h:66
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:35
FEATURE_SET ExtractOutlineFeatures(TBLOB *Blob)
Definition: outfeat.cpp:36
#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:78
FLOAT32 x
Definition: fpoint.h:31
void NormalizeOutlines(LIST Outlines, FLOAT32 *XScale, FLOAT32 *YScale)
Definition: mfoutline.cpp:295