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.

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  int resolution = pixGetXRes(tess->pix_binary());
204  // Zero resolution messes up the algorithms, so make sure it is credible.
205  if (resolution < kMinCredibleResolution)
206  resolution = kDefaultResolution;
207 
208  BLOCK_LIST blocks;
209  if (!read_unlv_file(name, width, height, &blocks))
210  FullPageBlock(width, height, &blocks);
211 
212  // Try to remove non-text regions from consideration.
213  TO_BLOCK_LIST land_blocks, port_blocks;
214  remove_nontext_regions(tess, &blocks, &port_blocks);
215 
216  if (port_blocks.empty()) {
217  // page segmentation did not succeed, so we need to find_components first.
218  tess->mutable_textord()->find_components(tess->pix_binary(),
219  &blocks, &port_blocks);
220  } else {
221  page_box.set_left(0);
222  page_box.set_bottom(0);
223  page_box.set_right(width);
224  page_box.set_top(height);
225  // Filter_blobs sets up the TO_BLOCKs the same as find_components does.
226  tess->mutable_textord()->filter_blobs(page_box.topright(),
227  &port_blocks, true);
228  }
229 
230  return os_detect(&port_blocks, osr, tess);
231 }
void set_right(int x)
Definition: rect.h:77
void filter_blobs(ICOORD page_tr, TO_BLOCK_LIST *blocks, BOOL8 testing_on)
Definition: tordmain.cpp:239
bool read_unlv_file(STRING name, inT32 xsize, inT32 ysize, BLOCK_LIST *blocks)
Definition: blread.cpp:37
#define NULL
Definition: host.h:144
Definition: rect.h:29
const int kMinCredibleResolution
Definition: osdetect.cpp:63
const ICOORD & topright() const
Definition: rect.h:93
int os_detect(TO_BLOCK_LIST *port_blocks, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:236
Textord * mutable_textord()
void find_components(Pix *pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
Definition: tordmain.cpp:208
const char * string() const
Definition: strngs.cpp:156
void FullPageBlock(int width, int height, BLOCK_LIST *blocks)
Definition: blread.cpp:69
Definition: strngs.h:40
void set_bottom(int y)
Definition: rect.h:63
#define ASSERT_HOST(x)
Definition: errcode.h:84
const int kDefaultResolution
Definition: osdetect.cpp:65
void set_top(int y)
Definition: rect.h:56
void set_left(int x)
Definition: rect.h:70
Pix * pix_binary() const
void remove_nontext_regions(tesseract::Tesseract *tess, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
Definition: osdetect.cpp:162
const int OrientationIdToValue ( const int &  id)

Definition at line 534 of file osdetect.cpp.

534  {
535  switch (id) {
536  case 0:
537  return 0;
538  case 1:
539  return 270;
540  case 2:
541  return 180;
542  case 3:
543  return 90;
544  default:
545  return -1;
546  }
547 }
int os_detect ( TO_BLOCK_LIST *  port_blocks,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 236 of file osdetect.cpp.

237  {
238  int blobs_total = 0;
239  TO_BLOCK_IT block_it;
240  block_it.set_to_list(port_blocks);
241 
242  BLOBNBOX_CLIST filtered_list;
243  BLOBNBOX_C_IT filtered_it(&filtered_list);
244 
245  for (block_it.mark_cycle_pt(); !block_it.cycled_list();
246  block_it.forward ()) {
247  TO_BLOCK* to_block = block_it.data();
248  if (to_block->block->poly_block() &&
249  !to_block->block->poly_block()->IsText()) continue;
250  BLOBNBOX_IT bbox_it;
251  bbox_it.set_to_list(&to_block->blobs);
252  for (bbox_it.mark_cycle_pt (); !bbox_it.cycled_list ();
253  bbox_it.forward ()) {
254  BLOBNBOX* bbox = bbox_it.data();
255  C_BLOB* blob = bbox->cblob();
256  TBOX box = blob->bounding_box();
257  ++blobs_total;
258 
259  float y_x = fabs((box.height() * 1.0) / box.width());
260  float x_y = 1.0f / y_x;
261  // Select a >= 1.0 ratio
262  float ratio = x_y > y_x ? x_y : y_x;
263  // Blob is ambiguous
264  if (ratio > kSizeRatioToReject) continue;
265  if (box.height() < kMinAcceptableBlobHeight) continue;
266  filtered_it.add_to_end(bbox);
267  }
268  }
269  return os_detect_blobs(&filtered_list, osr, tess);
270 }
BLOBNBOX_LIST blobs
Definition: blobbox.h:735
C_BLOB * cblob() const
Definition: blobbox.h:245
const float kSizeRatioToReject
Definition: osdetect.cpp:39
const int kMinAcceptableBlobHeight
Definition: osdetect.cpp:40
inT16 width() const
Definition: rect.h:104
Definition: rect.h:29
POLY_BLOCK * poly_block() const
Definition: pdblock.h:62
int os_detect_blobs(BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:275
TBOX bounding_box()
Definition: stepblob.cpp:192
bool IsText() const
Definition: polyblk.h:54
BLOCK * block
Definition: blobbox.h:740
inT16 height() const
Definition: rect.h:97
bool os_detect_blob ( BLOBNBOX bbox,
OrientationDetector o,
ScriptDetector s,
OSResults osr,
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(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  DENORM denorm;
351  denorm.SetupNormalization(NULL, NULL, &current_rotation, NULL, NULL, 0,
352  x_origin, y_origin, scaling, scaling,
353  0.0f, static_cast<float>(kBlnBaselineOffset));
354  TBLOB* rotated_blob = new TBLOB(*tblob);
355  rotated_blob->Normalize(denorm);
356  tess->AdaptiveClassifier(rotated_blob, denorm, ratings + i, NULL);
357  delete rotated_blob;
358  current_rotation.rotate(rotation90);
359  }
360  delete tblob;
361 
362  bool stop = o->detect_blob(ratings);
363  s->detect_blob(ratings);
364  int orientation = o->get_orientation();
365  stop = s->must_stop(orientation) && stop;
366  return stop;
367 }
const int kBlnXHeight
Definition: normalis.h:27
void AdaptiveClassifier(TBLOB *Blob, const DENORM &denorm, BLOB_CHOICE_LIST *Choices, CLASS_PRUNER_RESULTS cp_results)
Definition: adaptmatch.cpp:178
const int kBlnBaselineOffset
Definition: normalis.h:28
C_BLOB * cblob() const
Definition: blobbox.h:245
#define NULL
Definition: host.h:144
inT16 left() const
Definition: rect.h:67
void detect_blob(BLOB_CHOICE_LIST *scores)
Definition: osdetect.cpp:435
inT16 width() const
Definition: rect.h:104
Definition: rect.h:29
#define f(xc, yc)
Definition: imgscale.cpp:39
void Normalize(const DENORM &denorm)
Definition: blobs.cpp:330
inT16 right() const
Definition: rect.h:74
bool detect_blob(BLOB_CHOICE_LIST *scores)
Definition: osdetect.cpp:376
Definition: blobs.h:174
inT16 top() const
Definition: rect.h:53
bool must_stop(int orientation)
Definition: osdetect.cpp:526
TBOX bounding_box() const
Definition: blobs.cpp:384
Definition: points.h:189
void SetupNormalization(const BLOCK *block, const ROW *row, const FCOORD *rotation, const DENORM *predecessor, const DENORM_SEG *segs, int num_segs, float x_origin, float y_origin, float x_scale, float y_scale, float final_xshift, float final_yshift)
Definition: normalis.cpp:143
static TBLOB * PolygonalCopy(C_BLOB *src)
Definition: blobs.cpp:269
inT16 height() const
Definition: rect.h:97
inT16 bottom() const
Definition: rect.h:60
int os_detect_blobs ( BLOBNBOX_CLIST *  blob_list,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 275 of file osdetect.cpp.

276  {
277  OSResults osr_;
278  if (osr == NULL)
279  osr = &osr_;
280 
281  osr->unicharset = &tess->unicharset;
282  OrientationDetector o(osr);
283  ScriptDetector s(osr, tess);
284 
285  BLOBNBOX_C_IT filtered_it(blob_list);
286  int real_max = MIN(filtered_it.length(), kMaxCharactersToTry);
287  // printf("Total blobs found = %d\n", blobs_total);
288  // printf("Number of blobs post-filtering = %d\n", filtered_it.length());
289  // printf("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  printf("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 }
UNICHARSET * unicharset
Definition: osdetect.h:78
#define NULL
Definition: host.h:144
bool os_detect_blob(BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:323
const int kMaxCharactersToTry
Definition: osdetect.cpp:37
const int kMinCharactersToTry
Definition: osdetect.cpp:36
UNICHARSET unicharset
Definition: ccutil.h:72
#define MIN(x, y)
Definition: ndminx.h:28
void update_best_script(int orientation_id)
Definition: osdetect.cpp:94
void remove_nontext_regions ( tesseract::Tesseract tess,
BLOCK_LIST *  blocks,
TO_BLOCK_LIST *  to_blocks 
)

Definition at line 162 of file osdetect.cpp.

163  {
164  Pix *pix = tess->pix_binary();
165  ASSERT_HOST(pix != NULL);
166  int vertical_x = 0;
167  int vertical_y = 1;
168  tesseract::TabVector_LIST v_lines;
169  tesseract::TabVector_LIST h_lines;
170  const int kMinCredibleResolution = 70;
171  int resolution = (kMinCredibleResolution > pixGetXRes(pix)) ?
172  kMinCredibleResolution : pixGetXRes(pix);
173 
174  tesseract::LineFinder::FindAndRemoveLines(resolution, false, pix,
175  &vertical_x, &vertical_y,
176  NULL, &v_lines, &h_lines);
177  Pix* im_pix = tesseract::ImageFind::FindImages(pix);
178  if (im_pix != NULL) {
179  pixSubtract(pix, pix, im_pix);
180  pixDestroy(&im_pix);
181  }
182  tess->mutable_textord()->find_components(tess->pix_binary(),
183  blocks, to_blocks);
184 }
#define NULL
Definition: host.h:144
const int kMinCredibleResolution
Definition: osdetect.cpp:63
Textord * mutable_textord()
void find_components(Pix *pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
Definition: tordmain.cpp:208
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
static Pix * FindImages(Pix *pix)
Definition: imagefind.cpp:66
#define ASSERT_HOST(x)
Definition: errcode.h:84
Pix * pix_binary() const

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.