tesseract  4.0.0-1-g2a2b
IntegerMatcher Class Reference

#include <intmatcher.h>

Public Member Functions

 IntegerMatcher (tesseract::IntParam *classify_debug_level)
 
void Match (INT_CLASS ClassTemplate, BIT_VECTOR ProtoMask, BIT_VECTOR ConfigMask, int16_t NumFeatures, const INT_FEATURE_STRUCT *Features, tesseract::UnicharRating *Result, int AdaptFeatureThreshold, int Debug, bool SeparateDebugWindows)
 
float ApplyCNCorrection (float rating, int blob_length, int normalization_factor, int matcher_multiplier)
 
int FindGoodProtos (INT_CLASS ClassTemplate, BIT_VECTOR ProtoMask, BIT_VECTOR ConfigMask, uint16_t BlobLength, int16_t NumFeatures, INT_FEATURE_ARRAY Features, PROTO_ID *ProtoArray, int AdaptProtoThreshold, int Debug)
 
int FindBadFeatures (INT_CLASS ClassTemplate, BIT_VECTOR ProtoMask, BIT_VECTOR ConfigMask, uint16_t BlobLength, int16_t NumFeatures, INT_FEATURE_ARRAY Features, FEATURE_ID *FeatureArray, int AdaptFeatureThreshold, int Debug)
 

Static Public Attributes

static const int kIntThetaFudge = 128
 
static const int kEvidenceTableBits = 9
 
static const int kIntEvidenceTruncBits = 14
 
static const float kSEExponentialMultiplier = 0.0
 
static const float kSimilarityCenter = 0.0075
 

Detailed Description

Definition at line 83 of file intmatcher.h.

Constructor & Destructor Documentation

◆ IntegerMatcher()

IntegerMatcher::IntegerMatcher ( tesseract::IntParam classify_debug_level)

Definition at line 670 of file intmatcher.cpp.

671  : classify_debug_level_(classify_debug_level)
672 {
673  /* Initialize table for evidence to similarity lookup */
674  for (int i = 0; i < SE_TABLE_SIZE; i++) {
675  uint32_t IntSimilarity = i << (27 - SE_TABLE_BITS);
676  double Similarity = ((double) IntSimilarity) / 65536.0 / 65536.0;
677  double evidence = Similarity / kSimilarityCenter;
678  evidence = 255.0 / (evidence * evidence + 1.0);
679 
680  if (kSEExponentialMultiplier > 0.0) {
681  double scale = 1.0 - exp(-kSEExponentialMultiplier) *
682  exp(kSEExponentialMultiplier * ((double) i / SE_TABLE_SIZE));
683  evidence *= ClipToRange(scale, 0.0, 1.0);
684  }
685 
686  similarity_evidence_table_[i] = (uint8_t) (evidence + 0.5);
687  }
688 
689  /* Initialize evidence computation variables */
690  evidence_table_mask_ =
691  ((1 << kEvidenceTableBits) - 1) << (9 - kEvidenceTableBits);
692  mult_trunc_shift_bits_ = (14 - kIntEvidenceTruncBits);
693  table_trunc_shift_bits_ = (27 - SE_TABLE_BITS - (mult_trunc_shift_bits_ << 1));
694  evidence_mult_mask_ = ((1 << kIntEvidenceTruncBits) - 1);
695 }
static const float kSEExponentialMultiplier
Definition: intmatcher.h:92
static const int kIntEvidenceTruncBits
Definition: intmatcher.h:90
#define SE_TABLE_SIZE
Definition: intmatcher.h:67
static const float kSimilarityCenter
Definition: intmatcher.h:94
static const int kEvidenceTableBits
Definition: intmatcher.h:88
#define SE_TABLE_BITS
Definition: intmatcher.h:66
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111

Member Function Documentation

◆ ApplyCNCorrection()

float IntegerMatcher::ApplyCNCorrection ( float  rating,
int  blob_length,
int  normalization_factor,
int  matcher_multiplier 
)

