⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adaptmatch.cpp

📁 一OCR的相关资料。.希望对研究OCR的朋友有所帮助.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
  _ARGS((ADAPT_RESULTS *Results)); void RemoveExtraPuncs  _ARGS((ADAPT_RESULTS *Results)); void SetAdaptiveThreshold  _ARGS((FLOAT32 Threshold)); void ShowBestMatchFor  _ARGS((BLOB *Blob,  LINE_STATS *LineStats,  CLASS_ID ClassId,  BOOL8 AdaptiveOn,  BOOL8 PreTrainedOn));#undef _ARGS*//**----------------------------------------------------------------------------        Global Data Definitions and Declarations----------------------------------------------------------------------------**//* name of current image file being processed */extern char imagefile[];//extern char                                   *demodir;INT_VAR (tessedit_single_match, FALSE, "Top choice only from CP");//extern "C" int il1_adaption_test; //?//extern int display_ratings;//extern "C" int newcp_ratings_on;//extern int config_pruner_enabled;//extern "C" int feature_prune_percentile;//extern "C" double newcp_duff_rating;/* variables used to hold performance statistics */static int AdaptiveMatcherCalls = 0;static int BaselineClassifierCalls = 0;static int CharNormClassifierCalls = 0;static int AmbigClassifierCalls = 0;static int NumWordsAdaptedTo = 0;static int NumCharsAdaptedTo = 0;static int NumBaselineClassesTried = 0;static int NumCharNormClassesTried = 0;static int NumAmbigClassesTried = 0;static int NumClassesOutput = 0;/* define globals used to hold onto extracted features.  This is usedto map from the old scheme in which baseline features and char normfeatures are extracted separately, to the new scheme in which theyare extracted at the same time. */static BOOL8 FeaturesHaveBeenExtracted = FALSE;static BOOL8 FeaturesOK = TRUE;static INT_FEATURE_ARRAY BaselineFeatures;static INT_FEATURE_ARRAY CharNormFeatures;//static CLASS_NORMALIZATION_ARRAY//                                                      NormalizationAdjustments;static INT_FX_RESULT_STRUCT FXInfo;/* use a global variable to hold onto the current ratings so that thecomparison function passes to qsort can get at them */static FLOAT32 *CurrentRatings;/* define globals to hold filenames of training data */static const char *BuiltInTemplatesFile = BUILT_IN_TEMPLATES_FILE;static const char *BuiltInCutoffsFile = BUILT_IN_CUTOFFS_FILE;static CLASS_CUTOFF_ARRAY CharNormCutoffs;static CLASS_CUTOFF_ARRAY BaselineCutoffs;/* use global variables to hold onto built-in templates and adaptedtemplates */static INT_TEMPLATES PreTrainedTemplates;static ADAPT_TEMPLATES AdaptedTemplates;/* create dummy proto and config masks for use with the built-in templates */static BIT_VECTOR AllProtosOn;static BIT_VECTOR PrunedProtos;static BIT_VECTOR AllConfigsOn;static BIT_VECTOR AllProtosOff;static BIT_VECTOR AllConfigsOff;static BIT_VECTOR TempProtoMask;/* define control knobs for adaptive matcher */make_toggle_const (EnableAdaptiveMatcher, 1, MakeEnableAdaptiveMatcher);/* PREV DEFAULT 0 */make_toggle_const (UsePreAdaptedTemplates, 0, MakeUsePreAdaptedTemplates);make_toggle_const (SaveAdaptedTemplates, 0, MakeSaveAdaptedTemplates);make_toggle_var (EnableAdaptiveDebugger, 0, MakeEnableAdaptiveDebugger,18, 1, SetEnableAdaptiveDebugger, "Enable match debugger");make_int_var (MatcherDebugLevel, 0, MakeMatcherDebugLevel,18, 2, SetMatcherDebugLevel, "Matcher Debug Level: ");make_int_var (MatchDebugFlags, 0, MakeMatchDebugFlags,18, 3, SetMatchDebugFlags, "Matcher Debug Flags: ");make_toggle_var (EnableLearning, 1, MakeEnableLearning,18, 4, SetEnableLearning, "Enable learning");/* PREV DEFAULT 0 */                                 /*record it for multiple pages */static int old_enable_learning = 1;make_int_var (LearningDebugLevel, 0, MakeLearningDebugLevel,18, 5, SetLearningDebugLevel, "Learning Debug Level: ");make_float_var (GoodAdaptiveMatch, 0.125, MakeGoodAdaptiveMatch,18, 6, SetGoodAdaptiveMatch, "Good Match    (0-1): ");make_float_var (GreatAdaptiveMatch, 0.0, MakeGreatAdaptiveMatch,18, 7, SetGreatAdaptiveMatch, "Great Match   (0-1): ");/* PREV DEFAULT 0.10 */make_float_var (PerfectRating, 0.02, MakePerfectRating,18, 8, SetPerfectRating, "Perfect Match (0-1): ");make_float_var (BadMatchPad, 0.15, MakeBadMatchPad,18, 9, SetBadMatchPad, "Bad Match Pad (0-1): ");make_float_var (RatingMargin, 0.1, MakeRatingMargin,18, 10, SetRatingMargin, "New template margin (0-1): ");make_float_var (NoiseBlobLength, 0.6, MakeNoiseBlobLength,18, 11, SetNoiseBlobLength, "Avg. noise blob length: ");make_int_var (MinNumPermClasses, 3, MakeMinNumPermClasses,18, 12, SetMinNumPermClasses, "Min # of permanent classes: ");/* PREV DEFAULT 200 */make_int_var (ReliableConfigThreshold, 2, MakeReliableConfigThreshold,18, 13, SetReliableConfigThreshold,"Reliable Config Threshold: ");make_float_var (MaxAngleDelta, 0.015, MakeMaxAngleDelta,18, 14, SetMaxAngleDelta,"Maximum angle delta for proto clustering: ");make_toggle_var (EnableIntFX, 1, MakeEnableIntFX,18, 15, SetEnableIntFX, "Enable integer fx");/* PREV DEFAULT 0 */make_toggle_var (EnableNewAdaptRules, 1, MakeEnableNewAdaptRules,18, 16, SetEnableNewAdaptRules,"Enable new adaptation rules");/* PREV DEFAULT 0 */make_float_var (RatingScale, 30.0, MakeRatingScale,18, 17, SetRatingScale, "Rating scale: ");make_float_var (CertaintyScale, 20.0, MakeCertaintyScale,18, 18, SetCertaintyScale, "CertaintyScale: ");int tess_cn_matching = 0;int tess_bn_matching = 0;/**----------------------------------------------------------------------------              Public Code----------------------------------------------------------------------------**//*---------------------------------------------------------------------------*/LIST AdaptiveClassifier(TBLOB *Blob, TBLOB *DotBlob, TEXTROW *Row) {/* **							Parameters: **              Blob            blob to be classified **              DotBlob         (obsolete) **              Row             row of text that word appears in **							Globals: **							CurrentRatings              used by compare function for qsort**							Operation: This routine calls the adaptive matcher which returns**		(in an array) the class id of each class matched.  It also**							returns the number of classes matched.**							For each class matched it places the best rating**							found for that class into the Ratings array.**							Bad matches are then removed so that they don't need to be**							sorted.  The remaining good matches are then sorted and**							converted to choices.**							This routine also performs some simple speckle filtering.**							Return: List of choices found by adaptive matcher.**							Exceptions: none**							History: Mon Mar 11 10:00:58 1991, DSJ, Created.*/  LIST Choices;  ADAPT_RESULTS Results;  LINE_STATS LineStats;  if (AdaptedTemplates == NULL)    AdaptedTemplates = NewAdaptedTemplates ();  EnterClassifyMode;  Results.BlobLength = MAX_FLOAT32;  Results.NumMatches = 0;  Results.BestRating = WORST_POSSIBLE_RATING;  Results.BestClass = NO_CLASS;  Results.BestConfig = 0;  GetLineStatsFromRow(Row, &LineStats);  InitMatcherRatings (Results.Ratings);  DoAdaptiveMatch(Blob, &LineStats, &Results);  RemoveBadMatches(&Results);  /* save ratings in a global so that CompareCurrentRatings() can see them */  CurrentRatings = Results.Ratings;  qsort ((void *) (Results.Classes), Results.NumMatches,    sizeof (CLASS_ID), CompareCurrentRatings);  RemoveExtraPuncs(&Results);  Choices = ConvertMatchesToChoices (&Results);  if (MatcherDebugLevel >= 1) {    cprintf ("AD Matches =  ");    PrintAdaptiveMatchResults(stdout, &Results);  }  if (LargeSpeckle (Blob, Row))    Choices = AddLargeSpeckleTo (Choices);#ifndef GRAPHICS_DISABLED  if (EnableAdaptiveDebugger)    DebugAdaptiveClassifier(Blob, &LineStats, &Results);#endif  NumClassesOutput += count (Choices);  if (Choices == NIL) {    if (!bln_numericmode)      printf ("Nil classification!\n");  // Should never normally happen.    return (append_choice (NIL, "", 50.0f, -20.0f, -1));  }  return (Choices);}                                /* AdaptiveClassifier *//*---------------------------------------------------------------------------*/void AdaptToWord(TWERD *Word,                 TEXTROW *Row,                 const char *BestChoice,                 const char *BestRawChoice,                 const char *rejmap) {/* **							Parameters: **							Word              word to be adapted to**							Row              row of text that word is found in**							BestChoice              best choice for word found by system**							BestRawChoice              best choice for word found by classifier only**							Globals:**							EnableLearning              TRUE if learning is enabled**							Operation: This routine implements a preliminary version of the**							rules which are used to decide which characters to adapt to.**							A word is adapted to if it is in the dictionary or if it**							is a "good" number (no trailing units, etc.).  It cannot**							contain broken or merged characters.  Within that word, only**							letters and digits are adapted to (no punctuation).**							Return: none**							Exceptions: none**							History: Thu Mar 14 07:40:36 1991, DSJ, Created.*/  TBLOB *Blob;  LINE_STATS LineStats;  FLOAT32 Thresholds[MAX_ADAPTABLE_WERD_SIZE];  FLOAT32 *Threshold;  const char *map = rejmap;  char map_char = '1';  if (EnableLearning) {    NumWordsAdaptedTo++;    #ifndef SECURE_NAMES    if (LearningDebugLevel >= 1)      cprintf ("\n\nAdapting to word = %s\n", BestChoice);    #endif    GetLineStatsFromRow(Row, &LineStats);    GetAdaptThresholds(Word,                       &LineStats,                       BestChoice,                       BestRawChoice,                       Thresholds);    for (Blob = Word->blobs, Threshold = Thresholds;    Blob != NULL; Blob = Blob->next, BestChoice++, Threshold++) {      InitIntFX();      if (rejmap != NULL)        map_char = *map++;      assert (map_char == '1' || map_char == '0');      if (map_char == '1') {        if (isalnum (*BestChoice)) {          /* SPECIAL RULE:  don't adapt to an 'i' which is the first char             in a word because they are too ambiguous with 'I'.             The new adaptation rules should account for this             automatically, since they exclude ambiguous words from             adaptation, but for safety's sake we'll leave the rule in.             Also, don't adapt to i's that have only 1 blob in them             because this creates too much ambiguity for broken             characters. */          if ((*BestChoice == 'i'            || il1_adaption_test && *BestChoice == 'I'            && islower (BestChoice[1])) && (Blob == Word->blobs            ||            ispunct (*            (BestChoice -            1))            || !il1_adaption_test            &&            NumOutlinesInBlob          (Blob) != 2)) {            if (LearningDebugLevel >= 1)              cprintf ("Rejecting char = %c\n", *BestChoice);          }          else {            #ifndef SECURE_NAMES            if (LearningDebugLevel >= 1)              cprintf ("Adapting to char = %c, thr= %g\n", *BestChoice, *Threshold);            #endif            AdaptToChar(Blob, &LineStats, *BestChoice, *Threshold);          }        }        else          AdaptToPunc(Blob, &LineStats, *BestChoice, *Threshold);      }    }    if (LearningDebugLevel >= 1)      cprintf ("\n");  }}                                /* AdaptToWord *//*---------------------------------------------------------------------------*/void EndAdaptiveClassifier() {/* **							Parameters: none **							Globals: **							AdaptedTemplates              current set of adapted templates**							SaveAdaptedTemplates              TRUE if templates should be saved**							EnableAdaptiveMatcher              TRUE if adaptive matcher is enabled**							Operation: This routine performs cleanup operations on the**							adaptive classifier.  It should be called before the**							program is terminated.  Its main function is to save**							the adapted templates to a file.**							Return: none**							Exceptions: none**							History: Tue Mar 19 14:37:06 1991, DSJ, Created.*/  char Filename[256];  FILE *File;  #ifndef SECURE_NAMES  if (EnableAdaptiveMatcher && SaveAdaptedTemplates) {    strcpy(Filename, imagefile);    strcat(Filename, ADAPT_TEMPLATE_SUFFIX);    File = fopen (Filename, "wb");    if (File == NULL)      cprintf ("Unable to save adapted templates to %s!\n", Filename);    else {      cprintf ("\nSaving adapted templates to %s ...", Filename);      fflush(stdout);      WriteAdaptedTemplates(File, AdaptedTemplates);      cprintf ("\n");      fclose(File);    }  }  #endif  EndDangerousAmbigs();  FreeNormProtos();  free_int_templates(PreTrainedTemplates);  PreTrainedTemplates = NULL;  FreeBitVector(AllProtosOn);  FreeBitVector(PrunedProtos);  FreeBitVector(AllConfigsOn);  FreeBitVector(AllProtosOff);  FreeBitVector(AllConfigsOff);  FreeBitVector(TempProtoMask);  AllProtosOn = NULL;  PrunedProtos = NULL;  AllConfigsOn = NULL;  AllProtosOff = NULL;  AllConfigsOff = NULL;  TempProtoMask = NULL;}                                /* EndAdaptiveClassifier *//*---------------------------------------------------------------------------*/void InitAdaptiveClassifier() {/* **							Parameters: none **							Globals: **							BuiltInTemplatesFile              file to get built-in temps from**							BuiltInCutoffsFile              file to get avg. feat per class from**							PreTrainedTemplates              pre-trained configs and protos**							AdaptedTemplates              templates adapted to current page**							CharNormCutoffs              avg # of features per class**							AllProtosOn              dummy proto mask with all bits 1**							AllConfigsOn              dummy config mask with all bits 1**							UsePreAdaptedTemplates

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -