tesseract  4.0.0-1-g2a2b
picofeat.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: picofeat.c
3  ** Purpose: Definition of pico-features.
4  ** Author: Dan Johnson
5  ** History: 9/4/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 "picofeat.h"
22 
23 #include "classify.h"
24 #include "featdefs.h"
25 #include "fpoint.h"
26 #include "mfoutline.h"
27 #include "ocrfeatures.h"
28 #include "params.h"
29 #include "trainingsample.h"
30 
31 #include <cmath>
32 #include <cstdio>
33 
34 /*---------------------------------------------------------------------------
35  Variables
36 ----------------------------------------------------------------------------*/
37 
38 double_VAR(classify_pico_feature_length, 0.05, "Pico Feature Length");
39 
40 /*---------------------------------------------------------------------------
41  Private Function Prototypes
42 ----------------------------------------------------------------------------*/
44  FPOINT *End,
45  FEATURE_SET FeatureSet);
46 
47 void ConvertToPicoFeatures2(MFOUTLINE Outline, FEATURE_SET FeatureSet);
48 
49 void NormalizePicoX(FEATURE_SET FeatureSet);
50 
51 /*----------------------------------------------------------------------------
52  Public Code
53 ----------------------------------------------------------------------------*/
54 /*---------------------------------------------------------------------------*/
55 namespace tesseract {
65  LIST Outlines;
66  LIST RemainingOutlines;
67  MFOUTLINE Outline;
68  FEATURE_SET FeatureSet;
69  float XScale, YScale;
70 
71  FeatureSet = NewFeatureSet(MAX_PICO_FEATURES);
72  Outlines = ConvertBlob(Blob);
73  NormalizeOutlines(Outlines, &XScale, &YScale);
74  RemainingOutlines = Outlines;
75  iterate(RemainingOutlines) {
76  Outline = (MFOUTLINE) first_node (RemainingOutlines);
77  ConvertToPicoFeatures2(Outline, FeatureSet);
78  }
80  NormalizePicoX(FeatureSet);
81  FreeOutlines(Outlines);
82  return (FeatureSet);
83 
84 } /* ExtractPicoFeatures */
85 } // namespace tesseract
86 
87 /*----------------------------------------------------------------------------
88  Private Code
89 ----------------------------------------------------------------------------*/
90 /*---------------------------------------------------------------------------*/
105  FPOINT *End,
106  FEATURE_SET FeatureSet) {
107  FEATURE Feature;
108  float Angle;
109  float Length;
110  int NumFeatures;
111  FPOINT Center;
112  FPOINT Delta;
113  int i;
114 
115  Angle = NormalizedAngleFrom (Start, End, 1.0);
116  Length = DistanceBetween (*Start, *End);
117  NumFeatures = (int) floor (Length / classify_pico_feature_length + 0.5);
118  if (NumFeatures < 1)
119  NumFeatures = 1;
120 
121  /* compute vector for one pico feature */
122  Delta.x = XDelta (*Start, *End) / NumFeatures;
123  Delta.y = YDelta (*Start, *End) / NumFeatures;
124 
125  /* compute position of first pico feature */
126  Center.x = Start->x + Delta.x / 2.0;
127  Center.y = Start->y + Delta.y / 2.0;
128 
129  /* compute each pico feature in segment and add to feature set */
130  for (i = 0; i < NumFeatures; i++) {
131  Feature = NewFeature (&PicoFeatDesc);
132  Feature->Params[PicoFeatDir] = Angle;
133  Feature->Params[PicoFeatX] = Center.x;
134  Feature->Params[PicoFeatY] = Center.y;
135  AddFeature(FeatureSet, Feature);
136 
137  Center.x += Delta.x;
138  Center.y += Delta.y;
139  }
140 } /* ConvertSegmentToPicoFeat */
141 
142 
143 /*---------------------------------------------------------------------------*/
156 void ConvertToPicoFeatures2(MFOUTLINE Outline, FEATURE_SET FeatureSet) {
157  MFOUTLINE Next;
158  MFOUTLINE First;
159  MFOUTLINE Current;
160 
161  if (DegenerateOutline(Outline))
162  return;
163 
164  First = Outline;
165  Current = First;
166  Next = NextPointAfter(Current);
167  do {
168  /* note that an edge is hidden if the ending point of the edge is
169  marked as hidden. This situation happens because the order of
170  the outlines is reversed when they are converted from the old
171  format. In the old format, a hidden edge is marked by the
172  starting point for that edge. */
173  if (!(PointAt(Next)->Hidden))
174  ConvertSegmentToPicoFeat (&(PointAt(Current)->Point),
175  &(PointAt(Next)->Point), FeatureSet);
176 
177  Current = Next;
178  Next = NextPointAfter(Current);
179  }
180  while (Current != First);
181 
182 } /* ConvertToPicoFeatures2 */
183 
184 
185 /*---------------------------------------------------------------------------*/
195 void NormalizePicoX(FEATURE_SET FeatureSet) {
196  int i;
197  FEATURE Feature;
198  float Origin = 0.0;
199 
200  for (i = 0; i < FeatureSet->NumFeatures; i++) {
201  Feature = FeatureSet->Features[i];
202  Origin += Feature->Params[PicoFeatX];
203  }
204  Origin /= FeatureSet->NumFeatures;
205 
206  for (i = 0; i < FeatureSet->NumFeatures; i++) {
207  Feature = FeatureSet->Features[i];
208  Feature->Params[PicoFeatX] -= Origin;
209  }
210 } /* NormalizePicoX */
211 
212 namespace tesseract {
213 /*---------------------------------------------------------------------------*/
220  const TBLOB& blob, const INT_FX_RESULT_STRUCT& fx_info) {
221  INT_FX_RESULT_STRUCT local_fx_info(fx_info);
224  blob, false, &local_fx_info, &bl_features);
225  if (sample == nullptr) return nullptr;
226 
227  uint32_t num_features = sample->num_features();
228  const INT_FEATURE_STRUCT* features = sample->features();
229  FEATURE_SET feature_set = NewFeatureSet(num_features);
230  for (uint32_t f = 0; f < num_features; ++f) {
231  FEATURE feature = NewFeature(&IntFeatDesc);
232 
233  feature->Params[IntX] = features[f].X;
234  feature->Params[IntY] = features[f].Y;
235  feature->Params[IntDir] = features[f].Theta;
236  AddFeature(feature_set, feature);
237  }
238  delete sample;
239 
240  return feature_set;
241 } /* ExtractIntCNFeatures */
242 
243 /*---------------------------------------------------------------------------*/
250  const TBLOB& blob, const INT_FX_RESULT_STRUCT& fx_info) {
251  INT_FX_RESULT_STRUCT local_fx_info(fx_info);
254  blob, false, &local_fx_info, &bl_features);
255  if (sample == nullptr) return nullptr;
256 
257  FEATURE_SET feature_set = NewFeatureSet(1);
258  FEATURE feature = NewFeature(&IntFeatDesc);
259 
260  feature->Params[GeoBottom] = sample->geo_feature(GeoBottom);
261  feature->Params[GeoTop] = sample->geo_feature(GeoTop);
262  feature->Params[GeoWidth] = sample->geo_feature(GeoWidth);
263  AddFeature(feature_set, feature);
264  delete sample;
265 
266  return feature_set;
267 } /* ExtractIntGeoFeatures */
268 
269 } // namespace tesseract.
Definition: picofeat.h:31
TrainingSample * BlobToTrainingSample(const TBLOB &blob, bool nonlinear_norm, INT_FX_RESULT_STRUCT *fx_info, GenericVector< INT_FEATURE_STRUCT > *bl_features)
Definition: intfx.cpp:79
void NormalizeOutlines(LIST Outlines, float *XScale, float *YScale)
Definition: mfoutline.cpp:285
FEATURE_SET ExtractIntCNFeatures(const TBLOB &blob, const INT_FX_RESULT_STRUCT &fx_info)
Definition: picofeat.cpp:219
FEATURE_SET ExtractIntGeoFeatures(const TBLOB &blob, const INT_FX_RESULT_STRUCT &fx_info)
Definition: picofeat.cpp:249
FEATURE_SET NewFeatureSet(int NumFeatures)
Definition: ocrfeatures.cpp:97
Definition: cluster.h:32
void FreeOutlines(LIST Outlines)
Definition: mfoutline.cpp:172
#define DegenerateOutline(O)
Definition: mfoutline.h:67
#define double_VAR(name, val, comment)
Definition: params.h:285
float DistanceBetween(FPOINT A, FPOINT B)
Definition: fpoint.cpp:28
float NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, float FullScale)
Definition: fpoint.cpp:44
const FEATURE_DESC_STRUCT IntFeatDesc
Definition: fpoint.h:30
#define PointAt(O)
Definition: mfoutline.h:68
#define MAX_PICO_FEATURES
Definition: picofeat.h:46
float Params[1]
Definition: ocrfeatures.h:62
double classify_pico_feature_length
Definition: picofeat.cpp:38
FEATURE Features[1]
Definition: ocrfeatures.h:69
LIST ConvertBlob(TBLOB *blob)
Definition: mfoutline.cpp:39
#define XDelta(A, B)
Definition: fpoint.h:39
void ConvertSegmentToPicoFeat(FPOINT *Start, FPOINT *End, FEATURE_SET FeatureSet)
Definition: picofeat.cpp:104
void ConvertToPicoFeatures2(MFOUTLINE Outline, FEATURE_SET FeatureSet)
Definition: picofeat.cpp:156
uint16_t NumFeatures
Definition: ocrfeatures.h:67
Definition: picofeat.h:30
float y
Definition: fpoint.h:31
void NormalizePicoX(FEATURE_SET FeatureSet)
Definition: picofeat.cpp:195
#define first_node(l)
Definition: oldlist.h:141
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:81
#define NextPointAfter(E)
Definition: mfoutline.h:69
#define iterate(l)
Definition: oldlist.h:161
TESS_API const FEATURE_DESC_STRUCT PicoFeatDesc
Definition: blobs.h:268
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:41
float x
Definition: fpoint.h:31
LIST MFOUTLINE
Definition: mfoutline.h:34
#define YDelta(A, B)
Definition: fpoint.h:40
FEATURE_SET ExtractPicoFeatures(TBLOB *Blob)
Definition: picofeat.cpp:64