tesseract  4.00.00dev
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 (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 *osr, tesseract::Tesseract *tess)
 
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 kScriptAcceptRatio = 1.3
 
const float kHanRatioInKorean = 0.7
 
const float kHanRatioInJapanese = 0.3
 
const float kNonAmbiguousMargin = 1.0
 
const int kMinCredibleResolution = 70
 

Function Documentation

◆ orientation_and_script_detection()

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

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
Textord * mutable_textord()
Pix * pix_binary() const
void filter_blobs(ICOORD page_tr, TO_BLOCK_LIST *blocks, BOOL8 testing_on)
Definition: tordmain.cpp:236
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
void find_components(Pix *pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
Definition: tordmain.cpp:205
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()

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 osr,
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

◆ remove_nontext_regions()

void remove_nontext_regions ( tesseract::Tesseract tess,
BLOCK_LIST *  blocks,
TO_BLOCK_LIST *  to_blocks 
)

Definition at line 159 of file osdetect.cpp.

160  {
161  Pix *pix = tess->pix_binary();
162  ASSERT_HOST(pix != NULL);
163  int vertical_x = 0;
164  int vertical_y = 1;
165  tesseract::TabVector_LIST v_lines;
166  tesseract::TabVector_LIST h_lines;
167  int resolution;
168  if (kMinCredibleResolution > pixGetXRes(pix)) {
169  resolution = kMinCredibleResolution;
170  tprintf("Warning. Invalid resolution %d dpi. Using %d instead.\n",
171  pixGetXRes(pix), resolution);
172  } else {
173  resolution = pixGetXRes(pix);
174  }
175 
176  tesseract::LineFinder::FindAndRemoveLines(resolution, false, pix,
177  &vertical_x, &vertical_y,
178  NULL, &v_lines, &h_lines);
179  Pix* im_pix = tesseract::ImageFind::FindImages(pix, nullptr);
180  if (im_pix != NULL) {
181  pixSubtract(pix, pix, im_pix);
182  pixDestroy(&im_pix);
183  }
184  tess->mutable_textord()->find_components(tess->pix_binary(),
185  blocks, to_blocks);
186 }
Textord * mutable_textord()
Pix * pix_binary() const
#define tprintf(...)
Definition: tprintf.h:31
const int kMinCredibleResolution
Definition: osdetect.cpp:62
static Pix * FindImages(Pix *pix, DebugPixa *pixa_debug)
Definition: imagefind.cpp:66
#define ASSERT_HOST(x)
Definition: errcode.h:84
void find_components(Pix *pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
Definition: tordmain.cpp:205
static void FindAndRemoveLines(int resolution, bool debug, Pix *pix, int *vertical_x, int *vertical_y, Pix **pix_music_mask, TabVector_LIST *v_lines, TabVector_LIST *h_lines)
Definition: linefind.cpp:243

Variable Documentation

◆ kHanRatioInJapanese

const float kHanRatioInJapanese = 0.3

Definition at line 45 of file osdetect.cpp.

◆ kHanRatioInKorean

const float kHanRatioInKorean = 0.7

Definition at line 44 of file osdetect.cpp.

◆ kMaxCharactersToTry

const int kMaxCharactersToTry = 5 * kMinCharactersToTry

Definition at line 37 of file osdetect.cpp.

◆ kMinAcceptableBlobHeight

const int kMinAcceptableBlobHeight = 10

Definition at line 40 of file osdetect.cpp.

◆ kMinCharactersToTry

const int kMinCharactersToTry = 50

Definition at line 36 of file osdetect.cpp.

◆ kMinCredibleResolution

const int kMinCredibleResolution = 70

Definition at line 62 of file osdetect.cpp.

◆ kNonAmbiguousMargin

const float kNonAmbiguousMargin = 1.0

Definition at line 47 of file osdetect.cpp.

◆ kScriptAcceptRatio

const float kScriptAcceptRatio = 1.3

Definition at line 42 of file osdetect.cpp.

◆ kSizeRatioToReject

const float kSizeRatioToReject = 2.0

Definition at line 39 of file osdetect.cpp.