Applies the CN normalization factor to the given rating and returns the modified rating.

Definition at line 1195 of file intmatcher.cpp.

1197  {
1198  return (rating * blob_length +
1199  matcher_multiplier * normalization_factor / 256.0) /
1200  (blob_length + matcher_multiplier);
1201 }

◆ FindBadFeatures()

int IntegerMatcher::FindBadFeatures ( INT_CLASS  ClassTemplate,
BIT_VECTOR  ProtoMask,
BIT_VECTOR  ConfigMask,
uint16_t  BlobLength,
int16_t  NumFeatures,
INT_FEATURE_ARRAY  Features,
FEATURE_ID FeatureArray,
int  AdaptFeatureThreshold,
int  Debug 
)

FindBadFeatures finds all features with maximum feature-evidence < AdaptFeatureThresh. The list is ordered by increasing feature number.

Parameters
ClassTemplatePrototypes & tables for a class
ProtoMaskAND Mask for proto word
ConfigMaskAND Mask for config word
BlobLengthLength of unormalized blob
NumFeaturesNumber of features in blob
FeaturesArray of features
FeatureArrayArray of bad features
AdaptFeatureThresholdThreshold for bad features
DebugDebugger flag: 1=debugger on
Returns
Number of bad features in FeatureArray.

Definition at line 618 of file intmatcher.cpp.

627  {
628  ScratchEvidence *tables = new ScratchEvidence();
629  int NumBadFeatures = 0;
630 
631  /* DEBUG opening heading */
632  if (MatchDebuggingOn(Debug))
633  cprintf("Find Bad Features -------------------------------------------\n");
634 
635  tables->Clear(ClassTemplate);
636 
637  for (int Feature = 0; Feature < NumFeatures; Feature++) {
638  UpdateTablesForFeature(
639  ClassTemplate, ProtoMask, ConfigMask, Feature, &Features[Feature],
640  tables, Debug);
641 
642  /* Find Best Evidence for Current Feature */
643  int best = 0;
644  for (int i = 0; i < ClassTemplate->NumConfigs; i++)
645  if (tables->feature_evidence_[i] > best)
646  best = tables->feature_evidence_[i];
647 
648  /* Find Bad Features */
649  if (best < AdaptFeatureThreshold) {
650  *FeatureArray = Feature;
651  FeatureArray++;
652  NumBadFeatures++;
653  }
654  }
655 
656 #ifndef GRAPHICS_DISABLED
657  if (PrintProtoMatchesOn(Debug) || PrintMatchSummaryOn(Debug))
658  DebugFeatureProtoError(ClassTemplate, ProtoMask, ConfigMask, *tables,
659  NumFeatures, Debug);
660 #endif
661 
662  if (MatchDebuggingOn(Debug))
663  cprintf("Match Complete --------------------------------------------\n");
664 
665  delete tables;
666  return NumBadFeatures;
667 }
void cprintf(const char *format,...)
Definition: callcpp.cpp:33
#define MatchDebuggingOn(D)
Definition: intproto.h:193
uint8_t NumConfigs
Definition: intproto.h:108
void Clear(const INT_CLASS class_template)
Definition: intmatcher.cpp:700
#define PrintMatchSummaryOn(D)
Definition: intproto.h:194
uint8_t feature_evidence_[MAX_NUM_CONFIGS]
Definition: intmatcher.h:70
#define PrintProtoMatchesOn(D)
Definition: intproto.h:198

◆ FindGoodProtos()

int IntegerMatcher::FindGoodProtos ( INT_CLASS  ClassTemplate,
BIT_VECTOR  ProtoMask,
BIT_VECTOR  ConfigMask,
uint16_t  BlobLength,
int16_t  NumFeatures,
INT_FEATURE_ARRAY  Features,
PROTO_ID ProtoArray,
int  AdaptProtoThreshold,
int  Debug 
)

