All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
osdetect.h File Reference
#include "strngs.h"
#include "unicharset.h"

Go to the source code of this file.

Classes

struct  OSBestResult
 
struct  OSResults
 
class  OrientationDetector
 
class  ScriptDetector
 

Namespaces

 tesseract
 

Functions

int orientation_and_script_detection (STRING &filename, OSResults *, tesseract::Tesseract *)
 
int os_detect (TO_BLOCK_LIST *port_blocks, OSResults *osr, tesseract::Tesseract *tess)
 
int os_detect_blobs (const GenericVector< int > *allowed_scripts, BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
 
bool os_detect_blob (BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s, OSResults *, tesseract::Tesseract *tess)
 
TESS_API const int OrientationIdToValue (const int &id)
 

Variables

const int kMaxNumberOfScripts = 116 + 1 + 2 + 1
 

Function Documentation

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

Definition at line 189 of file osdetect.cpp.

191  {
192  STRING name = filename; //truncated name
193  const char *lastdot; //of name
194  TBOX page_box;
195 
196  lastdot = strrchr (name.string (), '.');
197  if (lastdot != NULL)
198  name[lastdot-name.string()] = '\0';
199 
200  ASSERT_HOST(tess->pix_binary() != NULL)
201  int width = pixGetWidth(tess->pix_binary());
202  int height = pixGetHeight(tess->pix_binary());
203 
204  BLOCK_LIST blocks;
205  if (!read_unlv_file(name, width, height, &blocks))
206  FullPageBlock(width, height, &blocks);
207 
208  // Try to remove non-text regions from consideration.
209  TO_BLOCK_LIST land_blocks, port_blocks;
210  remove_nontext_regions(tess, &blocks, &port_blocks);
211 
212  if (port_blocks.empty()) {
213  // page segmentation did not succeed, so we need to find_components first.
214  tess->mutable_textord()->find_components(tess->pix_binary(),
215  &blocks, &port_blocks);
216  } else {
217  page_box.set_left(0);
218  page_box.set_bottom(0);
219  page_box.set_right(width);
220  page_box.set_top(height);
221  // Filter_blobs sets up the TO_BLOCKs the same as find_components does.
222  tess->mutable_textord()->filter_blobs(page_box.topright(),
223  &port_blocks, true);
224  }
225 
226  return os_detect(&port_blocks, osr, tess);
227 }
void set_right(int x)
Definition: rect.h:78
void FullPageBlock(int width, int height, BLOCK_LIST *blocks)
Definition: blread.cpp:67
void set_left(int x)
Definition: rect.h:71
#define ASSERT_HOST(x)
Definition: errcode.h:84
void remove_nontext_regions(tesseract::Tesseract *tess, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
Definition: osdetect.cpp:162
void set_bottom(int y)
Definition: rect.h:64
name_table name
int os_detect(TO_BLOCK_LIST *port_blocks, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:232
Definition: rect.h:30
Definition: strngs.h:44
#define NULL
Definition: host.h:144
const ICOORD & topright() const
Definition: rect.h:100
void set_top(int y)
Definition: rect.h:57
const char * string() const
Definition: strngs.cpp:193
bool read_unlv_file(STRING name, inT32 xsize, inT32 ysize, BLOCK_LIST *blocks)
Definition: blread.cpp:36
TESS_API const int OrientationIdToValue ( const int &  id)

Definition at line 563 of file osdetect.cpp.

563  {
564  switch (id) {
565  case 0:
566  return 0;
567  case 1:
568  return 270;
569  case 2:
570  return 180;
571  case 3:
572  return 90;
573  default:
574  return -1;
575  }
576 }
int os_detect ( TO_BLOCK_LIST *  port_blocks,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 232 of file osdetect.cpp.

233  {
234  int blobs_total = 0;
235  TO_BLOCK_IT block_it;
236  block_it.set_to_list(port_blocks);
237 
238  BLOBNBOX_CLIST filtered_list;
239  BLOBNBOX_C_IT filtered_it(&filtered_list);
240 
241  for (block_it.mark_cycle_pt(); !block_it.cycled_list();
242  block_it.forward ()) {
243  TO_BLOCK* to_block = block_it.data();
244  if (to_block->block->poly_block() &&
245  !to_block->block->poly_block()->IsText()) continue;
246  BLOBNBOX_IT bbox_it;
247  bbox_it.set_to_list(&to_block->blobs);
248  for (bbox_it.mark_cycle_pt (); !bbox_it.cycled_list ();
249  bbox_it.forward ()) {
250  BLOBNBOX* bbox = bbox_it.data();
251  C_BLOB* blob = bbox->cblob();
252  TBOX box = blob->bounding_box();
253  ++blobs_total;
254 
255  float y_x = fabs((box.height() * 1.0) / box.width());
256  float x_y = 1.0f / y_x;
257  // Select a >= 1.0 ratio
258  float ratio = x_y > y_x ? x_y : y_x;
259  // Blob is ambiguous
260  if (ratio > kSizeRatioToReject) continue;
261  if (box.height() < kMinAcceptableBlobHeight) continue;
262  filtered_it.add_to_end(bbox);
263  }
264  }
265  return os_detect_blobs(NULL, &filtered_list, osr, tess);
266 }
const int kMinAcceptableBlobHeight
Definition: osdetect.cpp:40
bool IsText() const
Definition: polyblk.h:52
C_BLOB * cblob() const
Definition: blobbox.h:253
const float kSizeRatioToReject
Definition: osdetect.cpp:39
inT16 height() const
Definition: rect.h:104
inT16 width() const
Definition: rect.h:111
Definition: rect.h:30
int os_detect_blobs(const GenericVector< int > *allowed_scripts, BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:274
TBOX bounding_box() const
Definition: stepblob.cpp:250
#define NULL
Definition: host.h:144
POLY_BLOCK * poly_block() const
Definition: pdblock.h:59
BLOCK * block
Definition: blobbox.h:773
BLOBNBOX_LIST blobs
Definition: blobbox.h:768
bool os_detect_blob ( BLOBNBOX bbox,
OrientationDetector o,
ScriptDetector s,
OSResults ,
tesseract::Tesseract tess 
)

Definition at line 323 of file osdetect.cpp.

325  {
326  tess->tess_cn_matching.set_value(true); // turn it on
327  tess->tess_bn_matching.set_value(false);
328  C_BLOB* blob = bbox->cblob();
329  TBLOB* tblob = TBLOB::PolygonalCopy(tess->poly_allow_detailed_fx, blob);
330  TBOX box = tblob->bounding_box();
331  FCOORD current_rotation(1.0f, 0.0f);
332  FCOORD rotation90(0.0f, 1.0f);
333  BLOB_CHOICE_LIST ratings[4];
334  // Test the 4 orientations
335  for (int i = 0; i < 4; ++i) {
336  // Normalize the blob. Set the origin to the place we want to be the
337  // bottom-middle after rotation.
338  // Scaling is to make the rotated height the x-height.
339  float scaling = static_cast<float>(kBlnXHeight) / box.height();
340  float x_origin = (box.left() + box.right()) / 2.0f;
341  float y_origin = (box.bottom() + box.top()) / 2.0f;
342  if (i == 0 || i == 2) {
343  // Rotation is 0 or 180.
344  y_origin = i == 0 ? box.bottom() : box.top();
345  } else {
346  // Rotation is 90 or 270.
347  scaling = static_cast<float>(kBlnXHeight) / box.width();
348  x_origin = i == 1 ? box.left() : box.right();
349  }
350  TBLOB* rotated_blob = new TBLOB(*tblob);
351  rotated_blob->Normalize(NULL, &current_rotation, NULL,
352  x_origin, y_origin, scaling, scaling,
353  0.0f, static_cast<float>(kBlnBaselineOffset),
354  false, NULL);
355  tess->AdaptiveClassifier(rotated_blob, ratings + i);
356  delete rotated_blob;
357  current_rotation.rotate(rotation90);
358  }
359  delete tblob;
360 
361  bool stop = o->detect_blob(ratings);
362  s->detect_blob(ratings);
363  int orientation = o->get_orientation();
364  stop = s->must_stop(orientation) && stop;
365  return stop;
366 }
const int kBlnXHeight
Definition: normalis.h:28
Definition: blobs.h:261
void detect_blob(BLOB_CHOICE_LIST *scores)
Definition: osdetect.cpp:466
void Normalize(const BLOCK *block, const FCOORD *rotation, const DENORM *predecessor, float x_origin, float y_origin, float x_scale, float y_scale, float final_xshift, float final_yshift, bool inverse, Pix *pix)
Definition: blobs.cpp:413
inT16 right() const
Definition: rect.h:75
static TBLOB * PolygonalCopy(bool allow_detailed_fx, C_BLOB *src)
Definition: blobs.cpp:344
bool must_stop(int orientation)
Definition: osdetect.cpp:555
inT16 left() const
Definition: rect.h:68
void AdaptiveClassifier(TBLOB *Blob, BLOB_CHOICE_LIST *Choices)
Definition: adaptmatch.cpp:185
C_BLOB * cblob() const
Definition: blobbox.h:253
const int kBlnBaselineOffset
Definition: normalis.h:29
inT16 bottom() const
Definition: rect.h:61
inT16 height() const
Definition: rect.h:104
inT16 width() const
Definition: rect.h:111
Definition: rect.h:30
bool detect_blob(BLOB_CHOICE_LIST *scores)
Definition: osdetect.cpp:377
#define NULL
Definition: host.h:144
TBOX bounding_box() const
Definition: blobs.cpp:482
inT16 top() const
Definition: rect.h:54
Definition: points.h:189
int os_detect_blobs ( const GenericVector< int > *  allowed_scripts,
BLOBNBOX_CLIST *  blob_list,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 274 of file osdetect.cpp.

276  {
277  OSResults osr_;
278  if (osr == NULL)
279  osr = &osr_;
280 
281  osr->unicharset = &tess->unicharset;
282  OrientationDetector o(allowed_scripts, osr);
283  ScriptDetector s(allowed_scripts, osr, tess);
284 
285  BLOBNBOX_C_IT filtered_it(blob_list);
286  int real_max = MIN(filtered_it.length(), kMaxCharactersToTry);
287  // tprintf("Total blobs found = %d\n", blobs_total);
288  // tprintf("Number of blobs post-filtering = %d\n", filtered_it.length());
289  // tprintf("Number of blobs to try = %d\n", real_max);
290 
291  // If there are too few characters, skip this page entirely.
292  if (real_max < kMinCharactersToTry / 2) {
293  tprintf("Too few characters. Skipping this page\n");
294  return 0;
295  }
296 
297  BLOBNBOX** blobs = new BLOBNBOX*[filtered_it.length()];
298  int number_of_blobs = 0;
299  for (filtered_it.mark_cycle_pt (); !filtered_it.cycled_list ();
300  filtered_it.forward ()) {
301  blobs[number_of_blobs++] = (BLOBNBOX*)filtered_it.data();
302  }
303  QRSequenceGenerator sequence(number_of_blobs);
304  int num_blobs_evaluated = 0;
305  for (int i = 0; i < real_max; ++i) {
306  if (os_detect_blob(blobs[sequence.GetVal()], &o, &s, osr, tess)
307  && i > kMinCharactersToTry) {
308  break;
309  }
310  ++num_blobs_evaluated;
311  }
312  delete [] blobs;
313 
314  // Make sure the best_result is up-to-date
315  int orientation = o.get_orientation();
316  osr->update_best_script(orientation);
317  return num_blobs_evaluated;
318 }
const int kMaxCharactersToTry
Definition: osdetect.cpp:37
#define tprintf(...)
Definition: tprintf.h:31
#define MIN(x, y)
Definition: ndminx.h:28
UNICHARSET unicharset
Definition: ccutil.h:72
UNICHARSET * unicharset
Definition: osdetect.h:78
void update_best_script(int orientation_id)
Definition: osdetect.cpp:94
bool os_detect_blob(BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:323
#define NULL
Definition: host.h:144
const int kMinCharactersToTry
Definition: osdetect.cpp:36

Variable Documentation

const int kMaxNumberOfScripts = 116 + 1 + 2 + 1

Definition at line 36 of file osdetect.h.