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

#include <colpartitiongrid.h>

Inheritance diagram for tesseract::ColPartitionGrid:
tesseract::BBGrid< ColPartition, ColPartition_CLIST, ColPartition_C_IT > tesseract::GridBase

Public Member Functions

 ColPartitionGrid ()=default
 
 ColPartitionGrid (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
virtual ~ColPartitionGrid ()=default
 
void HandleClick (int x, int y)
 
void Merges (TessResultCallback2< bool, ColPartition *, TBOX *> *box_cb, TessResultCallback2< bool, const ColPartition *, const ColPartition *> *confirm_cb)
 
bool MergePart (TessResultCallback2< bool, ColPartition *, TBOX *> *box_cb, TessResultCallback2< bool, const ColPartition *, const ColPartition *> *confirm_cb, ColPartition *part)
 
int ComputeTotalOverlap (ColPartitionGrid **overlap_grid)
 
void FindOverlappingPartitions (const TBOX &box, const ColPartition *not_this, ColPartition_CLIST *parts)
 
ColPartitionBestMergeCandidate (const ColPartition *part, ColPartition_CLIST *candidates, bool debug, TessResultCallback2< bool, const ColPartition *, const ColPartition *> *confirm_cb, int *overlap_increase)
 
void SplitOverlappingPartitions (ColPartition_LIST *big_parts)
 
bool GridSmoothNeighbours (BlobTextFlowType source_type, Pix *nontext_map, const TBOX &im_box, const FCOORD &rerotation)
 
void ReflectInYAxis ()
 
void Deskew (const FCOORD &deskew)
 
void ExtractPartitionsAsBlocks (BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
 
void SetTabStops (TabFind *tabgrid)
 
bool MakeColPartSets (PartSetVector *part_sets)
 
ColPartitionSetMakeSingleColumnSet (WidthCallback *cb)
 
void ClaimBoxes ()
 
void ReTypeBlobs (BLOBNBOX_LIST *im_blobs)
 
void RecomputeBounds (int gridsize, const ICOORD &bleft, const ICOORD &tright, const ICOORD &vertical)
 
void GridFindMargins (ColPartitionSet **best_columns)
 
void ListFindMargins (ColPartitionSet **best_columns, ColPartition_LIST *parts)
 
void DeleteParts ()
 
void DeleteUnknownParts (TO_BLOCK *block)
 
void DeleteNonLeaderParts ()
 
void FindFigureCaptions ()
 
void FindPartitionPartners ()
 
void FindPartitionPartners (bool upper, ColPartition *part)
 
void FindVPartitionPartners (bool to_the_left, ColPartition *part)
 
void RefinePartitionPartners (bool get_desperate)
 
- Public Member Functions inherited from tesseract::BBGrid< ColPartition, ColPartition_CLIST, ColPartition_C_IT >
 BBGrid ()
 
 BBGrid (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
virtual ~BBGrid ()
 
void Init (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
void Clear ()
 
void ClearGridData (void(*free_method)(ColPartition *))
 
void InsertBBox (bool h_spread, bool v_spread, ColPartition *bbox)
 
void InsertPixPtBBox (int left, int bottom, Pix *pix, ColPartition *bbox)
 
void RemoveBBox (ColPartition *bbox)
 
bool RectangleEmpty (const TBOX &rect)
 
IntGridCountCellElements ()
 
ScrollViewMakeWindow (int x, int y, const char *window_name)
 
void DisplayBoxes (ScrollView *window)
 
void AssertNoDuplicates ()
 
- Public Member Functions inherited from tesseract::GridBase
 GridBase ()=default
 
 GridBase (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
virtual ~GridBase ()
 
void Init (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
int gridsize () const
 
int gridwidth () const
 
int gridheight () const
 
const ICOORDbleft () const
 
const ICOORDtright () const
 
void GridCoords (int x, int y, int *grid_x, int *grid_y) const
 
void ClipGridCoords (int *x, int *y) const
 

Additional Inherited Members

- Protected Attributes inherited from tesseract::BBGrid< ColPartition, ColPartition_CLIST, ColPartition_C_IT >
ColPartition_CLIST * grid_
 
- Protected Attributes inherited from tesseract::GridBase
int gridsize_
 
int gridwidth_
 
int gridheight_
 
int gridbuckets_
 
ICOORD bleft_
 
ICOORD tright_
 

Detailed Description

Definition at line 33 of file colpartitiongrid.h.

Constructor & Destructor Documentation

◆ ColPartitionGrid() [1/2]

tesseract::ColPartitionGrid::ColPartitionGrid ( )
default

◆ ColPartitionGrid() [2/2]

tesseract::ColPartitionGrid::ColPartitionGrid ( int  gridsize,
const ICOORD bleft,
const ICOORD tright 
)

Definition at line 69 of file colpartitiongrid.cpp.

71  : BBGrid<ColPartition, ColPartition_CLIST, ColPartition_C_IT>(gridsize,
72  bleft, tright) {
73 }
int gridsize() const
Definition: bbgrid.h:64
const ICOORD & bleft() const
Definition: bbgrid.h:73
const ICOORD & tright() const
Definition: bbgrid.h:76

◆ ~ColPartitionGrid()

virtual tesseract::ColPartitionGrid::~ColPartitionGrid ( )
virtualdefault

Member Function Documentation

◆ BestMergeCandidate()

ColPartition * tesseract::ColPartitionGrid::BestMergeCandidate ( const ColPartition part,
ColPartition_CLIST *  candidates,
bool  debug,
TessResultCallback2< bool, const ColPartition *, const ColPartition *> *  confirm_cb,
int *  overlap_increase 
)

Definition at line 406 of file colpartitiongrid.cpp.

409  {
410  if (overlap_increase != nullptr)
411  *overlap_increase = 0;
412  if (candidates->empty())
413  return nullptr;
414  int ok_overlap =
415  static_cast<int>(kTinyEnoughTextlineOverlapFraction * gridsize() + 0.5);
416  // The best neighbour to merge with is the one that causes least
417  // total pairwise overlap among all the neighbours.
418  // If more than one offers the same total overlap, choose the one
419  // with the least total area.
420  const TBOX& part_box = part->bounding_box();
421  ColPartition_C_IT it(candidates);
422  ColPartition* best_candidate = nullptr;
423  // Find the total combined box of all candidates and the original.
424  TBOX full_box(part_box);
425  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
426  ColPartition* candidate = it.data();
427  full_box += candidate->bounding_box();
428  }
429  // Keep valid neighbours in a list.
430  ColPartition_CLIST neighbours;
431  // Now run a rect search of the merged box for overlapping neighbours, as
432  // we need anything that might be overlapped by the merged box.
433  FindOverlappingPartitions(full_box, part, &neighbours);
434  if (debug) {
435  tprintf("Finding best merge candidate from %d, %d neighbours for box:",
436  candidates->length(), neighbours.length());
437  part_box.print();
438  }
439  // If the best increase in overlap is positive, then we also check the
440  // worst non-candidate overlap. This catches the case of multiple good
441  // candidates that overlap each other when merged. If the worst
442  // non-candidate overlap is better than the best overlap, then return
443  // the worst non-candidate overlap instead.
444  ColPartition_CLIST non_candidate_neighbours;
445  non_candidate_neighbours.set_subtract(SortByBoxLeft<ColPartition>, true,
446  &neighbours, candidates);
447  int worst_nc_increase = 0;
448  int best_increase = INT32_MAX;
449  int best_area = 0;
450  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
451  ColPartition* candidate = it.data();
452  if (confirm_cb != nullptr && !confirm_cb->Run(part, candidate)) {
453  if (debug) {
454  tprintf("Candidate not confirmed:");
455  candidate->bounding_box().print();
456  }
457  continue;
458  }
459  int increase = IncreaseInOverlap(part, candidate, ok_overlap, &neighbours);
460  const TBOX& cand_box = candidate->bounding_box();
461  if (best_candidate == nullptr || increase < best_increase) {
462  best_candidate = candidate;
463  best_increase = increase;
464  best_area = cand_box.bounding_union(part_box).area() - cand_box.area();
465  if (debug) {
466  tprintf("New best merge candidate has increase %d, area %d, over box:",
467  increase, best_area);
468  full_box.print();
469  candidate->Print();
470  }
471  } else if (increase == best_increase) {
472  int area = cand_box.bounding_union(part_box).area() - cand_box.area();
473  if (area < best_area) {
474  best_area = area;
475  best_candidate = candidate;
476  }
477  }
478  increase = IncreaseInOverlap(part, candidate, ok_overlap,
479  &non_candidate_neighbours);
480  if (increase > worst_nc_increase)
481  worst_nc_increase = increase;
482  }
483  if (best_increase > 0) {
484  // If the worst non-candidate increase is less than the best increase
485  // including the candidates, then all the candidates can merge together
486  // and the increase in outside overlap would be less, so use that result,
487  // but only if each candidate is either a good diacritic merge with part,
488  // or an ok merge candidate with all the others.
489  // See TestCompatibleCandidates for more explanation and a picture.
490  if (worst_nc_increase < best_increase &&
491  TestCompatibleCandidates(*part, debug, candidates)) {
492  best_increase = worst_nc_increase;
493  }
494  }
495  if (overlap_increase != nullptr)
496  *overlap_increase = best_increase;
497  return best_candidate;
498 }
int gridsize() const
Definition: bbgrid.h:64
void print() const
Definition: rect.h:278
Definition: rect.h:34
const double kTinyEnoughTextlineOverlapFraction
virtual R Run(A1, A2)=0
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
int32_t area() const
Definition: rect.h:122
void FindOverlappingPartitions(const TBOX &box, const ColPartition *not_this, ColPartition_CLIST *parts)
TBOX bounding_union(const TBOX &box) const
Definition: rect.cpp:129

◆ ClaimBoxes()

void tesseract::ColPartitionGrid::ClaimBoxes ( )

Definition at line 864 of file colpartitiongrid.cpp.

864  {
865  // Iterate the ColPartitions in the grid.
866  ColPartitionGridSearch gsearch(this);
867  gsearch.StartFullSearch();
868  ColPartition* part;
869  while ((part = gsearch.NextFullSearch()) != nullptr) {
870  part->ClaimBoxes();
871  }
872 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936

◆ ComputeTotalOverlap()

int tesseract::ColPartitionGrid::ComputeTotalOverlap ( ColPartitionGrid **  overlap_grid)

Definition at line 319 of file colpartitiongrid.cpp.

319  {
320  int total_overlap = 0;
321  // Iterate the ColPartitions in the grid.
322  ColPartitionGridSearch gsearch(this);
323  gsearch.StartFullSearch();
324  ColPartition* part;
325  while ((part = gsearch.NextFullSearch()) != nullptr) {
326  ColPartition_CLIST neighbors;
327  const TBOX& part_box = part->bounding_box();
328  FindOverlappingPartitions(part_box, part, &neighbors);
329  ColPartition_C_IT n_it(&neighbors);
330  bool any_part_overlap = false;
331  for (n_it.mark_cycle_pt(); !n_it.cycled_list(); n_it.forward()) {
332  const TBOX& n_box = n_it.data()->bounding_box();
333  int overlap = n_box.intersection(part_box).area();
334  if (overlap > 0 && overlap_grid != nullptr) {
335  if (*overlap_grid == nullptr) {
336  *overlap_grid = new ColPartitionGrid(gridsize(), bleft(), tright());
337  }
338  (*overlap_grid)->InsertBBox(true, true, n_it.data()->ShallowCopy());
339  if (!any_part_overlap) {
340  (*overlap_grid)->InsertBBox(true, true, part->ShallowCopy());
341  }
342  }
343  any_part_overlap = true;
344  total_overlap += overlap;
345  }
346  }
347  return total_overlap;
348 }
int gridsize() const
Definition: bbgrid.h:64
TBOX intersection(const TBOX &box) const
Definition: rect.cpp:87
const ICOORD & bleft() const
Definition: bbgrid.h:73
Definition: rect.h:34
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
int32_t area() const
Definition: rect.h:122
void FindOverlappingPartitions(const TBOX &box, const ColPartition *not_this, ColPartition_CLIST *parts)
const ICOORD & tright() const
Definition: bbgrid.h:76

◆ DeleteNonLeaderParts()

void tesseract::ColPartitionGrid::DeleteNonLeaderParts ( )

Definition at line 1043 of file colpartitiongrid.cpp.

1043  {
1044  ColPartitionGridSearch gsearch(this);
1045  gsearch.StartFullSearch();
1046  ColPartition* part;
1047  while ((part = gsearch.NextFullSearch()) != nullptr) {
1048  if (part->flow() != BTFT_LEADER) {
1049  gsearch.RemoveBBox();
1050  if (part->ReleaseNonLeaderBoxes()) {
1051  InsertBBox(true, true, part);
1052  gsearch.RepositionIterator();
1053  } else {
1054  delete part;
1055  }
1056  }
1057  }
1058 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:488

◆ DeleteParts()

void tesseract::ColPartitionGrid::DeleteParts ( )

Definition at line 1009 of file colpartitiongrid.cpp.

1009  {
1010  ColPartition_LIST dead_parts;
1011  ColPartition_IT dead_it(&dead_parts);
1012  ColPartitionGridSearch gsearch(this);
1013  gsearch.StartFullSearch();
1014  ColPartition* part;
1015  while ((part = gsearch.NextFullSearch()) != nullptr) {
1016  part->DisownBoxes();
1017  dead_it.add_to_end(part); // Parts will be deleted on return.
1018  }
1019  Clear();
1020 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936

◆ DeleteUnknownParts()

void tesseract::ColPartitionGrid::DeleteUnknownParts ( TO_BLOCK block)

Definition at line 1024 of file colpartitiongrid.cpp.

1024  {
1025  ColPartitionGridSearch gsearch(this);
1026  gsearch.StartFullSearch();
1027  ColPartition* part;
1028  while ((part = gsearch.NextFullSearch()) != nullptr) {
1029  if (part->blob_type() == BRT_UNKNOWN) {
1030  gsearch.RemoveBBox();
1031  // Once marked, the blobs will be swept up by DeleteUnownedNoise.
1032  part->set_flow(BTFT_NONTEXT);
1033  part->set_blob_type(BRT_NOISE);
1034  part->SetBlobTypes();
1035  part->DisownBoxes();
1036  delete part;
1037  }
1038  }
1039  block->DeleteUnownedNoise();
1040 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
void DeleteUnownedNoise()
Definition: blobbox.cpp:1038

◆ Deskew()

void tesseract::ColPartitionGrid::Deskew ( const FCOORD deskew)

Definition at line 736 of file colpartitiongrid.cpp.

736  {
737  ColPartition_LIST parts;
738  ColPartition_IT part_it(&parts);
739  // Iterate the ColPartitions in the grid to extract them.
740  ColPartitionGridSearch gsearch(this);
741  gsearch.StartFullSearch();
742  ColPartition* part;
743  while ((part = gsearch.NextFullSearch()) != nullptr) {
744  part_it.add_after_then_move(part);
745  }
746  // Rebuild the grid to the new size.
747  TBOX grid_box(bleft_, tright_);
748  grid_box.rotate_large(deskew);
749  Init(gridsize(), grid_box.botleft(), grid_box.topright());
750  // Reinitializing the grid with rotated coords also clears all the
751  // pointers, so parts will now own the ColPartitions. (Briefly).
752  for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
753  part = part_it.extract();
754  part->ComputeLimits();
755  InsertBBox(true, true, part);
756  }
757 }
int gridsize() const
Definition: bbgrid.h:64
Definition: rect.h:34
ICOORD tright_
Definition: bbgrid.h:92
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:488
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.h:447

◆ ExtractPartitionsAsBlocks()

void tesseract::ColPartitionGrid::ExtractPartitionsAsBlocks ( BLOCK_LIST *  blocks,
TO_BLOCK_LIST *  to_blocks 
)

Definition at line 675 of file colpartitiongrid.cpp.

676  {
677  TO_BLOCK_IT to_block_it(to_blocks);
678  BLOCK_IT block_it(blocks);
679  // All partitions will be put on this list and deleted on return.
680  ColPartition_LIST parts;
681  ColPartition_IT part_it(&parts);
682  // Iterate the ColPartitions in the grid to extract them.
683  ColPartitionGridSearch gsearch(this);
684  gsearch.StartFullSearch();
685  ColPartition* part;
686  while ((part = gsearch.NextFullSearch()) != nullptr) {
687  part_it.add_after_then_move(part);
688  // The partition has to be at least vaguely like text.
689  BlobRegionType blob_type = part->blob_type();
690  if (BLOBNBOX::IsTextType(blob_type) ||
691  (blob_type == BRT_UNKNOWN && part->boxes_count() > 1)) {
692  PolyBlockType type = blob_type == BRT_VERT_TEXT ? PT_VERTICAL_TEXT
693  : PT_FLOWING_TEXT;
694  // Get metrics from the row that will be used for the block.
695  TBOX box = part->bounding_box();
696  int median_width = part->median_width();
697  int median_height = part->median_height();
698  // Turn the partition into a TO_ROW.
699  TO_ROW* row = part->MakeToRow();
700  if (row == nullptr) {
701  // This partition is dead.
702  part->DeleteBoxes();
703  continue;
704  }
705  BLOCK* block = new BLOCK("", true, 0, 0, box.left(), box.bottom(),
706  box.right(), box.top());
707  block->pdblk.set_poly_block(new POLY_BLOCK(box, type));
708  TO_BLOCK* to_block = new TO_BLOCK(block);
709  TO_ROW_IT row_it(to_block->get_rows());
710  row_it.add_after_then_move(row);
711  // We haven't differentially rotated vertical and horizontal text at
712  // this point, so use width or height as appropriate.
713  if (blob_type == BRT_VERT_TEXT) {
714  to_block->line_size = static_cast<float>(median_width);
715  to_block->line_spacing = static_cast<float>(box.width());
716  to_block->max_blob_size = static_cast<float>(box.width() + 1);
717  } else {
718  to_block->line_size = static_cast<float>(median_height);
719  to_block->line_spacing = static_cast<float>(box.height());
720  to_block->max_blob_size = static_cast<float>(box.height() + 1);
721  }
722  if (to_block->line_size == 0) to_block->line_size = 1;
723  block_it.add_to_end(block);
724  to_block_it.add_to_end(to_block);
725  } else {
726  // This partition is dead.
727  part->DeleteBoxes();
728  }
729  }
730  Clear();
731  // Now it is safe to delete the ColPartitions as parts goes out of scope.
732 }
void set_poly_block(POLY_BLOCK *blk)
set the poly block
Definition: pdblock.h:58
BlobRegionType
Definition: blobbox.h:73
Definition: rect.h:34
PolyBlockType
Definition: publictypes.h:53
TO_ROW_LIST * get_rows()
Definition: blobbox.h:717
float line_spacing
Definition: blobbox.h:792
int16_t width() const
Definition: rect.h:115
float max_blob_size
Definition: blobbox.h:799
int16_t left() const
Definition: rect.h:72
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
int16_t top() const
Definition: rect.h:58
Definition: ocrblock.h:30
int16_t right() const
Definition: rect.h:79
int16_t bottom() const
Definition: rect.h:65
PDBLK pdblk
Definition: ocrblock.h:192
int16_t height() const
Definition: rect.h:108
static bool IsTextType(BlobRegionType type)
Definition: blobbox.h:419
float line_size
Definition: blobbox.h:798

◆ FindFigureCaptions()

void tesseract::ColPartitionGrid::FindFigureCaptions ( )

Definition at line 1061 of file colpartitiongrid.cpp.

1061  {
1062  // For each image region find its best candidate text caption region,
1063  // if any and mark it as such.
1064  ColPartitionGridSearch gsearch(this);
1065  gsearch.StartFullSearch();
1066  ColPartition* part;
1067  while ((part = gsearch.NextFullSearch()) != nullptr) {
1068  if (part->IsImageType()) {
1069  const TBOX& part_box = part->bounding_box();
1070  bool debug = AlignedBlob::WithinTestRegion(2, part_box.left(),
1071  part_box.bottom());
1072  ColPartition* best_caption = nullptr;
1073  int best_dist = 0; // Distance to best_caption.
1074  int best_upper = 0; // Direction of best_caption.
1075  // Handle both lower and upper directions.
1076  for (int upper = 0; upper < 2; ++upper) {
1077  ColPartition_C_IT partner_it(upper ? part->upper_partners()
1078  : part->lower_partners());
1079  // If there are no image partners, then this direction is ok.
1080  for (partner_it.mark_cycle_pt(); !partner_it.cycled_list();
1081  partner_it.forward()) {
1082  ColPartition* partner = partner_it.data();
1083  if (partner->IsImageType()) {
1084  break;
1085  }
1086  }
1087  if (!partner_it.cycled_list()) continue;
1088  // Find the nearest totally overlapping text partner.
1089  for (partner_it.mark_cycle_pt(); !partner_it.cycled_list();
1090  partner_it.forward()) {
1091  ColPartition* partner = partner_it.data();
1092  if (!partner->IsTextType() || partner->type() == PT_TABLE) continue;
1093  const TBOX& partner_box = partner->bounding_box();
1094  if (debug) {
1095  tprintf("Finding figure captions for image part:");
1096  part_box.print();
1097  tprintf("Considering partner:");
1098  partner_box.print();
1099  }
1100  if (partner_box.left() >= part_box.left() &&
1101  partner_box.right() <= part_box.right()) {
1102  int dist = partner_box.y_gap(part_box);
1103  if (best_caption == nullptr || dist < best_dist) {
1104  best_dist = dist;
1105  best_caption = partner;
1106  best_upper = upper;
1107  }
1108  }
1109  }
1110  }
1111  if (best_caption != nullptr) {
1112  if (debug) {
1113  tprintf("Best caption candidate:");
1114  best_caption->bounding_box().print();
1115  }
1116  // We have a candidate caption. Qualify it as being separable from
1117  // any body text. We are looking for either a small number of lines
1118  // or a big gap that indicates a separation from the body text.
1119  int line_count = 0;
1120  int biggest_gap = 0;
1121  int smallest_gap = INT16_MAX;
1122  int total_height = 0;
1123  int mean_height = 0;
1124  ColPartition* end_partner = nullptr;
1125  ColPartition* next_partner = nullptr;
1126  for (ColPartition* partner = best_caption; partner != nullptr &&
1127  line_count <= kMaxCaptionLines;
1128  partner = next_partner) {
1129  if (!partner->IsTextType()) {
1130  end_partner = partner;
1131  break;
1132  }
1133  ++line_count;
1134  total_height += partner->bounding_box().height();
1135  next_partner = partner->SingletonPartner(best_upper);
1136  if (next_partner != nullptr) {
1137  int gap = partner->bounding_box().y_gap(
1138  next_partner->bounding_box());
1139  if (gap > biggest_gap) {
1140  biggest_gap = gap;
1141  end_partner = next_partner;
1142  mean_height = total_height / line_count;
1143  } else if (gap < smallest_gap) {
1144  smallest_gap = gap;
1145  }
1146  // If the gap looks big compared to the text size and the smallest
1147  // gap seen so far, then we can stop.
1148  if (biggest_gap > mean_height * kMinCaptionGapHeightRatio &&
1149  biggest_gap > smallest_gap * kMinCaptionGapRatio)
1150  break;
1151  }
1152  }
1153  if (debug) {
1154  tprintf("Line count=%d, biggest gap %d, smallest%d, mean height %d\n",
1155  line_count, biggest_gap, smallest_gap, mean_height);
1156  if (end_partner != nullptr) {
1157  tprintf("End partner:");
1158  end_partner->bounding_box().print();
1159  }
1160  }
1161  if (next_partner == nullptr && line_count <= kMaxCaptionLines)
1162  end_partner = nullptr; // No gap, but line count is small.
1163  if (line_count <= kMaxCaptionLines) {
1164  // This is a qualified caption. Mark the text as caption.
1165  for (ColPartition* partner = best_caption; partner != nullptr &&
1166  partner != end_partner;
1167  partner = next_partner) {
1168  partner->set_type(PT_CAPTION_TEXT);
1169  partner->SetBlobTypes();
1170  if (debug) {
1171  tprintf("Set caption type for partition:");
1172  partner->bounding_box().print();
1173  }
1174  next_partner = partner->SingletonPartner(best_upper);
1175  }
1176  }
1177  }
1178  }
1179  }
1180 }
const double kMinCaptionGapRatio
void print() const
Definition: rect.h:278
int y_gap(const TBOX &box) const
Definition: rect.h:233
Definition: rect.h:34
static bool WithinTestRegion(int detail_level, int x, int y)
Definition: capi.h:100
int16_t left() const
Definition: rect.h:72
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
const double kMinCaptionGapHeightRatio
const int kMaxCaptionLines
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
int16_t right() const
Definition: rect.h:79
int16_t bottom() const
Definition: rect.h:65

◆ FindOverlappingPartitions()

void tesseract::ColPartitionGrid::FindOverlappingPartitions ( const TBOX box,
const ColPartition not_this,
ColPartition_CLIST *  parts 
)

Definition at line 353 of file colpartitiongrid.cpp.

355  {
356  ColPartitionGridSearch rsearch(this);
357  rsearch.StartRectSearch(box);
358  ColPartition* part;
359  while ((part = rsearch.NextRectSearch()) != nullptr) {
360  if (part != not_this)
361  parts->add_sorted(SortByBoxLeft<ColPartition>, true, part);
362  }
363 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936

◆ FindPartitionPartners() [1/2]

void tesseract::ColPartitionGrid::FindPartitionPartners ( )

Definition at line 1186 of file colpartitiongrid.cpp.

1186  {
1187  ColPartitionGridSearch gsearch(this);
1188  gsearch.StartFullSearch();
1189  ColPartition* part;
1190  while ((part = gsearch.NextFullSearch()) != nullptr) {
1191  if (part->IsVerticalType()) {
1192  FindVPartitionPartners(true, part);
1193  FindVPartitionPartners(false, part);
1194  } else {
1195  FindPartitionPartners(true, part);
1196  FindPartitionPartners(false, part);
1197  }
1198  }
1199 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
void FindVPartitionPartners(bool to_the_left, ColPartition *part)

◆ FindPartitionPartners() [2/2]

void tesseract::ColPartitionGrid::FindPartitionPartners ( bool  upper,
ColPartition part 
)

Definition at line 1203 of file colpartitiongrid.cpp.

1203  {
1204  if (part->type() == PT_NOISE)
1205  return; // Noise is not allowed to partner anything.
1206  const TBOX& box = part->bounding_box();
1207  int top = part->median_top();
1208  int bottom = part->median_bottom();
1209  int height = top - bottom;
1210  int mid_y = (bottom + top) / 2;
1211  ColPartitionGridSearch vsearch(this);
1212  // Search down for neighbour below
1213  vsearch.StartVerticalSearch(box.left(), box.right(), part->MidY());
1214  ColPartition* neighbour;
1215  ColPartition* best_neighbour = nullptr;
1216  int best_dist = INT32_MAX;
1217  while ((neighbour = vsearch.NextVerticalSearch(!upper)) != nullptr) {
1218  if (neighbour == part || neighbour->type() == PT_NOISE)
1219  continue; // Noise is not allowed to partner anything.
1220  int neighbour_bottom = neighbour->median_bottom();
1221  int neighbour_top = neighbour->median_top();
1222  int neighbour_y = (neighbour_bottom + neighbour_top) / 2;
1223  if (upper != (neighbour_y > mid_y))
1224  continue;
1225  if (!part->HOverlaps(*neighbour) && !part->WithinSameMargins(*neighbour))
1226  continue;
1227  if (!part->TypesMatch(*neighbour)) {
1228  if (best_neighbour == nullptr)
1229  best_neighbour = neighbour;
1230  continue;
1231  }
1232  int dist = upper ? neighbour_bottom - top : bottom - neighbour_top;
1233  if (dist <= kMaxPartitionSpacing * height) {
1234  if (dist < best_dist) {
1235  best_dist = dist;
1236  best_neighbour = neighbour;
1237  }
1238  } else {
1239  break;
1240  }
1241  }
1242  if (best_neighbour != nullptr)
1243  part->AddPartner(upper, best_neighbour);
1244 }
Definition: capi.h:101
const double kMaxPartitionSpacing
Definition: rect.h:34
int16_t left() const
Definition: rect.h:72
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
int16_t right() const
Definition: rect.h:79

◆ FindVPartitionPartners()

void tesseract::ColPartitionGrid::FindVPartitionPartners ( bool  to_the_left,
ColPartition part 
)

Definition at line 1248 of file colpartitiongrid.cpp.

1249  {
1250  if (part->type() == PT_NOISE)
1251  return; // Noise is not allowed to partner anything.
1252  const TBOX& box = part->bounding_box();
1253  int left = part->median_left();
1254  int right = part->median_right();
1255  int width = right >= left ? right - left : -1;
1256  int mid_x = (left + right) / 2;
1257  ColPartitionGridSearch hsearch(this);
1258  // Search left for neighbour to_the_left
1259  hsearch.StartSideSearch(mid_x, box.bottom(), box.top());
1260  ColPartition* neighbour;
1261  ColPartition* best_neighbour = nullptr;
1262  int best_dist = INT32_MAX;
1263  while ((neighbour = hsearch.NextSideSearch(to_the_left)) != nullptr) {
1264  if (neighbour == part || neighbour->type() == PT_NOISE)
1265  continue; // Noise is not allowed to partner anything.
1266  int neighbour_left = neighbour->median_left();
1267  int neighbour_right = neighbour->median_right();
1268  int neighbour_x = (neighbour_left + neighbour_right) / 2;
1269  if (to_the_left != (neighbour_x < mid_x))
1270  continue;
1271  if (!part->VOverlaps(*neighbour))
1272  continue;
1273  if (!part->TypesMatch(*neighbour))
1274  continue; // Only match to other vertical text.
1275  int dist = to_the_left ? left - neighbour_right : neighbour_left - right;
1276  if (dist <= kMaxPartitionSpacing * width) {
1277  if (dist < best_dist || best_neighbour == nullptr) {
1278  best_dist = dist;
1279  best_neighbour = neighbour;
1280  }
1281  } else {
1282  break;
1283  }
1284  }
1285  // For vertical partitions, the upper partner is to the left, and lower is
1286  // to the right.
1287  if (best_neighbour != nullptr)
1288  part->AddPartner(to_the_left, best_neighbour);
1289 }
Definition: capi.h:101
const double kMaxPartitionSpacing
Definition: rect.h:34
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
int16_t top() const
Definition: rect.h:58
int16_t bottom() const
Definition: rect.h:65

◆ GridFindMargins()

void tesseract::ColPartitionGrid::GridFindMargins ( ColPartitionSet **  best_columns)

Definition at line 967 of file colpartitiongrid.cpp.

967  {
968  // Iterate the ColPartitions in the grid.
969  ColPartitionGridSearch gsearch(this);
970  gsearch.StartFullSearch();
971  ColPartition* part;
972  while ((part = gsearch.NextFullSearch()) != nullptr) {
973  // Set up a rectangle search x-bounded by the column and y by the part.
974  ColPartitionSet* columns = best_columns != nullptr
975  ? best_columns[gsearch.GridY()]
976  : nullptr;
977  FindPartitionMargins(columns, part);
978  const TBOX& box = part->bounding_box();
979  if (AlignedBlob::WithinTestRegion(2, box.left(), box.bottom())) {
980  tprintf("Computed margins for part:");
981  part->Print();
982  }
983  }
984 }
Definition: rect.h:34
static bool WithinTestRegion(int detail_level, int x, int y)
int16_t left() const
Definition: rect.h:72
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
int16_t bottom() const
Definition: rect.h:65

◆ GridSmoothNeighbours()

bool tesseract::ColPartitionGrid::GridSmoothNeighbours ( BlobTextFlowType  source_type,
Pix *  nontext_map,
const TBOX im_box,
const FCOORD rerotation 
)

Definition at line 625 of file colpartitiongrid.cpp.

628  {
629  // Iterate the ColPartitions in the grid.
630  ColPartitionGridSearch gsearch(this);
631  gsearch.StartFullSearch();
632  ColPartition* part;
633  bool any_changed = false;
634  while ((part = gsearch.NextFullSearch()) != nullptr) {
635  if (part->flow() != source_type || BLOBNBOX::IsLineType(part->blob_type()))
636  continue;
637  const TBOX& box = part->bounding_box();
638  bool debug = AlignedBlob::WithinTestRegion(2, box.left(), box.bottom());
639  if (SmoothRegionType(nontext_map, im_box, rotation, debug, part))
640  any_changed = true;
641  }
642  return any_changed;
643 }
Definition: rect.h:34
static bool WithinTestRegion(int detail_level, int x, int y)
int16_t left() const
Definition: rect.h:72
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
int16_t bottom() const
Definition: rect.h:65
static bool IsLineType(BlobRegionType type)
Definition: blobbox.h:427

◆ HandleClick()

void tesseract::ColPartitionGrid::HandleClick ( int  x,
int  y 
)
virtual

Reimplemented from tesseract::BBGrid< ColPartition, ColPartition_CLIST, ColPartition_C_IT >.

Definition at line 76 of file colpartitiongrid.cpp.

76  {
77  BBGrid<ColPartition,
78  ColPartition_CLIST, ColPartition_C_IT>::HandleClick(x, y);
79  // Run a radial search for partitions that overlap.
80  ColPartitionGridSearch radsearch(this);
81  radsearch.SetUniqueMode(true);
82  radsearch.StartRadSearch(x, y, 1);
83  ColPartition* neighbour;
84  FCOORD click(x, y);
85  while ((neighbour = radsearch.NextRadSearch()) != nullptr) {
86  const TBOX& nbox = neighbour->bounding_box();
87  if (nbox.contains(click)) {
88  tprintf("Block box:");
89  neighbour->bounding_box().print();
90  neighbour->Print();
91  }
92  }
93 }
Definition: rect.h:34
void HandleClick(int x, int y)
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
bool contains(const FCOORD pt) const
Definition: rect.h:333
Definition: points.h:189

◆ ListFindMargins()

void tesseract::ColPartitionGrid::ListFindMargins ( ColPartitionSet **  best_columns,
ColPartition_LIST *  parts 
)

Definition at line 991 of file colpartitiongrid.cpp.

992  {
993  ColPartition_IT part_it(parts);
994  for (part_it.mark_cycle_pt(); !part_it.cycled_list(); part_it.forward()) {
995  ColPartition* part = part_it.data();
996  ColPartitionSet* columns = nullptr;
997  if (best_columns != nullptr) {
998  const TBOX& part_box = part->bounding_box();
999  // Get the columns from the y grid coord.
1000  int grid_x, grid_y;
1001  GridCoords(part_box.left(), part_box.bottom(), &grid_x, &grid_y);
1002  columns = best_columns[grid_y];
1003  }
1004  FindPartitionMargins(columns, part);
1005  }
1006 }
Definition: rect.h:34
int16_t left() const
Definition: rect.h:72
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:53
int16_t bottom() const
Definition: rect.h:65

◆ MakeColPartSets()

bool tesseract::ColPartitionGrid::MakeColPartSets ( PartSetVector part_sets)

Definition at line 784 of file colpartitiongrid.cpp.

784  {
785  ColPartition_LIST* part_lists = new ColPartition_LIST[gridheight()];
786  part_sets->reserve(gridheight());
787  // Iterate the ColPartitions in the grid to get parts onto lists for the
788  // y bottom of each.
789  ColPartitionGridSearch gsearch(this);
790  gsearch.StartFullSearch();
791  ColPartition* part;
792  bool any_parts_found = false;
793  while ((part = gsearch.NextFullSearch()) != nullptr) {
794  BlobRegionType blob_type = part->blob_type();
795  if (blob_type != BRT_NOISE &&
796  (blob_type != BRT_UNKNOWN || !part->boxes()->singleton())) {
797  int grid_x, grid_y;
798  const TBOX& part_box = part->bounding_box();
799  GridCoords(part_box.left(), part_box.bottom(), &grid_x, &grid_y);
800  ColPartition_IT part_it(&part_lists[grid_y]);
801  part_it.add_to_end(part);
802  any_parts_found = true;
803  }
804  }
805  if (any_parts_found) {
806  for (int grid_y = 0; grid_y < gridheight(); ++grid_y) {
807  ColPartitionSet* line_set = nullptr;
808  if (!part_lists[grid_y].empty()) {
809  line_set = new ColPartitionSet(&part_lists[grid_y]);
810  }
811  part_sets->push_back(line_set);
812  }
813  }
814  delete [] part_lists;
815  return any_parts_found;
816 }
BlobRegionType
Definition: blobbox.h:73
Definition: rect.h:34
int16_t left() const
Definition: rect.h:72
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:53
int16_t bottom() const
Definition: rect.h:65
int gridheight() const
Definition: bbgrid.h:70

◆ MakeSingleColumnSet()

ColPartitionSet * tesseract::ColPartitionGrid::MakeSingleColumnSet ( WidthCallback cb)

Definition at line 822 of file colpartitiongrid.cpp.

822  {
823  ColPartition* single_column_part = nullptr;
824  // Iterate the ColPartitions in the grid to get parts onto lists for the
825  // y bottom of each.
826  ColPartitionGridSearch gsearch(this);
827  gsearch.StartFullSearch();
828  ColPartition* part;
829  while ((part = gsearch.NextFullSearch()) != nullptr) {
830  BlobRegionType blob_type = part->blob_type();
831  if (blob_type != BRT_NOISE &&
832  (blob_type != BRT_UNKNOWN || !part->boxes()->singleton())) {
833  // Consider for single column.
834  BlobTextFlowType flow = part->flow();
835  if ((blob_type == BRT_TEXT &&
836  (flow == BTFT_STRONG_CHAIN || flow == BTFT_CHAIN ||
837  flow == BTFT_LEADER || flow == BTFT_TEXT_ON_IMAGE)) ||
838  blob_type == BRT_RECTIMAGE || blob_type == BRT_POLYIMAGE) {
839  if (single_column_part == nullptr) {
840  single_column_part = part->ShallowCopy();
841  single_column_part->set_blob_type(BRT_TEXT);
842  // Copy the tabs from itself to properly setup the margins.
843  single_column_part->CopyLeftTab(*single_column_part, false);
844  single_column_part->CopyRightTab(*single_column_part, false);
845  } else {
846  if (part->left_key() < single_column_part->left_key())
847  single_column_part->CopyLeftTab(*part, false);
848  if (part->right_key() > single_column_part->right_key())
849  single_column_part->CopyRightTab(*part, false);
850  }
851  }
852  }
853  }
854  if (single_column_part != nullptr) {
855  // Make a ColPartitionSet out of the single_column_part as a candidate
856  // for the single column case.
857  single_column_part->SetColumnGoodness(cb);
858  return new ColPartitionSet(single_column_part);
859  }
860  return nullptr;
861 }
BlobRegionType
Definition: blobbox.h:73
BlobTextFlowType
Definition: blobbox.h:115
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936

◆ MergePart()

bool tesseract::ColPartitionGrid::MergePart ( TessResultCallback2< bool, ColPartition *, TBOX *> *  box_cb,
TessResultCallback2< bool, const ColPartition *, const ColPartition *> *  confirm_cb,
ColPartition part 
)

Definition at line 123 of file colpartitiongrid.cpp.

127  {
128  if (part->IsUnMergeableType())
129  return false;
130  bool any_done = false;
131  // Repeatedly merge part while we find a best merge candidate that works.
132  bool merge_done = false;
133  do {
134  merge_done = false;
135  TBOX box = part->bounding_box();
136  bool debug = AlignedBlob::WithinTestRegion(2, box.left(), box.bottom());
137  if (debug) {
138  tprintf("Merge candidate:");
139  box.print();
140  }
141  // Set up a rectangle search bounded by the part.
142  if (!box_cb->Run(part, &box))
143  continue;
144  // Create a list of merge candidates.
145  ColPartition_CLIST merge_candidates;
146  FindMergeCandidates(part, box, debug, &merge_candidates);
147  // Find the best merge candidate based on minimal overlap increase.
148  int overlap_increase;
149  ColPartition* neighbour = BestMergeCandidate(part, &merge_candidates, debug,
150  confirm_cb,
151  &overlap_increase);
152  if (neighbour != nullptr && overlap_increase <= 0) {
153  if (debug) {
154  tprintf("Merging:hoverlap=%d, voverlap=%d, OLI=%d\n",
155  part->HCoreOverlap(*neighbour), part->VCoreOverlap(*neighbour),
156  overlap_increase);
157  }
158  // Looks like a good candidate so merge it.
159  RemoveBBox(neighbour);
160  // We will modify the box of part, so remove it from the grid, merge
161  // it and then re-insert it into the grid.
162  RemoveBBox(part);
163  part->Absorb(neighbour, nullptr);
164  InsertBBox(true, true, part);
165  merge_done = true;
166  any_done = true;
167  } else if (neighbour != nullptr) {
168  if (debug) {
169  tprintf("Overlapped when merged with increase %d: ", overlap_increase);
170  neighbour->bounding_box().print();
171  }
172  } else if (debug) {
173  tprintf("No candidate neighbour returned\n");
174  }
175  } while (merge_done);
176  return any_done;
177 }
void print() const
Definition: rect.h:278
Definition: rect.h:34
static bool WithinTestRegion(int detail_level, int x, int y)
int16_t left() const
Definition: rect.h:72
virtual R Run(A1, A2)=0
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:488
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
ColPartition * BestMergeCandidate(const ColPartition *part, ColPartition_CLIST *candidates, bool debug, TessResultCallback2< bool, const ColPartition *, const ColPartition *> *confirm_cb, int *overlap_increase)
int16_t bottom() const
Definition: rect.h:65

◆ Merges()

void tesseract::ColPartitionGrid::Merges ( TessResultCallback2< bool, ColPartition *, TBOX *> *  box_cb,
TessResultCallback2< bool, const ColPartition *, const ColPartition *> *  confirm_cb 
)

Definition at line 102 of file colpartitiongrid.cpp.

105  {
106  // Iterate the ColPartitions in the grid.
107  ColPartitionGridSearch gsearch(this);
108  gsearch.StartFullSearch();
109  ColPartition* part;
110  while ((part = gsearch.NextFullSearch()) != nullptr) {
111  if (MergePart(box_cb, confirm_cb, part))
112  gsearch.RepositionIterator();
113  }
114  delete box_cb;
115  delete confirm_cb;
116 }
bool MergePart(TessResultCallback2< bool, ColPartition *, TBOX *> *box_cb, TessResultCallback2< bool, const ColPartition *, const ColPartition *> *confirm_cb, ColPartition *part)
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936

◆ RecomputeBounds()

void tesseract::ColPartitionGrid::RecomputeBounds ( int  gridsize,
const ICOORD bleft,
const ICOORD tright,
const ICOORD vertical 
)

Definition at line 938 of file colpartitiongrid.cpp.

941  {
942  ColPartition_LIST saved_parts;
943  ColPartition_IT part_it(&saved_parts);
944  // Iterate the ColPartitions in the grid to get parts onto a list.
945  ColPartitionGridSearch gsearch(this);
946  gsearch.StartFullSearch();
947  ColPartition* part;
948  while ((part = gsearch.NextFullSearch()) != nullptr) {
949  part_it.add_to_end(part);
950  }
951  // Reinitialize grid to the new size.
953  // Recompute the bounds of the parts and put them back in the new grid.
954  for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
955  part = part_it.extract();
956  part->set_vertical(vertical);
957  part->ComputeLimits();
958  InsertBBox(true, true, part);
959  }
960 }
int gridsize() const
Definition: bbgrid.h:64
const ICOORD & bleft() const
Definition: bbgrid.h:73
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:488
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.h:447
const ICOORD & tright() const
Definition: bbgrid.h:76

◆ RefinePartitionPartners()

void tesseract::ColPartitionGrid::RefinePartitionPartners ( bool  get_desperate)

Definition at line 1294 of file colpartitiongrid.cpp.

1294  {
1295  ColPartitionGridSearch gsearch(this);
1296  // Refine in type order so that chasing multiple partners can be done
1297  // before eliminating type mis-matching partners.
1298  for (int type = PT_UNKNOWN + 1; type <= PT_COUNT; type++) {
1299  // Iterate the ColPartitions in the grid.
1300  gsearch.StartFullSearch();
1301  ColPartition* part;
1302  while ((part = gsearch.NextFullSearch()) != nullptr) {
1303  part->RefinePartners(static_cast<PolyBlockType>(type),
1304  get_desperate, this);
1305  // Iterator may have been messed up by a merge.
1306  gsearch.RepositionIterator();
1307  }
1308  }
1309 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
Definition: capi.h:101

◆ ReflectInYAxis()

void tesseract::ColPartitionGrid::ReflectInYAxis ( )

Definition at line 647 of file colpartitiongrid.cpp.

647  {
648  ColPartition_LIST parts;
649  ColPartition_IT part_it(&parts);
650  // Iterate the ColPartitions in the grid to extract them.
651  ColPartitionGridSearch gsearch(this);
652  gsearch.StartFullSearch();
653  ColPartition* part;
654  while ((part = gsearch.NextFullSearch()) != nullptr) {
655  part_it.add_after_then_move(part);
656  }
657  ICOORD bot_left(-tright().x(), bleft().y());
658  ICOORD top_right(-bleft().x(), tright().y());
659  // Reinitializing the grid with reflected coords also clears all the
660  // pointers, so parts will now own the ColPartitions. (Briefly).
661  Init(gridsize(), bot_left, top_right);
662  for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
663  part = part_it.extract();
664  part->ReflectInYAxis();
665  InsertBBox(true, true, part);
666  }
667 }
int gridsize() const
Definition: bbgrid.h:64
const ICOORD & bleft() const
Definition: bbgrid.h:73
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
integer coordinate
Definition: points.h:32
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:488
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.h:447
const ICOORD & tright() const
Definition: bbgrid.h:76

◆ ReTypeBlobs()

void tesseract::ColPartitionGrid::ReTypeBlobs ( BLOBNBOX_LIST *  im_blobs)

Definition at line 877 of file colpartitiongrid.cpp.

877  {
878  BLOBNBOX_IT im_blob_it(im_blobs);
879  ColPartition_LIST dead_parts;
880  ColPartition_IT dead_part_it(&dead_parts);
881  // Iterate the ColPartitions in the grid.
882  ColPartitionGridSearch gsearch(this);
883  gsearch.StartFullSearch();
884  ColPartition* part;
885  while ((part = gsearch.NextFullSearch()) != nullptr) {
886  BlobRegionType blob_type = part->blob_type();
887  BlobTextFlowType flow = part->flow();
888  bool any_blobs_moved = false;
889  if (blob_type == BRT_POLYIMAGE || blob_type == BRT_RECTIMAGE) {
890  BLOBNBOX_C_IT blob_it(part->boxes());
891  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
892  BLOBNBOX* blob = blob_it.data();
893  im_blob_it.add_after_then_move(blob);
894  }
895  } else if (blob_type != BRT_NOISE) {
896  // Make sure the blobs are marked with the correct type and flow.
897  BLOBNBOX_C_IT blob_it(part->boxes());
898  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
899  BLOBNBOX* blob = blob_it.data();
900  if (blob->region_type() == BRT_NOISE) {
901  // TODO(rays) Deprecated. Change this section to an assert to verify
902  // and then delete.
903  ASSERT_HOST(blob->cblob()->area() != 0);
904  blob->set_owner(nullptr);
905  blob_it.extract();
906  any_blobs_moved = true;
907  } else {
908  blob->set_region_type(blob_type);
909  if (blob->flow() != BTFT_LEADER)
910  blob->set_flow(flow);
911  }
912  }
913  }
914  if (blob_type == BRT_NOISE || part->boxes()->empty()) {
915  BLOBNBOX_C_IT blob_it(part->boxes());
916  part->DisownBoxes();
917  dead_part_it.add_to_end(part);
918  gsearch.RemoveBBox();
919  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
920  BLOBNBOX* blob = blob_it.data();
921  if (blob->cblob()->area() == 0) {
922  // Any blob with zero area is a fake image blob and should be deleted.
923  delete blob->cblob();
924  delete blob;
925  }
926  }
927  } else if (any_blobs_moved) {
928  gsearch.RemoveBBox();
929  part->ComputeLimits();
930  InsertBBox(true, true, part);
931  gsearch.RepositionIterator();
932  }
933  }
934 }
BlobRegionType
Definition: blobbox.h:73
BlobTextFlowType flow() const
Definition: blobbox.h:296
BlobTextFlowType
Definition: blobbox.h:115
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
void set_region_type(BlobRegionType new_type)
Definition: blobbox.h:287
void set_owner(tesseract::ColPartition *new_owner)
Definition: blobbox.h:356
BlobRegionType region_type() const
Definition: blobbox.h:284
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:488
void set_flow(BlobTextFlowType value)
Definition: blobbox.h:299
int32_t area()
Definition: stepblob.cpp:275
C_BLOB * cblob() const
Definition: blobbox.h:269
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ SetTabStops()

void tesseract::ColPartitionGrid::SetTabStops ( TabFind tabgrid)

Definition at line 760 of file colpartitiongrid.cpp.

760  {
761  // Iterate the ColPartitions in the grid.
762  ColPartitionGridSearch gsearch(this);
763  gsearch.StartFullSearch();
764  ColPartition* part;
765  while ((part = gsearch.NextFullSearch()) != nullptr) {
766  const TBOX& part_box = part->bounding_box();
767  TabVector* left_line = tabgrid->LeftTabForBox(part_box, true, false);
768  // If the overlapping line is not a left tab, try for non-overlapping.
769  if (left_line != nullptr && !left_line->IsLeftTab())
770  left_line = tabgrid->LeftTabForBox(part_box, false, false);
771  if (left_line != nullptr && left_line->IsLeftTab())
772  part->SetLeftTab(left_line);
773  TabVector* right_line = tabgrid->RightTabForBox(part_box, true, false);
774  if (right_line != nullptr && !right_line->IsRightTab())
775  right_line = tabgrid->RightTabForBox(part_box, false, false);
776  if (right_line != nullptr && right_line->IsRightTab())
777  part->SetRightTab(right_line);
778  part->SetColumnGoodness(tabgrid->WidthCB());
779  }
780 }
Definition: rect.h:34
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936

◆ SplitOverlappingPartitions()

void tesseract::ColPartitionGrid::SplitOverlappingPartitions ( ColPartition_LIST *  big_parts)

Definition at line 515 of file colpartitiongrid.cpp.

516  {
517  int ok_overlap =
518  static_cast<int>(kTinyEnoughTextlineOverlapFraction * gridsize() + 0.5);
519  // Iterate the ColPartitions in the grid.
520  ColPartitionGridSearch gsearch(this);
521  gsearch.StartFullSearch();
522  ColPartition* part;
523  while ((part = gsearch.NextFullSearch()) != nullptr) {
524  // Set up a rectangle search bounded by the part.
525  const TBOX& box = part->bounding_box();
526  ColPartitionGridSearch rsearch(this);
527  rsearch.SetUniqueMode(true);
528  rsearch.StartRectSearch(box);
529  int unresolved_overlaps = 0;
530 
531  ColPartition* neighbour;
532  while ((neighbour = rsearch.NextRectSearch()) != nullptr) {
533  if (neighbour == part)
534  continue;
535  const TBOX& neighbour_box = neighbour->bounding_box();
536  if (neighbour->OKMergeOverlap(*part, *part, ok_overlap, false) &&
537  part->OKMergeOverlap(*neighbour, *neighbour, ok_overlap, false))
538  continue; // The overlap is OK both ways.
539 
540  // If removal of the biggest box from either partition eliminates the
541  // overlap, and it is much bigger than the box left behind, then
542  // it is either a drop-cap, an inter-line join, or some junk that
543  // we don't want anyway, so put it in the big_parts list.
544  if (!part->IsSingleton()) {
545  BLOBNBOX* excluded = part->BiggestBox();
546  TBOX shrunken = part->BoundsWithoutBox(excluded);
547  if (!shrunken.overlap(neighbour_box) &&
548  excluded->bounding_box().height() >
549  kBigPartSizeRatio * shrunken.height()) {
550  // Removing the biggest box fixes the overlap, so do it!
551  gsearch.RemoveBBox();
552  RemoveBadBox(excluded, part, big_parts);
553  InsertBBox(true, true, part);
554  gsearch.RepositionIterator();
555  break;
556  }
557  } else if (box.contains(neighbour_box)) {
558  ++unresolved_overlaps;
559  continue; // No amount of splitting will fix it.
560  }
561  if (!neighbour->IsSingleton()) {
562  BLOBNBOX* excluded = neighbour->BiggestBox();
563  TBOX shrunken = neighbour->BoundsWithoutBox(excluded);
564  if (!shrunken.overlap(box) &&
565  excluded->bounding_box().height() >
566  kBigPartSizeRatio * shrunken.height()) {
567  // Removing the biggest box fixes the overlap, so do it!
568  rsearch.RemoveBBox();
569  RemoveBadBox(excluded, neighbour, big_parts);
570  InsertBBox(true, true, neighbour);
571  gsearch.RepositionIterator();
572  break;
573  }
574  }
575  int part_overlap_count = part->CountOverlappingBoxes(neighbour_box);
576  int neighbour_overlap_count = neighbour->CountOverlappingBoxes(box);
577  ColPartition* right_part = nullptr;
578  if (neighbour_overlap_count <= part_overlap_count ||
579  part->IsSingleton()) {
580  // Try to split the neighbour to reduce overlap.
581  BLOBNBOX* split_blob = neighbour->OverlapSplitBlob(box);
582  if (split_blob != nullptr) {
583  rsearch.RemoveBBox();
584  right_part = neighbour->SplitAtBlob(split_blob);
585  InsertBBox(true, true, neighbour);
586  ASSERT_HOST(right_part != nullptr);
587  }
588  } else {
589  // Try to split part to reduce overlap.
590  BLOBNBOX* split_blob = part->OverlapSplitBlob(neighbour_box);
591  if (split_blob != nullptr) {
592  gsearch.RemoveBBox();
593  right_part = part->SplitAtBlob(split_blob);
594  InsertBBox(true, true, part);
595  ASSERT_HOST(right_part != nullptr);
596  }
597  }
598  if (right_part != nullptr) {
599  InsertBBox(true, true, right_part);
600  gsearch.RepositionIterator();
601  rsearch.RepositionIterator();
602  break;
603  }
604  }
605  if (unresolved_overlaps > 2 && part->IsSingleton()) {
606  // This part is no good so just add to big_parts.
607  RemoveBBox(part);
608  ColPartition_IT big_it(big_parts);
609  part->set_block_owned(true);
610  big_it.add_to_end(part);
611  gsearch.RepositionIterator();
612  }
613  }
614 }
int gridsize() const
Definition: bbgrid.h:64
const double kBigPartSizeRatio
Definition: rect.h:34
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:936
const double kTinyEnoughTextlineOverlapFraction
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:488
bool overlap(const TBOX &box) const
Definition: rect.h:355
bool contains(const FCOORD pt) const
Definition: rect.h:333
const TBOX & bounding_box() const
Definition: blobbox.h:231
int16_t height() const
Definition: rect.h:108
#define ASSERT_HOST(x)
Definition: errcode.h:84

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