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 188 of file osdetect.cpp.

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

◆ OrientationIdToValue()

TESS_API int OrientationIdToValue ( const int &  id)

Definition at line 562 of file osdetect.cpp.

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

◆ os_detect()

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

Definition at line 231 of file osdetect.cpp.

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

◆ os_detect_blob()

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

Definition at line 322 of file osdetect.cpp.

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

◆ os_detect_blobs()

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

Definition at line 273 of file osdetect.cpp.

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

Variable Documentation

◆ kMaxNumberOfScripts

const int kMaxNumberOfScripts = 116 + 1 + 2 + 1

Definition at line 36 of file osdetect.h.