tesseract  4.1.1
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)
 
 ~ColPartitionGrid () override=default
 
void HandleClick (int x, int y) override
 
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)
 
 ~BBGrid () override
 
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 66 of file colpartitiongrid.cpp.

68  : BBGrid<ColPartition, ColPartition_CLIST, ColPartition_C_IT>(gridsize,
69  bleft, tright) {
70 }
int gridsize() const
Definition: bbgrid.h:63
const ICOORD & tright() const
Definition: bbgrid.h:75
const ICOORD & bleft() const
Definition: bbgrid.h:72

◆ ~ColPartitionGrid()

tesseract::ColPartitionGrid::~ColPartitionGrid ( )
overridedefault

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 403 of file colpartitiongrid.cpp.

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

◆ ClaimBoxes()

void tesseract::ColPartitionGrid::ClaimBoxes ( )

Definition at line 861 of file colpartitiongrid.cpp.

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

◆ ComputeTotalOverlap()

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

Definition at line 316 of file colpartitiongrid.cpp.

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

◆ DeleteNonLeaderParts()

void tesseract::ColPartitionGrid::DeleteNonLeaderParts ( )

Definition at line 1040 of file colpartitiongrid.cpp.

1040  {
1041  ColPartitionGridSearch gsearch(this);
1042  gsearch.StartFullSearch();
1043  ColPartition* part;
1044  while ((part = gsearch.NextFullSearch()) != nullptr) {
1045  if (part->flow() != BTFT_LEADER) {
1046  gsearch.RemoveBBox();
1047  if (part->ReleaseNonLeaderBoxes()) {
1048  InsertBBox(true, true, part);
1049  gsearch.RepositionIterator();
1050  } else {
1051  delete part;
1052  }
1053  }
1054  }
1055 }
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:486

◆ DeleteParts()

void tesseract::ColPartitionGrid::DeleteParts ( )

Definition at line 1006 of file colpartitiongrid.cpp.

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

◆ DeleteUnknownParts()

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

Definition at line 1021 of file colpartitiongrid.cpp.

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

◆ Deskew()

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

Definition at line 733 of file colpartitiongrid.cpp.

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

◆ ExtractPartitionsAsBlocks()

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

Definition at line 672 of file colpartitiongrid.cpp.

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

◆ FindFigureCaptions()

void tesseract::ColPartitionGrid::FindFigureCaptions ( )

Definition at line 1058 of file colpartitiongrid.cpp.

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

◆ FindOverlappingPartitions()

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

Definition at line 350 of file colpartitiongrid.cpp.

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

◆ FindPartitionPartners() [1/2]

void tesseract::ColPartitionGrid::FindPartitionPartners ( )

Definition at line 1183 of file colpartitiongrid.cpp.

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

◆ FindPartitionPartners() [2/2]

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

Definition at line 1200 of file colpartitiongrid.cpp.

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

◆ FindVPartitionPartners()

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

Definition at line 1245 of file colpartitiongrid.cpp.

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

◆ GridFindMargins()

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

Definition at line 964 of file colpartitiongrid.cpp.

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

◆ GridSmoothNeighbours()

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

Definition at line 622 of file colpartitiongrid.cpp.

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

◆ HandleClick()

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

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

Definition at line 73 of file colpartitiongrid.cpp.

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

◆ ListFindMargins()

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

Definition at line 988 of file colpartitiongrid.cpp.

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

◆ MakeColPartSets()

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

Definition at line 781 of file colpartitiongrid.cpp.

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

◆ MakeSingleColumnSet()

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

Definition at line 819 of file colpartitiongrid.cpp.

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

◆ MergePart()

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

Definition at line 120 of file colpartitiongrid.cpp.

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

◆ Merges()

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

Definition at line 99 of file colpartitiongrid.cpp.

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

◆ RecomputeBounds()

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

Definition at line 935 of file colpartitiongrid.cpp.

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

◆ RefinePartitionPartners()

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

Definition at line 1291 of file colpartitiongrid.cpp.

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

◆ ReflectInYAxis()

void tesseract::ColPartitionGrid::ReflectInYAxis ( )

Definition at line 644 of file colpartitiongrid.cpp.

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

◆ ReTypeBlobs()

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

Definition at line 874 of file colpartitiongrid.cpp.

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

◆ SetTabStops()

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

Definition at line 757 of file colpartitiongrid.cpp.

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

◆ SplitOverlappingPartitions()

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

Definition at line 512 of file colpartitiongrid.cpp.

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

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