classify/picofeat.cpp

Go to the documentation of this file.
00001 
00019 /* =================
00020           Include Files and Type Defines
00021  ==================== */
00022 #include "picofeat.h"
00023 #include "mfoutline.h"
00024 #include "variables.h"
00025 #include "sigmenu.h"
00026 #include "hideedge.h"
00027 #include "fpoint.h"
00028 
00029 #include <math.h>
00030 
00031 #include "ocrfeatures.h"         //Debug
00032 #include <stdio.h>               //Debug
00033 #include "efio.h"                //Debug
00034 //#include "christydbg.h"
00035 
00036 #define PICO_FEATURE_LENGTH 0.05
00037 
00038 /*---------------------------------------------------------------------------
00039           Private Function Prototypes
00040 ----------------------------------------------------------------------------*/
00041 void ConvertSegmentToPicoFeat(FPOINT *Start,
00042                               FPOINT *End,
00043                               FEATURE_SET FeatureSet);
00044 
00045 void ConvertToPicoFeatures2(MFOUTLINE Outline, FEATURE_SET FeatureSet); 
00046 
00047 void NormalizePicoX(FEATURE_SET FeatureSet); 
00048 
00049 /*
00050 #if defined(__STDC__) || defined(__cplusplus)
00051 # define _ARGS(s) s
00052 #else
00053 # define _ARGS(s) ()
00054 #endif*/
00055 
00056 /* /users/danj/wiseowl/src/danj/microfeatures/picofeat.c
00057 void ConvertSegmentToPicoFeat
00058   _ARGS((FPOINT *Start,
00059   FPOINT *End,
00060   FEATURE_SET FeatureSet));
00061 
00062 void ConvertToPicoFeatures2
00063   _ARGS((MFOUTLINE Outline,
00064   FEATURE_SET FeatureSet));
00065 
00066 void NormalizePicoX
00067   _ARGS((FEATURE_SET FeatureSet));
00068 
00069 #undef _ARGS
00070 */
00071 
00072 /* =================
00073  Global Data Definitions and Declarations
00074  ==================== */
00075 
00076 /* =================
00077      Public Code
00078  ==================== */
00089 FEATURE_SET ExtractPicoFeatures(TBLOB *Blob, LINE_STATS *LineStats) { 
00090   LIST Outlines;
00091   LIST RemainingOutlines;
00092   MFOUTLINE Outline;
00093   FEATURE_SET FeatureSet;
00094   FLOAT32 XScale, YScale;
00095 
00096   FeatureSet = NewFeatureSet (MAX_PICO_FEATURES);
00097 
00098   Outlines = ConvertBlob (Blob);
00099 
00100   NormalizeOutlines(Outlines, LineStats, &XScale, &YScale); 
00101   RemainingOutlines = Outlines;
00102   iterate(RemainingOutlines) { 
00103     Outline = (MFOUTLINE) first (RemainingOutlines);
00104     /*---------Debug--------------------------------------------------*
00105     OFile = fopen ("f:/ims/debug/pfOutline.logCPP", "r");
00106     if (OFile == NULL)
00107     {
00108       OFile = Efopen ("f:/ims/debug/pfOutline.logCPP", "w");
00109       WriteOutline(OFile, Outline);
00110     }
00111     else
00112     {
00113       fclose (OFile);
00114       OFile = Efopen ("f:/ims/debug/pfOutline.logCPP", "a");
00115     }
00116     WriteOutline(OFile, Outline);
00117     fclose (OFile);
00118     *--------------------------------------------------------------------*/
00119     ConvertToPicoFeatures2(Outline, FeatureSet); 
00120   }
00121   if (NormMethod == baseline)
00122     NormalizePicoX(FeatureSet); 
00123   /*---------Debug--------------------------------------------------*
00124   File = fopen ("f:/ims/debug/pfFeatSet.logCPP", "r");
00125   if (File == NULL)
00126   {
00127     File = Efopen ("f:/ims/debug/pfFeatSet.logCPP", "w");
00128     WriteFeatureSet(File, FeatureSet);
00129   }
00130   else
00131   {
00132     fclose (File);
00133     File = Efopen ("f:/ims/debug/pfFeatSet.logCPP", "a");
00134   }
00135   WriteFeatureSet(File, FeatureSet);
00136   fclose (File);
00137   *--------------------------------------------------------------------*/
00138   FreeOutlines(Outlines); 
00139   return (FeatureSet);
00140 
00141 }                                /* ExtractPicoFeatures */
00142 
00143 
00144 /* =============================== */
00155 void InitPicoFXVars() { 
00156 
00157    VALUE dummy;
00158    
00159    float_variable (PicoFeatureLength, "PicoFeatureLength",
00160       PICO_FEATURE_LENGTH);
00161    
00162 }  /* InitPicoFXVars */
00163 
00164 /* =================
00165     Private Code
00166  ==================== */
00184 void ConvertSegmentToPicoFeat(FPOINT *Start,
00185                               FPOINT *End,
00186                               FEATURE_SET FeatureSet) {
00187   FEATURE Feature;
00188   FLOAT32 Angle;
00189   FLOAT32 Length;
00190   int NumFeatures;
00191   FPOINT Center;
00192   FPOINT Delta;
00193   int i;
00194 
00195   Angle = NormalizedAngleFrom (Start, End, 1.0);
00196   Length = DistanceBetween (*Start, *End);
00197   NumFeatures = (int) floor (Length / PicoFeatureLength + 0.5);
00198   if (NumFeatures < 1)
00199     NumFeatures = 1;
00200 
00201   /* compute vector for one pico feature */
00202   Xof (Delta) = XDelta (*Start, *End) / NumFeatures;
00203   Yof (Delta) = YDelta (*Start, *End) / NumFeatures;
00204 
00205   /* compute position of first pico feature */
00206   Xof (Center) = Xof (*Start) + Xof (Delta) / 2.0;
00207   Yof (Center) = Yof (*Start) + Yof (Delta) / 2.0;
00208 
00209   /* compute each pico feature in segment and add to feature set */
00210   for (i = 0; i < NumFeatures; i++) {
00211     Feature = NewFeature (&PicoFeatDesc);
00212     ParamOf (Feature, PicoFeatDir) = Angle;
00213     ParamOf (Feature, PicoFeatX) = Xof (Center);
00214     ParamOf (Feature, PicoFeatY) = Yof (Center);
00215     AddFeature(FeatureSet, Feature); 
00216 
00217     Xof (Center) += Xof (Delta);
00218     Yof (Center) += Yof (Delta);
00219   }
00220 }                                /* ConvertSegmentToPicoFeat */
00221 
00222 
00223 /* =============================== */
00243 void ConvertToPicoFeatures2(MFOUTLINE Outline, FEATURE_SET FeatureSet) { 
00244   MFOUTLINE Next;
00245   MFOUTLINE First;
00246   MFOUTLINE Current;
00247 
00248   if (DegenerateOutline (Outline))
00249     return;
00250 
00251   First = Outline;
00252   Current = First;
00253   Next = NextPointAfter (Current);
00254   do {
00260     if (IsVisible (PointAt (Next)))
00261       ConvertSegmentToPicoFeat (&(PositionOf (PointAt (Current))),
00262         &(PositionOf (PointAt (Next))), FeatureSet);
00263 
00264     Current = Next;
00265     Next = NextPointAfter (Current);
00266   }
00267   while (Current != First);
00268 
00269 }                                /* ConvertToPicoFeatures2 */
00270 
00271 
00272 /* =============================== */
00284 void NormalizePicoX(FEATURE_SET FeatureSet) { 
00285   int i;
00286   FEATURE Feature;
00287   FLOAT32 Origin = 0.0;
00288 
00289   for (i = 0; i < NumFeaturesIn (FeatureSet); i++) {
00290     Feature = FeatureIn (FeatureSet, i);
00291     Origin += ParamOf (Feature, PicoFeatX);
00292   }
00293   Origin /= NumFeaturesIn (FeatureSet);
00294 
00295   for (i = 0; i < NumFeaturesIn (FeatureSet); i++) {
00296     Feature = FeatureIn (FeatureSet, i);
00297     ParamOf (Feature, PicoFeatX) -= Origin;
00298   }
00299 }                                /* NormalizePicoX */

Generated on Wed Feb 28 19:49:10 2007 for Tesseract by  doxygen 1.5.1