FindGoodProtos finds all protos whose normalized proto-evidence exceed classify_adapt_proto_thresh. The list is ordered by increasing proto id number.

Globals:

  • local_matcher_multiplier_ Normalization factor multiplier param ClassTemplate Prototypes & tables for a class param ProtoMask AND Mask for proto word param ConfigMask AND Mask for config word param BlobLength Length of unormalized blob param NumFeatures Number of features in blob param Features Array of features param ProtoArray Array of good protos param AdaptProtoThreshold Threshold for good protos param Debug Debugger flag: 1=debugger on
    Returns
    Number of good protos in ProtoArray.

Definition at line 549 of file intmatcher.cpp.

558  {
559  ScratchEvidence *tables = new ScratchEvidence();
560  int NumGoodProtos = 0;
561 
562  /* DEBUG opening heading */
563  if (MatchDebuggingOn (Debug))
564  cprintf
565  ("Find Good Protos -------------------------------------------\n");
566 
567  tables->Clear(ClassTemplate);
568 
569  for (int Feature = 0; Feature < NumFeatures; Feature++)
570  UpdateTablesForFeature(
571  ClassTemplate, ProtoMask, ConfigMask, Feature, &(Features[Feature]),
572  tables, Debug);
573 
574 #ifndef GRAPHICS_DISABLED
575  if (PrintProtoMatchesOn (Debug) || PrintMatchSummaryOn (Debug))
576  DebugFeatureProtoError(ClassTemplate, ProtoMask, ConfigMask, *tables,
577  NumFeatures, Debug);
578 #endif
579 
580  /* Average Proto Evidences & Find Good Protos */
581  for (int proto = 0; proto < ClassTemplate->NumProtos; proto++) {
582  /* Compute Average for Actual Proto */
583  int Temp = 0;
584  for (int i = 0; i < ClassTemplate->ProtoLengths[proto]; i++)
585  Temp += tables->proto_evidence_[proto][i];
586 
587  Temp /= ClassTemplate->ProtoLengths[proto];
588 
589  /* Find Good Protos */
590  if (Temp >= AdaptProtoThreshold) {
591  *ProtoArray = proto;
592  ProtoArray++;
593  NumGoodProtos++;
594  }
595  }
596 
597  if (MatchDebuggingOn (Debug))
598  cprintf ("Match Complete --------------------------------------------\n");
599  delete tables;
600 
601  return NumGoodProtos;
602 }
void cprintf(const char *format,...)
Definition: callcpp.cpp:33
#define MatchDebuggingOn(D)
Definition: intproto.h:193
uint8_t * ProtoLengths
Definition: intproto.h:110
void Clear(const INT_CLASS class_template)
Definition: intmatcher.cpp:700
#define PrintMatchSummaryOn(D)
Definition: intproto.h:194
uint16_t NumProtos
Definition: intproto.h:106
#define PrintProtoMatchesOn(D)
Definition: intproto.h:198
uint8_t proto_evidence_[MAX_NUM_PROTOS][MAX_PROTO_INDEX]
Definition: intmatcher.h:72

◆ Match()

void IntegerMatcher::Match ( INT_CLASS  ClassTemplate,
BIT_VECTOR  ProtoMask,
BIT_VECTOR  ConfigMask,
int16_t  NumFeatures,
const INT_FEATURE_STRUCT Features,
tesseract::UnicharRating Result,
int  AdaptFeatureThreshold,
int  Debug,
bool  SeparateDebugWindows 
)

IntegerMatcher returns the best configuration and rating for a single class. The class matched against is determined by the uniqueness of the ClassTemplate parameter. The best rating and its associated configuration are returned.

Globals:

  • local_matcher_multiplier_ Normalization factor multiplier param ClassTemplate Prototypes & tables for a class param BlobLength Length of unormalized blob param NumFeatures Number of features in blob param Features Array of features param NormalizationFactor Fudge factor from blob normalization process param Result Class rating & configuration: (0.0 -> 1.0), 0=bad, 1=good param Debug Debugger flag: 1=debugger on
    Returns
    none

