#include "protos.h"
#include "cluster.h"
#include "ocrfeatures.h"
#include "training.h"
Go to the source code of this file.
#define CONFIG_SUFFIX ".cl" |
#define MAX_LENGTH_MISMATCH (2.0 * GetPicoFeatureLength ()) |
#define PROTO_SUFFIX ".mf.p" |
#define WORST_EVIDENCE (1.0) |
#define WORST_MATCH_ALLOWED (0.9) |
(c) Copyright Hewlett-Packard Company, 1988. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Definition at line 29 of file mergenf.h.
Referenced by FindClosestExistingProto().
enum PICO_FEAT_PARAM_NAME |
Definition at line 45 of file mergenf.h.
00046 { 00047 PicoFeatY, 00048 PicoFeatDir, 00049 PicoFeatX 00050 } PICO_FEAT_PARAM_NAME;
Compare two prototypes and compute worst evidence rating.
p1 | proto to compare | |
p2 | proto to compare |
In other words, if p1 were broken into pico-features and each pico-feature was matched to p2, what is the worst evidence rating that will be achieved for any pico-feature.
Definition at line 94 of file mergenf.cpp.
References DummyFastMatch(), FreeFeature(), GetPicoFeatureLength, MAX_LENGTH_MISMATCH, NewFeature(), ParamOf, PI, PicoFeatDesc, PicoFeatDir, PicoFeatX, PicoFeatY, ProtoAngle, ProtoLength, ProtoX, ProtoY, SubfeatureEvidence(), and WORST_EVIDENCE.
Referenced by FindClosestExistingProto().
00097 { 00098 FEATURE Feature; 00099 FLOAT32 WorstEvidence = WORST_EVIDENCE; 00100 FLOAT32 Evidence; 00101 FLOAT32 Angle, Length; 00102 00103 /* if p1 and p2 are not close in length, don't let them match */ 00104 Length = fabs (ProtoLength (p1) - ProtoLength (p2)); 00105 if (Length > MAX_LENGTH_MISMATCH) 00106 return (0.0); 00107 00108 /* create a dummy pico-feature to be used for comparisons */ 00109 Feature = NewFeature (&PicoFeatDesc); 00110 ParamOf (Feature, PicoFeatDir) = ProtoAngle (p1); 00111 00112 /* convert angle to radians */ 00113 Angle = ProtoAngle (p1) * 2.0 * PI; 00114 00115 /* find distance from center of p1 to 1/2 picofeat from end */ 00116 Length = ProtoLength (p1) / 2.0 - GetPicoFeatureLength () / 2.0; 00117 if (Length < 0) Length = 0; 00118 00119 /* set the dummy pico-feature at one end of p1 and match it to p2 */ 00120 ParamOf (Feature, PicoFeatX) = ProtoX (p1) + cos (Angle) * Length; 00121 ParamOf (Feature, PicoFeatY) = ProtoY (p1) + sin (Angle) * Length; 00122 if (DummyFastMatch (Feature, p2)) 00123 { 00124 Evidence = SubfeatureEvidence (Feature, p2); 00125 if (Evidence < WorstEvidence) 00126 WorstEvidence = Evidence; 00127 } 00128 else 00129 { 00130 FreeFeature (Feature); 00131 return (0.0); 00132 } 00133 00134 /* set the dummy pico-feature at the other end of p1 and match it to p2 */ 00135 ParamOf (Feature, PicoFeatX) = ProtoX (p1) - cos (Angle) * Length; 00136 ParamOf (Feature, PicoFeatY) = ProtoY (p1) - sin (Angle) * Length; 00137 if (DummyFastMatch (Feature, p2)) 00138 { 00139 Evidence = SubfeatureEvidence (Feature, p2); 00140 if (Evidence < WorstEvidence) 00141 WorstEvidence = Evidence; 00142 } 00143 else 00144 { 00145 FreeFeature (Feature); 00146 return (0.0); 00147 } 00148 00149 FreeFeature (Feature); 00150 return (WorstEvidence); 00151 00152 }// CompareProtos
Computes a proto which is the weighted average of protos p1 and p2.
p1 | proto to merge | |
p2 | proto to merge | |
w1 | Weight of p1 | |
w2 | Weight of p2 | |
MergedProto | Place to put resulting merged proto |
Definition at line 167 of file mergenf.cpp.
References FillABC(), ProtoAngle, ProtoLength, ProtoX, and ProtoY.
Referenced by FindClosestExistingProto().
00173 { 00174 FLOAT32 TotalWeight; 00175 00176 TotalWeight = w1 + w2; 00177 w1 /= TotalWeight; 00178 w2 /= TotalWeight; 00179 00180 ProtoX (MergedProto) = ProtoX (p1) * w1 + ProtoX (p2) * w2; 00181 ProtoY (MergedProto) = ProtoY (p1) * w1 + ProtoY (p2) * w2; 00182 ProtoLength (MergedProto) = ProtoLength (p1) * w1 + ProtoLength (p2) * w2; 00183 ProtoAngle (MergedProto) = ProtoAngle (p1) * w1 + ProtoAngle (p2) * w2; 00184 FillABC (MergedProto); 00185 00186 }// ComputeMergedProto
void ComputePaddedBoundingBox | ( | PROTO | Proto, | |
FLOAT32 | TangentPad, | |||
FLOAT32 | OrthogonalPad, | |||
FRECT * | BoundingBox | |||
) |
Computes padded bounding box for Proto.
Proto | Proto to compute bounding box for | |
TangentPad | Amount of pad to add in direction of segment | |
OrthogonalPad | Amount of pad to add orthogonal to segment | |
BoundingBox | Place to put results |
Definition at line 407 of file mergenf.cpp.
References MAX, FRECT::MaxX, FRECT::MaxY, FRECT::MinX, FRECT::MinY, PI, ProtoAngle, ProtoLength, ProtoX, and ProtoY.
Referenced by DummyFastMatch().
00412 { 00413 FLOAT32 Pad, Length, Angle; 00414 FLOAT32 CosOfAngle, SinOfAngle; 00415 00416 Length = ProtoLength (Proto) / 2.0 + TangentPad; 00417 Angle = ProtoAngle (Proto) * 2.0 * PI; 00418 CosOfAngle = fabs (cos (Angle)); 00419 SinOfAngle = fabs (sin (Angle)); 00420 00421 Pad = MAX (CosOfAngle * Length, SinOfAngle * OrthogonalPad); 00422 BoundingBox->MinX = ProtoX (Proto) - Pad; 00423 BoundingBox->MaxX = ProtoX (Proto) + Pad; 00424 00425 Pad = MAX (SinOfAngle * Length, CosOfAngle * OrthogonalPad); 00426 BoundingBox->MinY = ProtoY (Proto) - Pad; 00427 BoundingBox->MaxY = ProtoY (Proto) + Pad; 00428 00429 }// ComputePaddedBoundingBox
Check if Feature would be matched by a fast match table built from Proto.
Feature | Feature to be "fast matched" to prototype | |
Proto | Prototype being "fast matched" against |
Definition at line 363 of file mergenf.cpp.
References ComputePaddedBoundingBox(), FALSE, GetPicoFeatureLength, ParamOf, PicoFeatDir, PicoFeatX, PicoFeatY, PointInside(), and ProtoAngle.
Referenced by CompareProtos().
00366 { 00367 FRECT BoundingBox; 00368 FLOAT32 MaxAngleError; 00369 FLOAT32 AngleError; 00370 00371 MaxAngleError = AnglePad / 360.0; 00372 AngleError = fabs (ProtoAngle (Proto) - ParamOf (Feature, PicoFeatDir)); 00373 if (AngleError > 0.5) 00374 AngleError = 1.0 - AngleError; 00375 00376 if (AngleError > MaxAngleError) 00377 return (FALSE); 00378 00379 ComputePaddedBoundingBox (Proto, 00380 TangentBBoxPad * GetPicoFeatureLength (), 00381 OrthogonalBBoxPad * GetPicoFeatureLength (), 00382 &BoundingBox); 00383 00384 return (PointInside (&BoundingBox, 00385 ParamOf (Feature, PicoFeatX), 00386 ParamOf (Feature, PicoFeatY))); 00387 00388 }// DummyFastMatch
Computes evidence Computes new type of evidence number corresponding to this distance value, no longer based on the chi-squared approximation. The new equation that represents the transform is:.
Similarity | Distance value (=Distance^2 + Dangle^2, see SubfeatureEvidence()) |
1 / (1 + (sim / midpoint) ^ curl)
Definition at line 320 of file mergenf.cpp.
Referenced by SubfeatureEvidence().
00322 { 00323 00324 Similarity /= SimilarityMidpoint; 00325 00326 if (SimilarityCurl == 3) 00327 Similarity = Similarity * Similarity * Similarity; 00328 else if (SimilarityCurl == 2) 00329 Similarity = Similarity * Similarity; 00330 else 00331 Similarity = pow (Similarity, SimilarityCurl); 00332 00333 return (1.0 / (1.0 + Similarity)); 00334 }// EvidenceOf
int FindClosestExistingProto | ( | CLASS_TYPE | Class, | |
int | NumMerged[], | |||
PROTOTYPE * | Prototype | |||
) |
Find Id of prototype closest matching Prototype in Class.
Class | Class(font) to search for matching old proto in | |
NumMerged | Number of protos merged into each proto of Class | |
Prototype | New proto to find match for |
Definition at line 202 of file mergenf.cpp.
References CompareProtos(), ComputeMergedProto(), MakeNewFromOld(), MIN, NO_PROTO, NumProtosIn, ProtoIn, and WORST_MATCH_ALLOWED.
00206 { 00207 PROTO_STRUCT NewProto; 00208 PROTO_STRUCT MergedProto; 00209 int Pid; 00210 PROTO Proto; 00211 int BestProto; 00212 FLOAT32 BestMatch; 00213 FLOAT32 Match, OldMatch, NewMatch; 00214 00215 MakeNewFromOld (&NewProto, Prototype); 00216 00217 BestProto = NO_PROTO; 00218 BestMatch = WORST_MATCH_ALLOWED; 00219 for (Pid = 0; Pid < NumProtosIn (Class); Pid++) 00220 { 00221 Proto = ProtoIn (Class, Pid); 00222 ComputeMergedProto (Proto, &NewProto, 00223 (FLOAT32) NumMerged[Pid], 1.0, &MergedProto); 00224 OldMatch = CompareProtos (Proto, &MergedProto); 00225 NewMatch = CompareProtos (&NewProto, &MergedProto); 00226 Match = MIN (OldMatch, NewMatch); 00227 if (Match > BestMatch) 00228 { 00229 BestProto = Pid; 00230 BestMatch = Match; 00231 } 00232 }//for 00233 return (BestProto); 00234 00235 }// FindClosestExistingProto
void InitFastTrainerVars | ( | ) |
Initialize all of control-variables for fast trainer.
Done once in fasttrain
Definition at line 342 of file mergenf.cpp.
00343 { 00344 MakeTangentBBoxPad (); 00345 MakeOrthogonalBBoxPad (); 00346 MakeAnglePad (); 00347 00348 }// InitFastTrainerVars
void InitSubfeatureVars | ( | ) |
Create and set up all menus and variables needed for this file.
Definition at line 265 of file mergenf.cpp.
00266 { 00267 MakeAngleMatchScale (); 00268 MakeSimilarityCurl (); 00269 MakeSimilarityMidpoint (); 00270 }
Fill in fields of New proto based on those of Old proto.
New | New proto to be filled in | |
Old | Old proto to be converted |
Definition at line 247 of file mergenf.cpp.
References CenterX, CenterY, FillABC(), LengthOf, proto::Mean, OrientationOf, ProtoAngle, ProtoLength, ProtoX, and ProtoY.
Referenced by FindClosestExistingProto().
00250 { 00251 ProtoX (New) = CenterX (Old->Mean); 00252 ProtoY (New) = CenterY (Old->Mean); 00253 ProtoLength (New) = LengthOf (Old->Mean); 00254 ProtoAngle (New) = OrientationOf (Old->Mean); 00255 FillABC (New); 00256 00257 }// MakeNewFromOld
Check if point (X,Y) is inside area of Rectangle.
Rectangle | ||
X | ||
Y |
Definition at line 443 of file mergenf.cpp.
References FALSE, FRECT::MaxX, FRECT::MaxY, and TRUE.
Referenced by DummyFastMatch().
00447 { 00448 if (X < Rectangle->MinX) return (FALSE); 00449 if (X > Rectangle->MaxX) return (FALSE); 00450 if (Y < Rectangle->MinY) return (FALSE); 00451 if (Y > Rectangle->MaxY) return (FALSE); 00452 return (TRUE); 00453 00454 }// PointInside
Compare a feature to a prototype.
Feature | Feature to compare to prototype | |
Proto | Prototype to compare to feature |
Definition at line 281 of file mergenf.cpp.
References CoefficientA, CoefficientB, CoefficientC, Distance, EvidenceOf(), ParamOf, PicoFeatDir, PicoFeatX, PicoFeatY, and ProtoAngle.
Referenced by CompareProtos().
00284 { 00285 float Distance; 00286 float Dangle; 00287 00288 Dangle = ProtoAngle (Proto) - ParamOf(Feature, PicoFeatDir); 00289 if (Dangle < -0.5) Dangle += 1.0; 00290 if (Dangle > 0.5) Dangle -= 1.0; 00291 Dangle *= AngleMatchScale; 00292 00293 Distance = CoefficientA (Proto) * ParamOf(Feature, PicoFeatX) + 00294 CoefficientB (Proto) * ParamOf(Feature, PicoFeatY) + 00295 CoefficientC (Proto); 00296 00297 return (EvidenceOf (Distance * Distance + Dangle * Dangle)); 00298 }// SubfeatureEvidence