tesseract 4.1.1
Loading...
Searching...
No Matches
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 **
6 ** (c) Copyright Hewlett-Packard Company, 1988.
7 ** Licensed under the Apache License, Version 2.0 (the "License");
8 ** you may not use this file except in compliance with the License.
9 ** You may obtain a copy of the License at
10 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 ******************************************************************************/
17/*----------------------------------------------------------------------------
18 Include Files and Type Defines
19----------------------------------------------------------------------------*/
20#include "outfeat.h"
21
22#include "classify.h"
23#include "featdefs.h"
24#include "mfoutline.h"
25#include "ocrfeatures.h"
26
27#include <cstdio>
28
29/*----------------------------------------------------------------------------
30 Public Code
31----------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33namespace tesseract {
42 LIST Outlines;
43 LIST RemainingOutlines;
44 MFOUTLINE Outline;
45 FEATURE_SET FeatureSet;
46 float XScale, YScale;
47
49 if (Blob == nullptr)
50 return (FeatureSet);
51
52 Outlines = ConvertBlob (Blob);
53
54 NormalizeOutlines(Outlines, &XScale, &YScale);
55 RemainingOutlines = Outlines;
56 iterate(RemainingOutlines) {
57 Outline = static_cast<MFOUTLINE>first_node (RemainingOutlines);
58 ConvertToOutlineFeatures(Outline, FeatureSet);
59 }
61 NormalizeOutlineX(FeatureSet);
62 FreeOutlines(Outlines);
63 return (FeatureSet);
64} /* ExtractOutlineFeatures */
65} // namespace tesseract
66
67/*----------------------------------------------------------------------------
68 Private Code
69----------------------------------------------------------------------------*/
70/*---------------------------------------------------------------------------*/
84 FPOINT *End,
85 FEATURE_SET FeatureSet) {
86 FEATURE Feature;
87
88 Feature = NewFeature(&OutlineFeatDesc);
89 Feature->Params[OutlineFeatDir] = NormalizedAngleFrom(Start, End, 1.0);
90 Feature->Params[OutlineFeatX] = AverageOf(Start->x, End->x);
91 Feature->Params[OutlineFeatY] = AverageOf(Start->y, End->y);
92 Feature->Params[OutlineFeatLength] = DistanceBetween(*Start, *End);
93 AddFeature(FeatureSet, Feature);
94
95} /* AddOutlineFeatureToSet */
96
97
98/*---------------------------------------------------------------------------*/
108 MFOUTLINE Next;
109 MFOUTLINE First;
110 FPOINT FeatureStart;
111 FPOINT FeatureEnd;
112
113 if (DegenerateOutline (Outline))
114 return;
115
116 First = Outline;
117 Next = First;
118 do {
119 FeatureStart = PointAt(Next)->Point;
120 Next = NextPointAfter(Next);
121
122 /* note that an edge is hidden if the ending point of the edge is
123 marked as hidden. This situation happens because the order of
124 the outlines is reversed when they are converted from the old
125 format. In the old format, a hidden edge is marked by the
126 starting point for that edge. */
127 if (!PointAt(Next)->Hidden) {
128 FeatureEnd = PointAt(Next)->Point;
129 AddOutlineFeatureToSet(&FeatureStart, &FeatureEnd, FeatureSet);
130 }
131 }
132 while (Next != First);
133} /* ConvertToOutlineFeatures */
134
135
136/*---------------------------------------------------------------------------*/
146 int i;
147 FEATURE Feature;
148 float Length;
149 float TotalX = 0.0;
150 float TotalWeight = 0.0;
151 float Origin;
152
153 if (FeatureSet->NumFeatures <= 0)
154 return;
155
156 for (i = 0; i < FeatureSet->NumFeatures; i++) {
157 Feature = FeatureSet->Features[i];
158 Length = Feature->Params[OutlineFeatLength];
159 TotalX += Feature->Params[OutlineFeatX] * Length;
160 TotalWeight += Length;
161 }
162 Origin = TotalX / TotalWeight;
163
164 for (i = 0; i < FeatureSet->NumFeatures; i++) {
165 Feature = FeatureSet->Features[i];
166 Feature->Params[OutlineFeatX] -= Origin;
167 }
168} /* NormalizeOutlineX */
const FEATURE_DESC_STRUCT OutlineFeatDesc
float DistanceBetween(FPOINT A, FPOINT B)
Definition: fpoint.cpp:29
float NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, float FullScale)
Definition: fpoint.cpp:44
LIST ConvertBlob(TBLOB *blob)
Definition: mfoutline.cpp:37
void FreeOutlines(LIST Outlines)
Definition: mfoutline.cpp:167
@ baseline
Definition: mfoutline.h:63
#define AverageOf(A, B)
Definition: mfoutline.h:68
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:78
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:40
FEATURE_SET NewFeatureSet(int NumFeatures)
Definition: ocrfeatures.cpp:94
void NormalizeOutlineX(FEATURE_SET FeatureSet)
Definition: outfeat.cpp:145
void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:107
void AddOutlineFeatureToSet(FPOINT *Start, FPOINT *End, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:83
@ OutlineFeatDir
Definition: outfeat.h:32
@ OutlineFeatX
Definition: outfeat.h:29
@ OutlineFeatY
Definition: outfeat.h:30
@ OutlineFeatLength
Definition: outfeat.h:31
#define MAX_OUTLINE_FEATURES
Definition: outfeat.h:35
#define iterate(l)
Definition: oldlist.h:101
#define first_node(l)
Definition: oldlist.h:92
Definition: blobs.h:284
void NormalizeOutlines(LIST Outlines, float *XScale, float *YScale)
Definition: mfoutline.cpp:276
FEATURE_SET ExtractOutlineFeatures(TBLOB *Blob)
Definition: outfeat.cpp:41
Definition: fpoint.h:29
float y
Definition: fpoint.h:30
float x
Definition: fpoint.h:30
float Params[1]
Definition: ocrfeatures.h:61
FEATURE Features[1]
Definition: ocrfeatures.h:68
uint16_t NumFeatures
Definition: ocrfeatures.h:66