tesseract  3.04.00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mfx.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: mfx.c
3  ** Purpose: Micro feature extraction routines
4  ** Author: Dan Johnson
5  ** History: 7/21/89, 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 "mfdefs.h"
22 #include "mfoutline.h"
23 #include "clusttool.h" //NEEDED
24 #include "const.h"
25 #include "intfx.h"
26 #include "normalis.h"
27 #include "params.h"
28 
29 #include <math.h>
30 
35 /* old numbers corresponded to 10.0 degrees and 80.0 degrees */
36 double_VAR(classify_min_slope, 0.414213562,
37  "Slope below which lines are called horizontal");
38 double_VAR(classify_max_slope, 2.414213562,
39  "Slope above which lines are called vertical");
40 
44 /* miscellaneous macros */
45 #define NormalizeAngle(A) ( (((A)<0)?((A)+2*PI):(A)) / (2*PI) )
46 
47 /*----------------------------------------------------------------------------
48  Private Function Prototypes
49 -----------------------------------------------------------------------------*/
51 
53  MICROFEATURES MicroFeatures);
54 
56 
61 /*---------------------------------------------------------------------------*/
62 MICROFEATURES BlobMicroFeatures(TBLOB* Blob, const DENORM& cn_denorm) {
63 /*
64  ** Parameters:
65  ** Blob blob to extract micro-features from
66  ** denorm control parameter to feature extractor
67  ** Operation:
68  ** This routine extracts micro-features from the specified
69  ** blob and returns a list of the micro-features. All
70  ** micro-features are normalized according to the specified
71  ** line statistics.
72  ** Return: List of micro-features extracted from the blob.
73  ** Exceptions: none
74  ** History: 7/21/89, DSJ, Created.
75  */
76  MICROFEATURES MicroFeatures = NIL_LIST;
77  LIST Outlines;
78  LIST RemainingOutlines;
79  MFOUTLINE Outline;
80 
81  if (Blob != NULL) {
82  Outlines = ConvertBlob(Blob);
83 
84  RemainingOutlines = Outlines;
85  iterate(RemainingOutlines) {
86  Outline = (MFOUTLINE) first_node (RemainingOutlines);
87  CharNormalizeOutline(Outline, cn_denorm);
88  }
89 
90  RemainingOutlines = Outlines;
91  iterate(RemainingOutlines) {
92  Outline = (MFOUTLINE) first_node(RemainingOutlines);
94  MarkDirectionChanges(Outline);
95  MicroFeatures = ConvertToMicroFeatures(Outline, MicroFeatures);
96  }
97  FreeOutlines(Outlines);
98  }
99  return MicroFeatures;
100 } /* BlobMicroFeatures */
101 
102 
103 /*---------------------------------------------------------------------------
104  Private Code
105 ---------------------------------------------------------------------------*/
106 
107 /*---------------------------------------------------------------------------*/
109 /*
110  ** Parameters:
111  ** Start starting edge point of micro-feature
112  ** End ending edge point of micro-feature
113  ** Globals: none
114  ** Operation:
115  ** This routine computes the orientation parameter of the
116  ** specified micro-feature. The orientation is the angle of
117  ** the vector from Start to End. It is normalized to a number
118  ** between 0 and 1 where 0 corresponds to 0 degrees and 1
119  ** corresponds to 360 degrees. The actual range is [0,1), i.e.
120  ** 1 is excluded from the range (since it is actual the
121  ** same orientation as 0). This routine assumes that Start
122  ** and End are not the same point.
123  ** Return: Orientation parameter for the specified micro-feature.
124  ** Exceptions: none
125  ** History: 7/27/89, DSJ, Created.
126  */
128 
129  Orientation = NormalizeAngle (AngleFrom (Start->Point, End->Point));
130 
131  /* ensure that round-off errors do not put circular param out of range */
132  if ((Orientation < 0) || (Orientation >= 1))
133  Orientation = 0;
134  return (Orientation);
135 } /* ComputeOrientation */
136 
137 
138 /*---------------------------------------------------------------------------*/
140  MICROFEATURES MicroFeatures) {
141 /*
142  ** Parameters:
143  ** Outline outline to extract micro-features from
144  ** MicroFeatures list of micro-features to add to
145  ** Globals: none
146  ** Operation:
147  ** This routine
148  ** Return: List of micro-features with new features added to front.
149  ** Exceptions: none
150  ** History: 7/26/89, DSJ, Created.
151  */
152  MFOUTLINE Current;
153  MFOUTLINE Last;
154  MFOUTLINE First;
156 
157  if (DegenerateOutline (Outline))
158  return (MicroFeatures);
159 
160  First = NextExtremity (Outline);
161  Last = First;
162  do {
163  Current = NextExtremity (Last);
164  if (!PointAt(Current)->Hidden) {
165  NewFeature = ExtractMicroFeature (Last, Current);
166  if (NewFeature != NULL)
167  MicroFeatures = push (MicroFeatures, NewFeature);
168  }
169  Last = Current;
170  }
171  while (Last != First);
172 
173  return (MicroFeatures);
174 } /* ConvertToMicroFeatures */
175 
176 
177 /*---------------------------------------------------------------------------*/
179 /*
180  ** Parameters:
181  ** Start starting point of micro-feature
182  ** End ending point of micro-feature
183  ** Globals: none
184  ** Operation:
185  ** This routine computes the feature parameters which describe
186  ** the micro-feature that starts and Start and ends at End.
187  ** A new micro-feature is allocated, filled with the feature
188  ** parameters, and returned. The routine assumes that
189  ** Start and End are not the same point. If they are the
190  ** same point, NULL is returned, a warning message is
191  ** printed, and the current outline is dumped to stdout.
192  ** Return: New micro-feature or NULL if the feature was rejected.
193  ** Exceptions: none
194  ** History: 7/26/89, DSJ, Created.
195  ** 11/17/89, DSJ, Added handling for Start and End same point.
196  */
198  MFEDGEPT *P1, *P2;
199 
200  P1 = PointAt(Start);
201  P2 = PointAt(End);
202 
203  NewFeature = NewMicroFeature ();
204  NewFeature[XPOSITION] = AverageOf(P1->Point.x, P2->Point.x);
205  NewFeature[YPOSITION] = AverageOf(P1->Point.y, P2->Point.y);
206  NewFeature[MFLENGTH] = DistanceBetween(P1->Point, P2->Point);
207  NewFeature[ORIENTATION] = NormalizedAngleFrom(&P1->Point, &P2->Point, 1.0);
208  NewFeature[FIRSTBULGE] = 0.0f; // deprecated
209  NewFeature[SECONDBULGE] = 0.0f; // deprecated
210 
211  return NewFeature;
212 } /* ExtractMicroFeature */
Definition: blobs.h:261
#define NormalizeAngle(A)
Definition: mfx.cpp:45
#define first_node(l)
Definition: oldlist.h:139
float FLOAT32
Definition: host.h:111
#define FIRSTBULGE
Definition: mfdefs.h:40
void CharNormalizeOutline(MFOUTLINE Outline, const DENORM &cn_denorm)
Definition: mfoutline.cpp:364
#define double_VAR(name, val, comment)
Definition: params.h:286
#define AverageOf(A, B)
Definition: mfoutline.h:60
#define XPOSITION
Definition: mfdefs.h:36
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
#define YPOSITION
Definition: mfdefs.h:37
FLOAT32 NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, FLOAT32 FullScale)
Definition: fpoint.cpp:39
double classify_min_slope
Definition: mfx.cpp:37
#define iterate(l)
Definition: oldlist.h:159
FLOAT32 * MICROFEATURE
Definition: mfdefs.h:33
MFOUTLINE NextExtremity(MFOUTLINE EdgePoint)
Definition: mfoutline.cpp:235
#define SECONDBULGE
Definition: mfdefs.h:41
void FindDirectionChanges(MFOUTLINE Outline, FLOAT32 MinSlope, FLOAT32 MaxSlope)
Definition: mfoutline.cpp:105
MICROFEATURE NewMicroFeature()
Definition: mfdefs.cpp:29
#define AngleFrom(A, B)
Definition: fpoint.h:42
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
MICROFEATURES BlobMicroFeatures(TBLOB *Blob, const DENORM &cn_denorm)
Definition: mfx.cpp:62
#define NIL_LIST
Definition: oldlist.h:126
void MarkDirectionChanges(MFOUTLINE Outline)
Definition: mfoutline.cpp:191
FLOAT32 ComputeOrientation(MFEDGEPT *Start, MFEDGEPT *End)
Definition: mfx.cpp:108
LIST push(LIST list, void *element)
Definition: oldlist.cpp:323
#define ORIENTATION
Definition: mfdefs.h:39
#define NULL
Definition: host.h:144
MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, MICROFEATURES MicroFeatures)
Definition: mfx.cpp:139
FPOINT Point
Definition: mfoutline.h:40
double classify_max_slope
Definition: mfx.cpp:39
MICROFEATURE ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End)
Definition: mfx.cpp:178
LIST MFOUTLINE
Definition: mfoutline.h:33
FLOAT32 x
Definition: fpoint.h:31
#define MFLENGTH
Definition: mfdefs.h:38