tesseract  4.0.0-1-g2a2b
tesseract::PageIterator Class Reference

#include <pageiterator.h>

Inheritance diagram for tesseract::PageIterator:
tesseract::LTRResultIterator tesseract::ResultIterator tesseract::MutableIterator

Public Member Functions

 PageIterator (PAGE_RES *page_res, Tesseract *tesseract, int scale, int scaled_yres, int rect_left, int rect_top, int rect_width, int rect_height)
 
virtual ~PageIterator ()
 
 PageIterator (const PageIterator &src)
 
const PageIteratoroperator= (const PageIterator &src)
 
bool PositionedAtSameWord (const PAGE_RES_IT *other) const
 
virtual void Begin ()
 
virtual void RestartParagraph ()
 
bool IsWithinFirstTextlineOfParagraph () const
 
virtual void RestartRow ()
 
virtual bool Next (PageIteratorLevel level)
 
virtual bool IsAtBeginningOf (PageIteratorLevel level) const
 
virtual bool IsAtFinalElement (PageIteratorLevel level, PageIteratorLevel element) const
 
int Cmp (const PageIterator &other) const
 
void SetBoundingBoxComponents (bool include_upper_dots, bool include_lower_dots)
 
bool BoundingBox (PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
 
bool BoundingBox (PageIteratorLevel level, const int padding, int *left, int *top, int *right, int *bottom) const
 
bool BoundingBoxInternal (PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
 
bool Empty (PageIteratorLevel level) const
 
PolyBlockType BlockType () const
 
Pta * BlockPolygon () const
 
Pix * GetBinaryImage (PageIteratorLevel level) const
 
Pix * GetImage (PageIteratorLevel level, int padding, Pix *original_img, int *left, int *top) const
 
bool Baseline (PageIteratorLevel level, int *x1, int *y1, int *x2, int *y2) const
 
void Orientation (tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
 
void ParagraphInfo (tesseract::ParagraphJustification *justification, bool *is_list_item, bool *is_crown, int *first_line_indent) const
 
bool SetWordBlamerBundle (BlamerBundle *blamer_bundle)
 

Protected Member Functions

TESS_LOCAL void BeginWord (int offset)
 

Protected Attributes

PAGE_RESpage_res_
 
Tesseracttesseract_
 
PAGE_RES_ITit_
 
WERDword_
 
int word_length_
 
int blob_index_
 
C_BLOB_IT * cblob_it_
 
bool include_upper_dots_
 
bool include_lower_dots_
 
int scale_
 
int scaled_yres_
 
int rect_left_
 
int rect_top_
 
int rect_width_
 
int rect_height_
 

Detailed Description

Class to iterate over tesseract page structure, providing access to all levels of the page hierarchy, without including any tesseract headers or having to handle any tesseract structures. WARNING! This class points to data held within the TessBaseAPI class, and therefore can only be used while the TessBaseAPI class still exists and has not been subjected to a call of Init, SetImage, Recognize, Clear, End DetectOS, or anything else that changes the internal PAGE_RES. See apitypes.h for the definition of PageIteratorLevel. See also ResultIterator, derived from PageIterator, which adds in the ability to access OCR output with text-specific methods.

Definition at line 52 of file pageiterator.h.

Constructor & Destructor Documentation

◆ PageIterator() [1/2]

tesseract::PageIterator::PageIterator ( PAGE_RES page_res,
Tesseract tesseract,
int  scale,
int  scaled_yres,
int  rect_left,
int  rect_top,
int  rect_width,
int  rect_height 
)

page_res and tesseract come directly from the BaseAPI. The rectangle parameters are copied indirectly from the Thresholder, via the BaseAPI. They represent the coordinates of some rectangle in an original image (in top-left-origin coordinates) and therefore the top-left needs to be added to any output boxes in order to specify coordinates in the original image. See TessBaseAPI::SetRectangle. The scale and scaled_yres are in case the Thresholder scaled the image rectangle prior to thresholding. Any coordinates in tesseract's image must be divided by scale before adding (rect_left, rect_top). The scaled_yres indicates the effective resolution of the binary image that tesseract has been given by the Thresholder. After the constructor, Begin has already been called.

Definition at line 31 of file pageiterator.cpp.

34  : page_res_(page_res),
36  word_(nullptr),
37  word_length_(0),
38  blob_index_(0),
39  cblob_it_(nullptr),
40  include_upper_dots_(false),
41  include_lower_dots_(false),
42  scale_(scale),
43  scaled_yres_(scaled_yres),
44  rect_left_(rect_left),
45  rect_top_(rect_top),
46  rect_width_(rect_width),
47  rect_height_(rect_height) {
48  it_ = new PAGE_RES_IT(page_res);
50 }

◆ ~PageIterator()

tesseract::PageIterator::~PageIterator ( )
virtual

Definition at line 52 of file pageiterator.cpp.

52  {
53  delete it_;
54  delete cblob_it_;
55 }

◆ PageIterator() [2/2]

tesseract::PageIterator::PageIterator ( const PageIterator src)

Page/ResultIterators may be copied! This makes it possible to iterate over all the objects at a lower level, while maintaining an iterator to objects at a higher level. These constructors DO NOT CALL Begin, so iterations will continue from the location of src.

PageIterators may be copied! This makes it possible to iterate over all the objects at a lower level, while maintaining an iterator to objects at a higher level.

Definition at line 62 of file pageiterator.cpp.

63  : page_res_(src.page_res_),
64  tesseract_(src.tesseract_),
65  word_(nullptr),
66  word_length_(src.word_length_),
67  blob_index_(src.blob_index_),
68  cblob_it_(nullptr),
69  include_upper_dots_(src.include_upper_dots_),
70  include_lower_dots_(src.include_lower_dots_),
71  scale_(src.scale_),
72  scaled_yres_(src.scaled_yres_),
73  rect_left_(src.rect_left_),
74  rect_top_(src.rect_top_),
75  rect_width_(src.rect_width_),
76  rect_height_(src.rect_height_) {
77  it_ = new PAGE_RES_IT(*src.it_);
78  BeginWord(src.blob_index_);
79 }
TESS_LOCAL void BeginWord(int offset)

Member Function Documentation

◆ Baseline()

bool tesseract::PageIterator::Baseline ( PageIteratorLevel  level,
int *  x1,
int *  y1,
int *  x2,
int *  y2 
) const

Returns the baseline of the current object at the given level. The baseline is the line that passes through (x1, y1) and (x2, y2). WARNING: with vertical text, baselines may be vertical! Returns false if there is no baseline at the current position.

Returns the baseline of the current object at the given level. The baseline is the line that passes through (x1, y1) and (x2, y2). WARNING: with vertical text, baselines may be vertical!

Definition at line 489 of file pageiterator.cpp.

490  {
491  if (it_->word() == nullptr) return false; // Already at the end!
492  ROW* row = it_->row()->row;
493  WERD* word = it_->word()->word;
494  TBOX box = (level == RIL_WORD || level == RIL_SYMBOL)
495  ? word->bounding_box()
496  : row->bounding_box();
497  int left = box.left();
498  ICOORD startpt(left, static_cast<int16_t>(row->base_line(left) + 0.5));
499  int right = box.right();
500  ICOORD endpt(right, static_cast<int16_t>(row->base_line(right) + 0.5));
501  // Rotate to image coordinates and convert to global image coords.
502  startpt.rotate(it_->block()->block->re_rotation());
503  endpt.rotate(it_->block()->block->re_rotation());
504  *x1 = startpt.x() / scale_ + rect_left_;
505  *y1 = (rect_height_ - startpt.y()) / scale_ + rect_top_;
506  *x2 = endpt.x() / scale_ + rect_left_;
507  *y2 = (rect_height_ - endpt.y()) / scale_ + rect_top_;
508  return true;
509 }
BLOCK_RES * block() const
Definition: pageres.h:757
ROW_RES * row() const
Definition: pageres.h:754
FCOORD re_rotation() const
Definition: ocrblock.h:136
TBOX bounding_box() const
Definition: werd.cpp:159
float base_line(float xpos) const
Definition: ocrrow.h:59
Definition: rect.h:34
BLOCK * block
Definition: pageres.h:117
int16_t left() const
Definition: rect.h:72
integer coordinate
Definition: points.h:32
WERD_RES * word() const
Definition: pageres.h:751
Definition: werd.h:59
TBOX bounding_box() const
Definition: ocrrow.h:88
Definition: ocrrow.h:36
void rotate(const FCOORD &vec)
Definition: points.h:537
int16_t right() const
Definition: rect.h:79
ROW * row
Definition: pageres.h:143
WERD * word
Definition: pageres.h:189

◆ Begin()

void tesseract::PageIterator::Begin ( )
virtual

Moves the iterator to point to the start of the page to begin an iteration.

Resets the iterator to point to the start of the page.

Reimplemented in tesseract::ResultIterator.

Definition at line 106 of file pageiterator.cpp.

106  {
108  BeginWord(0);
109 }
TESS_LOCAL void BeginWord(int offset)
WERD_RES * restart_page_with_empties()
Definition: pageres.h:701

◆ BeginWord()

void tesseract::PageIterator::BeginWord ( int  offset)
protected

Sets up the internal data for iterating the blobs of a new word, then moves the iterator to the given offset.

Definition at line 578 of file pageiterator.cpp.

578  {
579  WERD_RES* word_res = it_->word();
580  if (word_res == nullptr) {
581  // This is a non-text block, so there is no word.
582  word_length_ = 0;
583  blob_index_ = 0;
584  word_ = nullptr;
585  return;
586  }
587  if (word_res->best_choice != nullptr) {
588  // Recognition has been done, so we are using the box_word, which
589  // is already baseline denormalized.
590  word_length_ = word_res->best_choice->length();
591  if (word_res->box_word != nullptr) {
592  if (word_res->box_word->length() != word_length_) {
593  tprintf("Corrupted word! best_choice[len=%d] = %s, box_word[len=%d]: ",
595  word_res->box_word->length());
596  word_res->box_word->bounding_box().print();
597  }
598  ASSERT_HOST(word_res->box_word->length() == word_length_);
599  }
600  word_ = nullptr;
601  // We will be iterating the box_word.
602  delete cblob_it_;
603  cblob_it_ = nullptr;
604  } else {
605  // No recognition yet, so a "symbol" is a cblob.
606  word_ = word_res->word;
607  ASSERT_HOST(word_->cblob_list() != nullptr);
608  word_length_ = word_->cblob_list()->length();
609  if (cblob_it_ == nullptr) cblob_it_ = new C_BLOB_IT;
610  cblob_it_->set_to_list(word_->cblob_list());
611  }
612  for (blob_index_ = 0; blob_index_ < offset; ++blob_index_) {
613  if (cblob_it_ != nullptr)
614  cblob_it_->forward();
615  }
616 }
void print() const
Definition: rect.h:278
const char * string() const
Definition: strngs.cpp:196
const TBOX & bounding_box() const
Definition: boxword.h:80
WERD_RES * word() const
Definition: pageres.h:751
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
int length() const
Definition: ratngs.h:303
C_BLOB_LIST * cblob_list()
Definition: werd.h:98
int length() const
Definition: boxword.h:83
const STRING & unichar_string() const
Definition: ratngs.h:541
WERD_CHOICE * best_choice
Definition: pageres.h:235
tesseract::BoxWord * box_word
Definition: pageres.h:266
#define ASSERT_HOST(x)
Definition: errcode.h:84
WERD * word
Definition: pageres.h:189

◆ BlockPolygon()

Pta * tesseract::PageIterator::BlockPolygon ( ) const

Returns the polygon outline of the current block. The returned Pta must be ptaDestroy-ed after use. Note that the returned Pta lists the vertices of the polygon, and the last edge is the line segment between the last point and the first point. nullptr will be returned if the iterator is at the end of the document or layout analysis was not used.

Returns the polygon outline of the current block. The returned Pta must be ptaDestroy-ed after use.

Definition at line 369 of file pageiterator.cpp.

369  {
370  if (it_->block() == nullptr || it_->block()->block == nullptr)
371  return nullptr; // Already at the end!
372  if (it_->block()->block->pdblk.poly_block() == nullptr)
373  return nullptr; // No layout analysis used - no polygon.
374  ICOORDELT_IT it(it_->block()->block->pdblk.poly_block()->points());
375  Pta* pta = ptaCreate(it.length());
376  int num_pts = 0;
377  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward(), ++num_pts) {
378  ICOORD* pt = it.data();
379  // Convert to top-down coords within the input image.
380  float x = static_cast<float>(pt->x()) / scale_ + rect_left_;
381  float y = rect_top_ + rect_height_ - static_cast<float>(pt->y()) / scale_;
382  ptaAddPt(pta, x, y);
383  }
384  return pta;
385 }
BLOCK_RES * block() const
Definition: pageres.h:757
int16_t y() const
access_function
Definition: points.h:57
BLOCK * block
Definition: pageres.h:117
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53
POLY_BLOCK * poly_block() const
Definition: pdblock.h:56
ICOORDELT_LIST * points()
Definition: polyblk.h:39
PDBLK pdblk
Definition: ocrblock.h:192

◆ BlockType()

PolyBlockType tesseract::PageIterator::BlockType ( ) const

Returns the type of the current block. See apitypes.h for PolyBlockType.

Definition at line 359 of file pageiterator.cpp.

359  {
360  if (it_->block() == nullptr || it_->block()->block == nullptr)
361  return PT_UNKNOWN; // Already at the end!
362  if (it_->block()->block->pdblk.poly_block() == nullptr)
363  return PT_FLOWING_TEXT; // No layout analysis used - assume text.
364  return it_->block()->block->pdblk.poly_block()->isA();
365 }
BLOCK_RES * block() const
Definition: pageres.h:757
BLOCK * block
Definition: pageres.h:117
PolyBlockType isA() const
Definition: polyblk.h:45
POLY_BLOCK * poly_block() const
Definition: pdblock.h:56
PDBLK pdblk
Definition: ocrblock.h:192

◆ BoundingBox() [1/2]

bool tesseract::PageIterator::BoundingBox ( PageIteratorLevel  level,
int *  left,
int *  top,
int *  right,
int *  bottom 
) const

Returns the bounding rectangle of the current object at the given level. See comment on coordinate system above. Returns false if there is no such object at the current position. The returned bounding box is guaranteed to match the size and position of the image returned by GetBinaryImage, but may clip foreground pixels from a grey image. The padding argument to GetImage can be used to expand the image to include more foreground pixels. See GetImage below.

Returns the bounding rectangle of the current object at the given level in coordinates of the original image. See comment on coordinate system above. Returns false if there is no such object at the current position.

Definition at line 326 of file pageiterator.cpp.

328  {
329  return BoundingBox(level, 0, left, top, right, bottom);
330 }
bool BoundingBox(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const

◆ BoundingBox() [2/2]

bool tesseract::PageIterator::BoundingBox ( PageIteratorLevel  level,
const int  padding,
int *  left,
int *  top,
int *  right,
int *  bottom 
) const

Definition at line 332 of file pageiterator.cpp.

334  {
335  if (!BoundingBoxInternal(level, left, top, right, bottom))
336  return false;
337  // Convert to the coordinate system of the original image.
338  *left = ClipToRange(*left / scale_ + rect_left_ - padding,
340  *top = ClipToRange(*top / scale_ + rect_top_ - padding,
342  *right = ClipToRange((*right + scale_ - 1) / scale_ + rect_left_ + padding,
343  *left, rect_left_ + rect_width_);
344  *bottom = ClipToRange((*bottom + scale_ - 1) / scale_ + rect_top_ + padding,
345  *top, rect_top_ + rect_height_);
346  return true;
347 }
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111
bool BoundingBoxInternal(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const

◆ BoundingBoxInternal()

bool tesseract::PageIterator::BoundingBoxInternal ( PageIteratorLevel  level,
int *  left,
int *  top,
int *  right,
int *  bottom 
) const

Returns the bounding rectangle of the object in a coordinate system of the working image rectangle having its origin at (rect_left_, rect_top_) with respect to the original image and is scaled by a factor scale_.

Returns the bounding rectangle of the current object at the given level in the coordinates of the working image that is pix_binary(). See comment on coordinate system above. Returns false if there is no such object at the current position.

Definition at line 266 of file pageiterator.cpp.

268  {
269  if (Empty(level))
270  return false;
271  TBOX box;
272  PARA *para = nullptr;
273  switch (level) {
274  case RIL_BLOCK:
277  break;
278  case RIL_PARA:
279  para = it_->row()->row->para();
280  // explicit fall-through.
281  case RIL_TEXTLINE:
284  break;
285  case RIL_WORD:
288  break;
289  case RIL_SYMBOL:
290  if (cblob_it_ == nullptr)
291  box = it_->word()->box_word->BlobBox(blob_index_);
292  else
293  box = cblob_it_->data()->bounding_box();
294  }
295  if (level == RIL_PARA) {
296  PageIterator other = *this;
297  other.Begin();
298  do {
299  if (other.it_->block() &&
300  other.it_->block()->block == it_->block()->block &&
301  other.it_->row() && other.it_->row()->row &&
302  other.it_->row()->row->para() == para) {
303  box = box.bounding_union(other.it_->row()->row->bounding_box());
304  }
305  } while (other.Next(RIL_TEXTLINE));
306  }
307  if (level != RIL_SYMBOL || cblob_it_ != nullptr)
308  box.rotate(it_->block()->block->re_rotation());
309  // Now we have a box in tesseract coordinates relative to the image rectangle,
310  // we have to convert the coords to a top-down system.
311  const int pix_height = pixGetHeight(tesseract_->pix_binary());
312  const int pix_width = pixGetWidth(tesseract_->pix_binary());
313  *left = ClipToRange(static_cast<int>(box.left()), 0, pix_width);
314  *top = ClipToRange(pix_height - box.top(), 0, pix_height);
315  *right = ClipToRange(static_cast<int>(box.right()), *left, pix_width);
316  *bottom = ClipToRange(pix_height - box.bottom(), *top, pix_height);
317  return true;
318 }
BLOCK_RES * block() const
Definition: pageres.h:757
ROW_RES * row() const
Definition: pageres.h:754
void rotate(const FCOORD &vec)
Definition: rect.h:197
FCOORD re_rotation() const
Definition: ocrblock.h:136
Definition: rect.h:34
PageIterator(PAGE_RES *page_res, Tesseract *tesseract, int scale, int scaled_yres, int rect_left, int rect_top, int rect_width, int rect_height)
const TBOX & BlobBox(int index) const
Definition: boxword.h:84
BLOCK * block
Definition: pageres.h:117
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
Definition: ocrpara.h:29
TBOX restricted_bounding_box(bool upper_dots, bool lower_dots) const
Definition: ocrblock.cpp:89
TBOX restricted_bounding_box(bool upper_dots, bool lower_dots) const
Definition: ocrrow.cpp:85
WERD_RES * word() const
Definition: pageres.h:751
Pix * pix_binary() const
TBOX restricted_bounding_box(bool upper_dots, bool lower_dots) const
Definition: werd.cpp:163
int16_t right() const
Definition: rect.h:79
bool Empty(PageIteratorLevel level) const
TBOX bounding_union(const TBOX &box) const
Definition: rect.cpp:129
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111
int16_t bottom() const
Definition: rect.h:65
tesseract::BoxWord * box_word
Definition: pageres.h:266
ROW * row
Definition: pageres.h:143
PARA * para() const
Definition: ocrrow.h:118
WERD * word
Definition: pageres.h:189

◆ Cmp()

int tesseract::PageIterator::Cmp ( const PageIterator other) const

Returns whether this iterator is positioned before other: -1 equal to other: 0 after other: 1

Definition at line 236 of file pageiterator.cpp.

236  {
237  int word_cmp = it_->cmp(*other.it_);
238  if (word_cmp != 0)
239  return word_cmp;
240  if (blob_index_ < other.blob_index_)
241  return -1;
242  if (blob_index_ == other.blob_index_)
243  return 0;
244  return 1;
245 }
int cmp(const PAGE_RES_IT &other) const
Definition: pageres.cpp:1201

◆ Empty()

bool tesseract::PageIterator::Empty ( PageIteratorLevel  level) const

Returns whether there is no object of a given level.

Return that there is no such object at a given level.

Definition at line 350 of file pageiterator.cpp.

350  {
351  if (it_->block() == nullptr) return true; // Already at the end!
352  if (it_->word() == nullptr && level != RIL_BLOCK) return true; // image block
353  if (level == RIL_SYMBOL && blob_index_ >= word_length_)
354  return true; // Zero length word, or already at the end of it.
355  return false;
356 }
BLOCK_RES * block() const
Definition: pageres.h:757
WERD_RES * word() const
Definition: pageres.h:751

◆ GetBinaryImage()

Pix * tesseract::PageIterator::GetBinaryImage ( PageIteratorLevel  level) const

Returns a binary image of the current object at the given level. The position and size match the return from BoundingBoxInternal, and so this could be upscaled with respect to the original input image. Use pixDestroy to delete the image after use.

Returns a binary image of the current object at the given level. The position and size match the return from BoundingBoxInternal, and so this could be upscaled with respect to the original input image. Use pixDestroy to delete the image after use. The following methods are used to generate the images: RIL_BLOCK: mask the page image with the block polygon. RIL_TEXTLINE: Clip the rectangle of the line box from the page image. TODO(rays) fix this to generate and use a line polygon. RIL_WORD: Clip the rectangle of the word box from the page image. RIL_SYMBOL: Render the symbol outline to an image for cblobs (prior to recognition) or the bounding box otherwise. A reconstruction of the original image (using xor to check for double representation) should be reasonably accurate, apart from removed noise, at the block level. Below the block level, the reconstruction will be missing images and line separators. At the symbol level, kerned characters will be invade the bounding box if rendered after recognition, making an xor reconstruction inaccurate, but an or construction better. Before recognition, symbol-level reconstruction should be good, even with xor, since the images come from the connected components.

Definition at line 409 of file pageiterator.cpp.

409  {
410  int left, top, right, bottom;
411  if (!BoundingBoxInternal(level, &left, &top, &right, &bottom))
412  return nullptr;
413  if (level == RIL_SYMBOL && cblob_it_ != nullptr &&
414  cblob_it_->data()->area() != 0)
415  return cblob_it_->data()->render();
416  Box* box = boxCreate(left, top, right - left, bottom - top);
417  Pix* pix = pixClipRectangle(tesseract_->pix_binary(), box, nullptr);
418  boxDestroy(&box);
419  if (level == RIL_BLOCK || level == RIL_PARA) {
420  // Clip to the block polygon as well.
421  TBOX mask_box;
422  Pix* mask = it_->block()->block->render_mask(&mask_box);
423  int mask_x = left - mask_box.left();
424  int mask_y = top - (tesseract_->ImageHeight() - mask_box.top());
425  // AND the mask and pix, putting the result in pix.
426  pixRasterop(pix, std::max(0, -mask_x), std::max(0, -mask_y), pixGetWidth(pix),
427  pixGetHeight(pix), PIX_SRC & PIX_DST, mask, std::max(0, mask_x),
428  std::max(0, mask_y));
429  pixDestroy(&mask);
430  }
431  return pix;
432 }
BLOCK_RES * block() const
Definition: pageres.h:757
Definition: rect.h:34
BLOCK * block
Definition: pageres.h:117
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
Pix * render_mask(TBOX *mask_box)
Definition: ocrblock.h:162
Pix * pix_binary() const
bool BoundingBoxInternal(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const

◆ GetImage()

Pix * tesseract::PageIterator::GetImage ( PageIteratorLevel  level,
int  padding,
Pix *  original_img,
int *  left,
int *  top 
) const

Returns an image of the current object at the given level in greyscale if available in the input. To guarantee a binary image use BinaryImage. NOTE that in order to give the best possible image, the bounds are expanded slightly over the binary connected component, by the supplied padding, so the top-left position of the returned image is returned in (left,top). These will most likely not match the coordinates returned by BoundingBox. If you do not supply an original image, you will get a binary one. Use pixDestroy to delete the image after use.

Definition at line 445 of file pageiterator.cpp.

447  {
448  int right, bottom;
449  if (!BoundingBox(level, left, top, &right, &bottom))
450  return nullptr;
451  if (original_img == nullptr)
452  return GetBinaryImage(level);
453 
454  // Expand the box.
455  *left = std::max(*left - padding, 0);
456  *top = std::max(*top - padding, 0);
457  right = std::min(right + padding, rect_width_);
458  bottom = std::min(bottom + padding, rect_height_);
459  Box* box = boxCreate(*left, *top, right - *left, bottom - *top);
460  Pix* grey_pix = pixClipRectangle(original_img, box, nullptr);
461  boxDestroy(&box);
462  if (level == RIL_BLOCK || level == RIL_PARA) {
463  // Clip to the block polygon as well.
464  TBOX mask_box;
465  Pix* mask = it_->block()->block->render_mask(&mask_box);
466  // Copy the mask registered correctly into an image the size of grey_pix.
467  int mask_x = *left - mask_box.left();
468  int mask_y = *top - (pixGetHeight(original_img) - mask_box.top());
469  int width = pixGetWidth(grey_pix);
470  int height = pixGetHeight(grey_pix);
471  Pix* resized_mask = pixCreate(width, height, 1);
472  pixRasterop(resized_mask, std::max(0, -mask_x), std::max(0, -mask_y), width, height,
473  PIX_SRC, mask, std::max(0, mask_x), std::max(0, mask_y));
474  pixDestroy(&mask);
475  pixDilateBrick(resized_mask, resized_mask, 2 * padding + 1,
476  2 * padding + 1);
477  pixInvert(resized_mask, resized_mask);
478  pixSetMasked(grey_pix, resized_mask, UINT32_MAX);
479  pixDestroy(&resized_mask);
480  }
481  return grey_pix;
482 }
BLOCK_RES * block() const
Definition: pageres.h:757
bool BoundingBox(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
Definition: rect.h:34
BLOCK * block
Definition: pageres.h:117
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
Pix * render_mask(TBOX *mask_box)
Definition: ocrblock.h:162
Pix * GetBinaryImage(PageIteratorLevel level) const

◆ IsAtBeginningOf()

bool tesseract::PageIterator::IsAtBeginningOf ( PageIteratorLevel  level) const
virtual

Returns true if the iterator is at the start of an object at the given level.

For instance, suppose an iterator it is pointed to the first symbol of the first word of the third line of the second paragraph of the first block in a page, then: it.IsAtBeginningOf(RIL_BLOCK) = false it.IsAtBeginningOf(RIL_PARA) = false it.IsAtBeginningOf(RIL_TEXTLINE) = true it.IsAtBeginningOf(RIL_WORD) = true it.IsAtBeginningOf(RIL_SYMBOL) = true

Returns true if the iterator is at the start of an object at the given level. Possible uses include determining if a call to Next(RIL_WORD) moved to the start of a RIL_PARA.

Reimplemented in tesseract::ResultIterator.

Definition at line 186 of file pageiterator.cpp.

186  {
187  if (it_->block() == nullptr) return false; // Already at the end!
188  if (it_->word() == nullptr) return true; // In an image block.
189  switch (level) {
190  case RIL_BLOCK:
191  return blob_index_ == 0 && it_->block() != it_->prev_block();
192  case RIL_PARA:
193  return blob_index_ == 0 &&
194  (it_->block() != it_->prev_block() ||
195  it_->row()->row->para() != it_->prev_row()->row->para());
196  case RIL_TEXTLINE:
197  return blob_index_ == 0 && it_->row() != it_->prev_row();
198  case RIL_WORD:
199  return blob_index_ == 0;
200  case RIL_SYMBOL:
201  return true;
202  }
203  return false;
204 }
BLOCK_RES * block() const
Definition: pageres.h:757
ROW_RES * row() const
Definition: pageres.h:754
ROW_RES * prev_row() const
Definition: pageres.h:745
WERD_RES * word() const
Definition: pageres.h:751
BLOCK_RES * prev_block() const
Definition: pageres.h:748
ROW * row
Definition: pageres.h:143
PARA * para() const
Definition: ocrrow.h:118

◆ IsAtFinalElement()

bool tesseract::PageIterator::IsAtFinalElement ( PageIteratorLevel  level,
PageIteratorLevel  element 
) const
virtual

Returns whether the iterator is positioned at the last element in a given level. (e.g. the last word in a line, the last line in a block)

Here's some two-paragraph example

text. It starts off innocuously enough but quickly turns bizarre. The author inserts a cornucopia of words to guard against confused references.

Now take an iterator it pointed to the start of "bizarre." it.IsAtFinalElement(RIL_PARA, RIL_SYMBOL) = false it.IsAtFinalElement(RIL_PARA, RIL_WORD) = true it.IsAtFinalElement(RIL_BLOCK, RIL_WORD) = false

Returns whether the iterator is positioned at the last element in a given level. (e.g. the last word in a line, the last line in a block)

Reimplemented in tesseract::ResultIterator.

Definition at line 210 of file pageiterator.cpp.

211  {
212  if (Empty(element)) return true; // Already at the end!
213  // The result is true if we step forward by element and find we are
214  // at the the end of the page or at beginning of *all* levels in:
215  // [level, element).
216  // When there is more than one level difference between element and level,
217  // we could for instance move forward one symbol and still be at the first
218  // word on a line, so we also have to be at the first symbol in a word.
219  PageIterator next(*this);
220  next.Next(element);
221  if (next.Empty(element)) return true; // Reached the end of the page.
222  while (element > level) {
223  element = static_cast<PageIteratorLevel>(element - 1);
224  if (!next.IsAtBeginningOf(element))
225  return false;
226  }
227  return true;
228 }
PageIterator(PAGE_RES *page_res, Tesseract *tesseract, int scale, int scaled_yres, int rect_left, int rect_top, int rect_width, int rect_height)
bool Empty(PageIteratorLevel level) const

◆ IsWithinFirstTextlineOfParagraph()

bool tesseract::PageIterator::IsWithinFirstTextlineOfParagraph ( ) const

Return whether this iterator points anywhere in the first textline of a paragraph.

Definition at line 124 of file pageiterator.cpp.

124  {
125  PageIterator p_start(*this);
126  p_start.RestartParagraph();
127  return p_start.it_->row() == it_->row();
128 }
ROW_RES * row() const
Definition: pageres.h:754
PageIterator(PAGE_RES *page_res, Tesseract *tesseract, int scale, int scaled_yres, int rect_left, int rect_top, int rect_width, int rect_height)

◆ Next()

bool tesseract::PageIterator::Next ( PageIteratorLevel  level)
virtual

Moves to the start of the next object at the given level in the page hierarchy, and returns false if the end of the page was reached. NOTE that RIL_SYMBOL will skip non-text blocks, but all other PageIteratorLevel level values will visit each non-text block once. Think of non text blocks as containing a single para, with a single line, with a single imaginary word. Calls to Next with different levels may be freely intermixed. This function iterates words in right-to-left scripts correctly, if the appropriate language has been loaded into Tesseract.

Moves to the start of the next object at the given level in the page hierarchy, and returns false if the end of the page was reached. NOTE (CHANGED!) that ALL PageIteratorLevel level values will visit each non-text block at least once. Think of non text blocks as containing a single para, with at least one line, with a single imaginary word, containing a single symbol. The bounding boxes mark out any polygonal nature of the block, and PTIsTextType(BLockType()) is false for non-text blocks. Calls to Next with different levels may be freely intermixed. This function iterates words in right-to-left scripts correctly, if the appropriate language has been loaded into Tesseract.

Reimplemented in tesseract::ResultIterator.

Definition at line 148 of file pageiterator.cpp.

148  {
149  if (it_->block() == nullptr) return false; // Already at the end!
150  if (it_->word() == nullptr)
151  level = RIL_BLOCK;
152 
153  switch (level) {
154  case RIL_BLOCK:
155  it_->forward_block();
156  break;
157  case RIL_PARA:
159  break;
160  case RIL_TEXTLINE:
161  for (it_->forward_with_empties(); it_->row() == it_->prev_row();
163  break;
164  case RIL_WORD:
166  break;
167  case RIL_SYMBOL:
168  if (cblob_it_ != nullptr)
169  cblob_it_->forward();
170  ++blob_index_;
171  if (blob_index_ >= word_length_)
173  else
174  return true;
175  break;
176  }
177  BeginWord(0);
178  return it_->block() != nullptr;
179 }
BLOCK_RES * block() const
Definition: pageres.h:757
ROW_RES * row() const
Definition: pageres.h:754
WERD_RES * forward_paragraph()
Definition: pageres.cpp:1652
TESS_LOCAL void BeginWord(int offset)
WERD_RES * forward_block()
Definition: pageres.cpp:1667
ROW_RES * prev_row() const
Definition: pageres.h:745
WERD_RES * forward_with_empties()
Definition: pageres.h:735
WERD_RES * word() const
Definition: pageres.h:751

◆ operator=()

const PageIterator & tesseract::PageIterator::operator= ( const PageIterator src)

Definition at line 81 of file pageiterator.cpp.

81  {
82  page_res_ = src.page_res_;
83  tesseract_ = src.tesseract_;
84  include_upper_dots_ = src.include_upper_dots_;
85  include_lower_dots_ = src.include_lower_dots_;
86  scale_ = src.scale_;
87  scaled_yres_ = src.scaled_yres_;
88  rect_left_ = src.rect_left_;
89  rect_top_ = src.rect_top_;
90  rect_width_ = src.rect_width_;
91  rect_height_ = src.rect_height_;
92  delete it_;
93  it_ = new PAGE_RES_IT(*src.it_);
94  BeginWord(src.blob_index_);
95  return *this;
96 }
TESS_LOCAL void BeginWord(int offset)

◆ Orientation()

void tesseract::PageIterator::Orientation ( tesseract::Orientation orientation,
tesseract::WritingDirection writing_direction,
tesseract::TextlineOrder textline_order,
float *  deskew_angle 
) const

Returns orientation for the block the iterator points to. orientation, writing_direction, textline_order: see publictypes.h deskew_angle: after rotating the block so the text orientation is upright, how many radians does one have to rotate the block anti-clockwise for it to be level? -Pi/4 <= deskew_angle <= Pi/4

Definition at line 511 of file pageiterator.cpp.

514  {
515  BLOCK* block = it_->block()->block;
516 
517  // Orientation
518  FCOORD up_in_image(0.0, 1.0);
519  up_in_image.unrotate(block->classify_rotation());
520  up_in_image.rotate(block->re_rotation());
521 
522  if (up_in_image.x() == 0.0F) {
523  if (up_in_image.y() > 0.0F) {
524  *orientation = ORIENTATION_PAGE_UP;
525  } else {
526  *orientation = ORIENTATION_PAGE_DOWN;
527  }
528  } else if (up_in_image.x() > 0.0F) {
529  *orientation = ORIENTATION_PAGE_RIGHT;
530  } else {
531  *orientation = ORIENTATION_PAGE_LEFT;
532  }
533 
534  // Writing direction
535  bool is_vertical_text = (block->classify_rotation().x() == 0.0);
536  bool right_to_left = block->right_to_left();
537  *writing_direction =
538  is_vertical_text
540  : (right_to_left
543 
544  // Textline Order
545  const bool is_mongolian = false; // TODO(eger): fix me
546  *textline_order = is_vertical_text
547  ? (is_mongolian
551 
552  // Deskew angle
553  FCOORD skew = block->skew(); // true horizontal for textlines
554  *deskew_angle = -skew.angle();
555 }
BLOCK_RES * block() const
Definition: pageres.h:757
bool right_to_left() const
Definition: ocrblock.h:81
FCOORD re_rotation() const
Definition: ocrblock.h:136
float angle() const
find angle
Definition: points.h:248
FCOORD skew() const
Definition: ocrblock.h:148
BLOCK * block
Definition: pageres.h:117
FCOORD classify_rotation() const
Definition: ocrblock.h:142
Definition: ocrblock.h:30
Definition: points.h:189
float x() const
Definition: points.h:208

◆ ParagraphInfo()

void tesseract::PageIterator::ParagraphInfo ( tesseract::ParagraphJustification justification,
bool *  is_list_item,
bool *  is_crown,
int *  first_line_indent 
) const

Returns information about the current paragraph, if available.

justification - LEFT if ragged right, or fully justified and script is left-to-right. RIGHT if ragged left, or fully justified and script is right-to-left. unknown if it looks like source code or we have very few lines. is_list_item - true if we believe this is a member of an ordered or unordered list. is_crown - true if the first line of the paragraph is aligned with the other lines of the paragraph even though subsequent paragraphs have first line indents. This typically indicates that this is the continuation of a previous paragraph or that it is the very first paragraph in the chapter. first_line_indent - For LEFT aligned paragraphs, the first text line of paragraphs of this kind are indented this many pixels from the left edge of the rest of the paragraph. for RIGHT aligned paragraphs, the first text line of paragraphs of this kind are indented this many pixels from the right edge of the rest of the paragraph. NOTE 1: This value may be negative. NOTE 2: if *is_crown == true, the first line of this paragraph is actually flush, and first_line_indent is set to the "common" first_line_indent for subsequent paragraphs in this block of text.

Definition at line 557 of file pageiterator.cpp.

560  {
562  if (!it_->row() || !it_->row()->row || !it_->row()->row->para() ||
563  !it_->row()->row->para()->model)
564  return;
565 
566  PARA *para = it_->row()->row->para();
567  *is_list_item = para->is_list_item;
568  *is_crown = para->is_very_first_or_continuation;
569  *first_line_indent = para->model->first_indent() -
570  para->model->body_indent();
571  *just = para->model->justification();
572 }
int body_indent() const
Definition: ocrpara.h:169
ROW_RES * row() const
Definition: pageres.h:754
tesseract::ParagraphJustification justification() const
Definition: ocrpara.h:164
bool is_list_item
Definition: ocrpara.h:38
Definition: ocrpara.h:29
int first_indent() const
Definition: ocrpara.h:168
const ParagraphModel * model
Definition: ocrpara.h:36
bool is_very_first_or_continuation
Definition: ocrpara.h:43
ROW * row
Definition: pageres.h:143
PARA * para() const
Definition: ocrrow.h:118

◆ PositionedAtSameWord()

bool tesseract::PageIterator::PositionedAtSameWord ( const PAGE_RES_IT other) const

Are we positioned at the same location as other?

Definition at line 98 of file pageiterator.cpp.

98  {
99  return (it_ == nullptr && it_ == other) ||
100  ((other != nullptr) && (it_ != nullptr) && (*it_ == *other));
101 }

◆ RestartParagraph()

void tesseract::PageIterator::RestartParagraph ( )
virtual

Moves the iterator to the beginning of the paragraph. This class implements this functionality by moving it to the zero indexed blob of the first (leftmost) word on the first row of the paragraph.

Definition at line 111 of file pageiterator.cpp.

111  {
112  if (it_->block() == nullptr) return; // At end of the document.
113  PAGE_RES_IT para(page_res_);
114  PAGE_RES_IT next_para(para);
115  next_para.forward_paragraph();
116  while (next_para.cmp(*it_) <= 0) {
117  para = next_para;
118  next_para.forward_paragraph();
119  }
120  *it_ = para;
121  BeginWord(0);
122 }
BLOCK_RES * block() const
Definition: pageres.h:757
TESS_LOCAL void BeginWord(int offset)

◆ RestartRow()

void tesseract::PageIterator::RestartRow ( )
virtual

Moves the iterator to the beginning of the text line. This class implements this functionality by moving it to the zero indexed blob of the first (leftmost) word of the row.

Definition at line 130 of file pageiterator.cpp.

130  {
131  it_->restart_row();
132  BeginWord(0);
133 }
TESS_LOCAL void BeginWord(int offset)
WERD_RES * restart_row()
Definition: pageres.cpp:1637

◆ SetBoundingBoxComponents()

void tesseract::PageIterator::SetBoundingBoxComponents ( bool  include_upper_dots,
bool  include_lower_dots 
)
inline

Controls what to include in a bounding box. Bounding boxes of all levels between RIL_WORD and RIL_BLOCK can include or exclude potential diacritics. Between layout analysis and recognition, it isn't known where all diacritics belong, so this control is used to include or exclude some diacritics that are above or below the main body of the word. In most cases where the placement is obvious, and after recognition, it doesn't make as much difference, as the diacritics will already be included in the word.

Definition at line 191 of file pageiterator.h.

192  {
193  include_upper_dots_ = include_upper_dots;
194  include_lower_dots_ = include_lower_dots;
195  }

◆ SetWordBlamerBundle()

bool tesseract::PageIterator::SetWordBlamerBundle ( BlamerBundle blamer_bundle)

Definition at line 618 of file pageiterator.cpp.

618  {
619  if (it_->word() != nullptr) {
620  it_->word()->blamer_bundle = blamer_bundle;
621  return true;
622  } else {
623  return false;
624  }
625 }
WERD_RES * word() const
Definition: pageres.h:751
BlamerBundle * blamer_bundle
Definition: pageres.h:246

Member Data Documentation

◆ blob_index_

int tesseract::PageIterator::blob_index_
protected

The current blob index within the word.

Definition at line 343 of file pageiterator.h.

◆ cblob_it_

C_BLOB_IT* tesseract::PageIterator::cblob_it_
protected

Iterator to the blobs within the word. If nullptr, then we are iterating OCR results in the box_word. Owned by this ResultIterator.

Definition at line 349 of file pageiterator.h.

◆ include_lower_dots_

bool tesseract::PageIterator::include_lower_dots_
protected

Definition at line 352 of file pageiterator.h.

◆ include_upper_dots_

bool tesseract::PageIterator::include_upper_dots_
protected

Control over what to include in bounding boxes.

Definition at line 351 of file pageiterator.h.

◆ it_

PAGE_RES_IT* tesseract::PageIterator::it_
protected

The iterator to the page_res_. Owned by this ResultIterator. A pointer just to avoid dragging in Tesseract includes.

Definition at line 334 of file pageiterator.h.

◆ page_res_

PAGE_RES* tesseract::PageIterator::page_res_
protected

Pointer to the page_res owned by the API.

Definition at line 327 of file pageiterator.h.

◆ rect_height_

int tesseract::PageIterator::rect_height_
protected

Definition at line 359 of file pageiterator.h.

◆ rect_left_

int tesseract::PageIterator::rect_left_
protected

Definition at line 356 of file pageiterator.h.

◆ rect_top_

int tesseract::PageIterator::rect_top_
protected

Definition at line 357 of file pageiterator.h.

◆ rect_width_

int tesseract::PageIterator::rect_width_
protected

Definition at line 358 of file pageiterator.h.

◆ scale_

int tesseract::PageIterator::scale_
protected

Parameters saved from the Thresholder. Needed to rebuild coordinates.

Definition at line 354 of file pageiterator.h.

◆ scaled_yres_

int tesseract::PageIterator::scaled_yres_
protected

Definition at line 355 of file pageiterator.h.

◆ tesseract_

Tesseract* tesseract::PageIterator::tesseract_
protected

Pointer to the Tesseract object owned by the API.

Definition at line 329 of file pageiterator.h.

◆ word_

WERD* tesseract::PageIterator::word_
protected

The current input WERD being iterated. If there is an output from OCR, then word_ is nullptr. Owned by the API

Definition at line 339 of file pageiterator.h.

◆ word_length_

int tesseract::PageIterator::word_length_
protected

The length of the current word_.

Definition at line 341 of file pageiterator.h.


The documentation for this class was generated from the following files: