tesseract v5.3.3.20231005
tesseract::OrientationDetector Class Reference

#include <osdetect.h>

Public Member Functions

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

Detailed Description

Definition at line 83 of file osdetect.h.

Constructor & Destructor Documentation

◆ OrientationDetector()

tesseract::OrientationDetector::OrientationDetector ( const std::vector< int > *  allowed_scripts,
OSResults results 
)

Definition at line 364 of file osdetect.cpp.

364 {
365 osr_ = osr;
366 allowed_scripts_ = allowed_scripts;
367}

Member Function Documentation

◆ detect_blob()

bool tesseract::OrientationDetector::detect_blob ( BLOB_CHOICE_LIST *  scores)

Definition at line 371 of file osdetect.cpp.

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

◆ get_orientation()

int tesseract::OrientationDetector::get_orientation ( )

Definition at line 439 of file osdetect.cpp.

439 {
441 return osr_->best_result.orientation_id;
442}
OSBestResult best_result
Definition: osdetect.h:80
void update_best_orientation()
Definition: osdetect.cpp:63

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