Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
osdetect.cpp File Reference
#include "osdetect.h"
#include "blobbox.h"
#include "blread.h"
#include "colfind.h"
#include "fontinfo.h"
#include "imagefind.h"
#include "linefind.h"
#include "oldlist.h"
#include "qrsequence.h"
#include "ratngs.h"
#include "strngs.h"
#include "tabvector.h"
#include "tesseractclass.h"
#include "textord.h"

Go to the source code of this file.

Functions

void remove_nontext_regions (tesseract::Tesseract *tess, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
int orientation_and_script_detection (STRING &filename, OSResults *osr, tesseract::Tesseract *tess)
int os_detect (TO_BLOCK_LIST *port_blocks, OSResults *osr, tesseract::Tesseract *tess)
int os_detect_blobs (BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
bool os_detect_blob (BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s, OSResults *osr, tesseract::Tesseract *tess)
const int OrientationIdToValue (const int &id)

Variables

const int kMinCharactersToTry = 50
const int kMaxCharactersToTry = 5 * kMinCharactersToTry
const float kSizeRatioToReject = 2.0
const int kMinAcceptableBlobHeight = 10
const float kOrientationAcceptRatio = 1.3
const float kScriptAcceptRatio = 1.3
const float kHanRatioInKorean = 0.7
const float kHanRatioInJapanese = 0.3
const float kNonAmbiguousMargin = 1.0
const int kMinCredibleResolution = 70
const int kDefaultResolution = 300

Function Documentation

int orientation_and_script_detection ( STRING filename,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 189 of file osdetect.cpp.

{
STRING name = filename; //truncated name
const char *lastdot; //of name
TBOX page_box;
lastdot = strrchr (name.string (), '.');
if (lastdot != NULL)
name[lastdot-name.string()] = '\0';
int width = pixGetWidth(tess->pix_binary());
int height = pixGetHeight(tess->pix_binary());
int resolution = pixGetXRes(tess->pix_binary());
// Zero resolution messes up the algorithms, so make sure it is credible.
if (resolution < kMinCredibleResolution)
resolution = kDefaultResolution;
BLOCK_LIST blocks;
if (!read_unlv_file(name, width, height, &blocks))
FullPageBlock(width, height, &blocks);
// Try to remove non-text regions from consideration.
TO_BLOCK_LIST land_blocks, port_blocks;
remove_nontext_regions(tess, &blocks, &port_blocks);
if (port_blocks.empty()) {
// page segmentation did not succeed, so we need to find_components first.
&blocks, &port_blocks);
} else {
page_box.set_left(0);
page_box.set_bottom(0);
page_box.set_right(width);
page_box.set_top(height);
// Filter_blobs sets up the TO_BLOCKs the same as find_components does.
tess->mutable_textord()->filter_blobs(page_box.topright(),
&port_blocks, true);
}
return os_detect(&port_blocks, osr, tess);
}
const int OrientationIdToValue ( const int &  id)

Definition at line 534 of file osdetect.cpp.

{
switch (id) {
case 0:
return 0;
case 1:
return 270;
case 2:
return 180;
case 3:
return 90;
default:
return -1;
}
}
int os_detect ( TO_BLOCK_LIST *  port_blocks,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 236 of file osdetect.cpp.

{
int blobs_total = 0;
TO_BLOCK_IT block_it;
block_it.set_to_list(port_blocks);
BLOBNBOX_CLIST filtered_list;
BLOBNBOX_C_IT filtered_it(&filtered_list);
for (block_it.mark_cycle_pt(); !block_it.cycled_list();
block_it.forward ()) {
TO_BLOCK* to_block = block_it.data();
if (to_block->block->poly_block() &&
!to_block->block->poly_block()->IsText()) continue;
BLOBNBOX_IT bbox_it;
bbox_it.set_to_list(&to_block->blobs);
for (bbox_it.mark_cycle_pt (); !bbox_it.cycled_list ();
bbox_it.forward ()) {
BLOBNBOX* bbox = bbox_it.data();
C_BLOB* blob = bbox->cblob();
TBOX box = blob->bounding_box();
++blobs_total;
float y_x = fabs((box.height() * 1.0) / box.width());
float x_y = 1.0f / y_x;
// Select a >= 1.0 ratio
float ratio = x_y > y_x ? x_y : y_x;
// Blob is ambiguous
if (ratio > kSizeRatioToReject) continue;
if (box.height() < kMinAcceptableBlobHeight) continue;
filtered_it.add_to_end(bbox);
}
}
return os_detect_blobs(&filtered_list, osr, tess);
}
bool os_detect_blob ( BLOBNBOX bbox,
OrientationDetector o,
ScriptDetector s,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 323 of file osdetect.cpp.

{
tess->tess_cn_matching.set_value(true); // turn it on
tess->tess_bn_matching.set_value(false);
C_BLOB* blob = bbox->cblob();
TBLOB* tblob = TBLOB::PolygonalCopy(blob);
TBOX box = tblob->bounding_box();
FCOORD current_rotation(1.0f, 0.0f);
FCOORD rotation90(0.0f, 1.0f);
BLOB_CHOICE_LIST ratings[4];
// Test the 4 orientations
for (int i = 0; i < 4; ++i) {
// Normalize the blob. Set the origin to the place we want to be the
// bottom-middle after rotation.
// Scaling is to make the rotated height the x-height.
float scaling = static_cast<float>(kBlnXHeight) / box.height();
float x_origin = (box.left() + box.right()) / 2.0f;
float y_origin = (box.bottom() + box.top()) / 2.0f;
if (i == 0 || i == 2) {
// Rotation is 0 or 180.
y_origin = i == 0 ? box.bottom() : box.top();
} else {
// Rotation is 90 or 270.
scaling = static_cast<float>(kBlnXHeight) / box.width();
x_origin = i == 1 ? box.left() : box.right();
}
DENORM denorm;
denorm.SetupNormalization(NULL, NULL, &current_rotation, NULL, NULL, 0,
x_origin, y_origin, scaling, scaling,
0.0f, static_cast<float>(kBlnBaselineOffset));
TBLOB* rotated_blob = new TBLOB(*tblob);
rotated_blob->Normalize(denorm);
tess->AdaptiveClassifier(rotated_blob, denorm, ratings + i, NULL);
delete rotated_blob;
current_rotation.rotate(rotation90);
}
delete tblob;
bool stop = o->detect_blob(ratings);
s->detect_blob(ratings);
int orientation = o->get_orientation();
stop = s->must_stop(orientation) && stop;
return stop;
}
int os_detect_blobs ( BLOBNBOX_CLIST *  blob_list,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 275 of file osdetect.cpp.

{
OSResults osr_;
if (osr == NULL)
osr = &osr_;
osr->unicharset = &tess->unicharset;
ScriptDetector s(osr, tess);
BLOBNBOX_C_IT filtered_it(blob_list);
int real_max = MIN(filtered_it.length(), kMaxCharactersToTry);
// printf("Total blobs found = %d\n", blobs_total);
// printf("Number of blobs post-filtering = %d\n", filtered_it.length());
// printf("Number of blobs to try = %d\n", real_max);
// If there are too few characters, skip this page entirely.
if (real_max < kMinCharactersToTry / 2) {
printf("Too few characters. Skipping this page\n");
return 0;
}
BLOBNBOX** blobs = new BLOBNBOX*[filtered_it.length()];
int number_of_blobs = 0;
for (filtered_it.mark_cycle_pt (); !filtered_it.cycled_list ();
filtered_it.forward ()) {
blobs[number_of_blobs++] = (BLOBNBOX*)filtered_it.data();
}
QRSequenceGenerator sequence(number_of_blobs);
int num_blobs_evaluated = 0;
for (int i = 0; i < real_max; ++i) {
if (os_detect_blob(blobs[sequence.GetVal()], &o, &s, osr, tess)
break;
}
++num_blobs_evaluated;
}
delete [] blobs;
// Make sure the best_result is up-to-date
int orientation = o.get_orientation();
osr->update_best_script(orientation);
return num_blobs_evaluated;
}
void remove_nontext_regions ( tesseract::Tesseract tess,
BLOCK_LIST *  blocks,
TO_BLOCK_LIST *  to_blocks 
)

Definition at line 162 of file osdetect.cpp.

{
Pix *pix = tess->pix_binary();
ASSERT_HOST(pix != NULL);
int vertical_x = 0;
int vertical_y = 1;
tesseract::TabVector_LIST v_lines;
tesseract::TabVector_LIST h_lines;
const int kMinCredibleResolution = 70;
int resolution = (kMinCredibleResolution > pixGetXRes(pix)) ?
kMinCredibleResolution : pixGetXRes(pix);
&vertical_x, &vertical_y,
NULL, &v_lines, &h_lines);
if (im_pix != NULL) {
pixSubtract(pix, pix, im_pix);
pixDestroy(&im_pix);
}
blocks, to_blocks);
}

Variable Documentation

const int kDefaultResolution = 300

Definition at line 65 of file osdetect.cpp.

const float kHanRatioInJapanese = 0.3

Definition at line 46 of file osdetect.cpp.

const float kHanRatioInKorean = 0.7

Definition at line 45 of file osdetect.cpp.

const int kMaxCharactersToTry = 5 * kMinCharactersToTry

Definition at line 37 of file osdetect.cpp.

const int kMinAcceptableBlobHeight = 10

Definition at line 40 of file osdetect.cpp.

const int kMinCharactersToTry = 50

Definition at line 36 of file osdetect.cpp.

const int kMinCredibleResolution = 70

Definition at line 63 of file osdetect.cpp.

const float kNonAmbiguousMargin = 1.0

Definition at line 48 of file osdetect.cpp.

const float kOrientationAcceptRatio = 1.3

Definition at line 42 of file osdetect.cpp.

const float kScriptAcceptRatio = 1.3

Definition at line 43 of file osdetect.cpp.

const float kSizeRatioToReject = 2.0

Definition at line 39 of file osdetect.cpp.