Tesseract  3.02
 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 /*---------------------------------------------------------------------------*/
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  FLOAT32 XScale, YScale;
78  LIST Outlines;
79  LIST RemainingOutlines;
80  MFOUTLINE Outline;
81  INT_FEATURE_ARRAY blfeatures;
82  INT_FEATURE_ARRAY cnfeatures;
83  INT_FX_RESULT_STRUCT results;
84 
85  if (Blob != NULL) {
86  Outlines = ConvertBlob (Blob);
87  if (!ExtractIntFeat(Blob, denorm, blfeatures, cnfeatures, &results))
88  return NULL;
89  XScale = 0.2f / results.Ry;
90  YScale = 0.2f / results.Rx;
91 
92  RemainingOutlines = Outlines;
93  iterate(RemainingOutlines) {
94  Outline = (MFOUTLINE) first_node (RemainingOutlines);
95  CharNormalizeOutline (Outline,
96  results.Xmean, results.Ymean,
97  XScale, YScale);
98  }
99 
100  RemainingOutlines = Outlines;
101  iterate(RemainingOutlines) {
102  Outline = (MFOUTLINE) first_node (RemainingOutlines);
104  MarkDirectionChanges(Outline);
105  MicroFeatures = ConvertToMicroFeatures (Outline, MicroFeatures);
106  }
107  FreeOutlines(Outlines);
108  }
109  return ((CHAR_FEATURES) MicroFeatures);
110 } /* BlobMicroFeatures */
111 
112 
113 /*---------------------------------------------------------------------------
114  Private Code
115 ---------------------------------------------------------------------------*/
116 
117 /*---------------------------------------------------------------------------*/
119 /*
120  ** Parameters:
121  ** Start starting edge point of micro-feature
122  ** End ending edge point of micro-feature
123  ** Globals: none
124  ** Operation:
125  ** This routine computes the orientation parameter of the
126  ** specified micro-feature. The orientation is the angle of
127  ** the vector from Start to End. It is normalized to a number
128  ** between 0 and 1 where 0 corresponds to 0 degrees and 1
129  ** corresponds to 360 degrees. The actual range is [0,1), i.e.
130  ** 1 is excluded from the range (since it is actual the
131  ** same orientation as 0). This routine assumes that Start
132  ** and End are not the same point.
133  ** Return: Orientation parameter for the specified micro-feature.
134  ** Exceptions: none
135  ** History: 7/27/89, DSJ, Created.
136  */
138 
139  Orientation = NormalizeAngle (AngleFrom (Start->Point, End->Point));
140 
141  /* ensure that round-off errors do not put circular param out of range */
142  if ((Orientation < 0) || (Orientation >= 1))
143  Orientation = 0;
144  return (Orientation);
145 } /* ComputeOrientation */
146 
147 
148 /*---------------------------------------------------------------------------*/
150  MICROFEATURES MicroFeatures) {
151 /*
152  ** Parameters:
153  ** Outline outline to extract micro-features from
154  ** MicroFeatures list of micro-features to add to
155  ** Globals: none
156  ** Operation:
157  ** This routine
158  ** Return: List of micro-features with new features added to front.
159  ** Exceptions: none
160  ** History: 7/26/89, DSJ, Created.
161  */
162  MFOUTLINE Current;
163  MFOUTLINE Last;
164  MFOUTLINE First;
166 
167  if (DegenerateOutline (Outline))
168  return (MicroFeatures);
169 
170  First = NextExtremity (Outline);
171  Last = First;
172  do {
173  Current = NextExtremity (Last);
174  if (!PointAt(Current)->Hidden) {
175  NewFeature = ExtractMicroFeature (Last, Current);
176  if (NewFeature != NULL)
177  MicroFeatures = push (MicroFeatures, NewFeature);
178  }
179  Last = Current;
180  }
181  while (Last != First);
182 
183  return (MicroFeatures);
184 } /* ConvertToMicroFeatures */
185 
186 
187 /*---------------------------------------------------------------------------*/
189 /*
190  ** Parameters:
191  ** Start starting point of micro-feature
192  ** End ending point of micro-feature
193  ** Globals: none
194  ** Operation:
195  ** This routine computes the feature parameters which describe
196  ** the micro-feature that starts and Start and ends at End.
197  ** A new micro-feature is allocated, filled with the feature
198  ** parameters, and returned. The routine assumes that
199  ** Start and End are not the same point. If they are the
200  ** same point, NULL is returned, a warning message is
201  ** printed, and the current outline is dumped to stdout.
202  ** Return: New micro-feature or NULL if the feature was rejected.
203  ** Exceptions: none
204  ** History: 7/26/89, DSJ, Created.
205  ** 11/17/89, DSJ, Added handling for Start and End same point.
206  */
208  MFEDGEPT *P1, *P2;
209 
210  P1 = PointAt(Start);
211  P2 = PointAt(End);
212 
213  NewFeature = NewMicroFeature ();
214  NewFeature[XPOSITION] = AverageOf(P1->Point.x, P2->Point.x);
215  NewFeature[YPOSITION] = AverageOf(P1->Point.y, P2->Point.y);
216  NewFeature[MFLENGTH] = DistanceBetween(P1->Point, P2->Point);
217  NewFeature[ORIENTATION] = NormalizedAngleFrom(&P1->Point, &P2->Point, 1.0);
218  NewFeature[FIRSTBULGE] = 0.0f; // deprecated
219  NewFeature[SECONDBULGE] = 0.0f; // deprecated
220 
221  return NewFeature;
222 } /* ExtractMicroFeature */