tesseract  4.00.00dev
OrientationDetector Class Reference

#include <osdetect.h>

Public Member Functions

 OrientationDetector (const GenericVector< int > *allowed_scripts, OSResults *results)
 
bool detect_blob (BLOB_CHOICE_LIST *scores)
 
int get_orientation ()
 

Detailed Description

Definition at line 82 of file osdetect.h.

Constructor & Destructor Documentation

◆ OrientationDetector()

OrientationDetector::OrientationDetector ( const GenericVector< int > *  allowed_scripts,
OSResults results 
)

Definition at line 368 of file osdetect.cpp.

369  {
370  osr_ = osr;
371  allowed_scripts_ = allowed_scripts;
372 }

Member Function Documentation

◆ detect_blob()

bool OrientationDetector::detect_blob ( BLOB_CHOICE_LIST *  scores)

Definition at line 376 of file osdetect.cpp.

376  {
377  float blob_o_score[4] = {0.0f, 0.0f, 0.0f, 0.0f};
378  float total_blob_o_score = 0.0f;
379 
380  for (int i = 0; i < 4; ++i) {
381  BLOB_CHOICE_IT choice_it(scores + i);
382  if (!choice_it.empty()) {
383  BLOB_CHOICE* choice = NULL;
384  if (allowed_scripts_ != NULL && !allowed_scripts_->empty()) {
385  // Find the top choice in an allowed script.
386  for (choice_it.mark_cycle_pt(); !choice_it.cycled_list() &&
387  choice == NULL; choice_it.forward()) {
388  int choice_script = choice_it.data()->script_id();
389  int s = 0;
390  for (s = 0; s < allowed_scripts_->size(); ++s) {
391  if ((*allowed_scripts_)[s] == choice_script) {
392  choice = choice_it.data();
393  break;
394  }
395  }
396  }
397  } else {
398  choice = choice_it.data();
399  }
400  if (choice != NULL) {
401  // The certainty score ranges between [-20,0]. This is converted here to
402  // [0,1], with 1 indicating best match.
403  blob_o_score[i] = 1 + 0.05 * choice->certainty();
404  total_blob_o_score += blob_o_score[i];
405  }
406  }
407  }
408  if (total_blob_o_score == 0.0) return false;
409  // Fill in any blanks with the worst score of the others. This is better than
410  // picking an arbitrary probability for it and way better than -inf.
411  float worst_score = 0.0f;
412  int num_good_scores = 0;
413  for (int i = 0; i < 4; ++i) {
414  if (blob_o_score[i] > 0.0f) {
415  ++num_good_scores;
416  if (worst_score == 0.0f || blob_o_score[i] < worst_score)
417  worst_score = blob_o_score[i];
418  }
419  }
420  if (num_good_scores == 1) {
421  // Lower worst if there is only one.
422  worst_score /= 2.0f;
423  }
424  for (int i = 0; i < 4; ++i) {
425  if (blob_o_score[i] == 0.0f) {
426  blob_o_score[i] = worst_score;
427  total_blob_o_score += worst_score;
428  }
429  }
430  // Normalize the orientation scores for the blob and use them to
431  // update the aggregated orientation score.
432  for (int i = 0; total_blob_o_score != 0 && i < 4; ++i) {
433  osr_->orientations[i] += log(blob_o_score[i] / total_blob_o_score);
434  }
435 
436  // TODO(ranjith) Add an early exit test, based on min_orientation_margin,
437  // as used in pagesegmain.cpp.
438  return false;
439 }
bool empty() const
Definition: genericvector.h:91
int script_id() const
Definition: ratngs.h:111
int size() const
Definition: genericvector.h:72
float orientations[4]
Definition: osdetect.h:74
float certainty() const
Definition: ratngs.h:82

◆ get_orientation()

int OrientationDetector::get_orientation ( )

Definition at line 441 of file osdetect.cpp.

441  {
442  osr_->update_best_orientation();
443  return osr_->best_result.orientation_id;
444 }
void update_best_orientation()
Definition: osdetect.cpp:61
OSBestResult best_result
Definition: osdetect.h:79
int orientation_id
Definition: osdetect.h:41

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