tesseract  4.00.00dev
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 int OrientationIdToValue (const int &id)
 

Variables

const int kMaxNumberOfScripts = 116 + 1 + 2 + 1
 

Function Documentation

◆ orientation_and_script_detection()

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

Definition at line 191 of file osdetect.cpp.

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

◆ OrientationIdToValue()

TESS_API int OrientationIdToValue ( const int id)

Definition at line 565 of file osdetect.cpp.

565  {
566  switch (id) {
567  case 0:
568  return 0;
569  case 1:
570  return 270;
571  case 2:
572  return 180;
573  case 3:
574  return 90;
575  default:
576  return -1;
577  }
578 }

◆ os_detect()

int os_detect ( TO_BLOCK_LIST *  port_blocks,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 234 of file osdetect.cpp.

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

◆ os_detect_blob()

bool os_detect_blob ( BLOBNBOX bbox,
OrientationDetector o,
ScriptDetector s,
OSResults ,
tesseract::Tesseract tess 
)

Definition at line 325 of file osdetect.cpp.

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

◆ os_detect_blobs()

int os_detect_blobs ( const GenericVector< int > *  allowed_scripts,
BLOBNBOX_CLIST *  blob_list,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 276 of file osdetect.cpp.

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

Variable Documentation

◆ kMaxNumberOfScripts

const int kMaxNumberOfScripts = 116 + 1 + 2 + 1

Definition at line 36 of file osdetect.h.