Definition at line 470 of file intmatcher.cpp.

478  {
479  ScratchEvidence *tables = new ScratchEvidence();
480  int Feature;
481 
482  if (MatchDebuggingOn (Debug))
483  cprintf ("Integer Matcher -------------------------------------------\n");
484 
485  tables->Clear(ClassTemplate);
486  Result->feature_misses = 0;
487 
488  for (Feature = 0; Feature < NumFeatures; Feature++) {
489  int csum = UpdateTablesForFeature(ClassTemplate, ProtoMask, ConfigMask,
490  Feature, &Features[Feature],
491  tables, Debug);
492  // Count features that were missed over all configs.
493  if (csum == 0)
494  ++Result->feature_misses;
495  }
496 
497 #ifndef GRAPHICS_DISABLED
498  if (PrintProtoMatchesOn(Debug) || PrintMatchSummaryOn(Debug)) {
499  DebugFeatureProtoError(ClassTemplate, ProtoMask, ConfigMask, *tables,
500  NumFeatures, Debug);
501  }
502 
503  if (DisplayProtoMatchesOn(Debug)) {
504  DisplayProtoDebugInfo(ClassTemplate, ProtoMask, ConfigMask,
505  *tables, SeparateDebugWindows);
506  }
507 
508  if (DisplayFeatureMatchesOn(Debug)) {
509  DisplayFeatureDebugInfo(ClassTemplate, ProtoMask, ConfigMask, NumFeatures,
510  Features, AdaptFeatureThreshold, Debug,
511  SeparateDebugWindows);
512  }
513 #endif
514 
515  tables->UpdateSumOfProtoEvidences(ClassTemplate, ConfigMask, NumFeatures);
516  tables->NormalizeSums(ClassTemplate, NumFeatures, NumFeatures);
517 
518  FindBestMatch(ClassTemplate, *tables, Result);
519 
520 #ifndef GRAPHICS_DISABLED
521  if (PrintMatchSummaryOn(Debug))
522  Result->Print();
523 
524  if (MatchDebuggingOn(Debug))
525  cprintf("Match Complete --------------------------------------------\n");
526 #endif
527 
528  delete tables;
529 }
void NormalizeSums(INT_CLASS ClassTemplate, int16_t NumFeatures, int32_t used_features)
#define DisplayProtoMatchesOn(D)
Definition: intproto.h:196
void cprintf(const char *format,...)
Definition: callcpp.cpp:33
void UpdateSumOfProtoEvidences(INT_CLASS ClassTemplate, BIT_VECTOR ConfigMask, int16_t NumFeatures)
#define MatchDebuggingOn(D)
Definition: intproto.h:193
void Clear(const INT_CLASS class_template)
Definition: intmatcher.cpp:700
#define DisplayFeatureMatchesOn(D)
Definition: intproto.h:195
#define PrintMatchSummaryOn(D)
Definition: intproto.h:194
#define PrintProtoMatchesOn(D)
Definition: intproto.h:198

Member Data Documentation

◆ kEvidenceTableBits

const int IntegerMatcher::kEvidenceTableBits = 9
static

Definition at line 88 of file intmatcher.h.

◆ kIntEvidenceTruncBits

const int IntegerMatcher::kIntEvidenceTruncBits = 14
static

Definition at line 90 of file intmatcher.h.

◆ kIntThetaFudge

const int IntegerMatcher::kIntThetaFudge = 128
static

Definition at line 86 of file intmatcher.h.

◆ kSEExponentialMultiplier

const float IntegerMatcher::kSEExponentialMultiplier = 0.0
static

Definition at line 92 of file intmatcher.h.

◆ kSimilarityCenter

const float IntegerMatcher::kSimilarityCenter = 0.0075
static

Definition at line 94 of file intmatcher.h.


The documentation for this class was generated from the following files: