All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Go to the documentation of this file.
1 // File: colpartitionrid.h
3 // Description: Class collecting code that acts on a BBGrid of ColPartitions.
4 // Author: Ray Smith
5 // Created: Mon Oct 05 08:42:01 PDT 2009
6 //
7 // (C) Copyright 2009, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
23 #include "bbgrid.h"
24 #include "colpartition.h"
25 #include "colpartitionset.h"
27 namespace tesseract {
29 class TabFind;
31 // ColPartitionGrid is a BBGrid of ColPartition.
32 // It collects functions that work on the grid.
33 class ColPartitionGrid : public BBGrid<ColPartition,
34  ColPartition_CLIST,
35  ColPartition_C_IT> {
36  public:
38  ColPartitionGrid(int gridsize, const ICOORD& bleft, const ICOORD& tright);
40  virtual ~ColPartitionGrid();
42  // Handles a click event in a display window.
43  void HandleClick(int x, int y);
45  // Merges ColPartitions in the grid that look like they belong in the same
46  // textline.
47  // For all partitions in the grid, calls the box_cb permanent callback
48  // to compute the search box, seaches the box, and if a candidate is found,
49  // calls the confirm_cb to check any more rules. If the confirm_cb returns
50  // true, then the partitions are merged.
51  // Both callbacks are deleted before returning.
53  TessResultCallback2<bool, const ColPartition*,
54  const ColPartition*>* confirm_cb);
56  // For the given partition, calls the box_cb permanent callback
57  // to compute the search box, searches the box, and if a candidate is found,
58  // calls the confirm_cb to check any more rules. If the confirm_cb returns
59  // true, then the partitions are merged.
60  // Returns true if the partition is consumed by one or more merges.
62  TessResultCallback2<bool, const ColPartition*,
63  const ColPartition*>* confirm_cb,
64  ColPartition* part);
66  // Computes and returns the total overlap of all partitions in the grid.
67  // If overlap_grid is non-null, it is filled with a grid that holds empty
68  // partitions representing the union of all overlapped partitions.
69  int ComputeTotalOverlap(ColPartitionGrid** overlap_grid);
71  // Finds all the ColPartitions in the grid that overlap with the given
72  // box and returns them SortByBoxLeft(ed) and uniqued in the given list.
73  // Any partition equal to not_this (may be NULL) is excluded.
74  void FindOverlappingPartitions(const TBOX& box, const ColPartition* not_this,
75  ColPartition_CLIST* parts);
77  // Finds and returns the best candidate ColPartition to merge with part,
78  // selected from the candidates list, based on the minimum increase in
79  // pairwise overlap among all the partitions overlapped by the combined box.
80  // If overlap_increase is not NULL then it returns the increase in overlap
81  // that would result from the merge.
82  // See colpartitiongrid.cpp for a diagram.
84  const ColPartition* part, ColPartition_CLIST* candidates, bool debug,
85  TessResultCallback2<bool, const ColPartition*,
86  const ColPartition*>* confirm_cb,
87  int* overlap_increase);
89  // Split partitions where it reduces overlap between their bounding boxes.
90  // ColPartitions are after all supposed to be a partitioning of the blobs
91  // AND of the space on the page!
92  // Blobs that cause overlaps get removed, put in individual partitions
93  // and added to the big_parts list. They are most likely characters on
94  // 2 textlines that touch, or something big like a dropcap.
95  void SplitOverlappingPartitions(ColPartition_LIST* big_parts);
97  // Filters partitions of source_type by looking at local neighbours.
98  // Where a majority of neighbours have a text type, the partitions are
99  // changed to text, where the neighbours have image type, they are changed
100  // to image, and partitions that have no definite neighbourhood type are
101  // left unchanged.
102  // im_box and rerotation are used to map blob coordinates onto the
103  // nontext_map, which is used to prevent the spread of text neighbourhoods
104  // into images.
105  // Returns true if anything was changed.
106  bool GridSmoothNeighbours(BlobTextFlowType source_type, Pix* nontext_map,
107  const TBOX& im_box, const FCOORD& rerotation);
109  // Compute the mean RGB of the light and dark pixels in each ColPartition
110  // and also the rms error in the linearity of color.
111  void ComputePartitionColors(Pix* scaled_color, int scaled_factor,
112  const FCOORD& rerotation);
114  // Reflects the grid and its colpartitions in the y-axis, assuming that
115  // all blob boxes have already been done.
116  void ReflectInYAxis();
118  // Rotates the grid and its colpartitions by the given angle, assuming that
119  // all blob boxes have already been done.
120  void Deskew(const FCOORD& deskew);
122  // Transforms the grid of partitions to the output blocks, putting each
123  // partition into a separate block. We don't really care about the order,
124  // as we just want to get as much text as possible without trying to organize
125  // it into proper blocks or columns.
126  void ExtractPartitionsAsBlocks(BLOCK_LIST* blocks, TO_BLOCK_LIST* to_blocks);
128  // Sets the left and right tabs of the partitions in the grid.
129  void SetTabStops(TabFind* tabgrid);
131  // Makes the ColPartSets and puts them in the PartSetVector ready
132  // for finding column bounds. Returns false if no partitions were found.
133  // Each ColPartition in the grid is placed in a single ColPartSet based
134  // on the bottom-left of its bounding box.
135  bool MakeColPartSets(PartSetVector* part_sets);
137  // Makes a single ColPartitionSet consisting of a single ColPartition that
138  // represents the total horizontal extent of the significant content on the
139  // page. Used for the single column setting in place of automatic detection.
140  // Returns NULL if the page is empty of significant content.
143  // Mark the BLOBNBOXes in each partition as being owned by that partition.
144  void ClaimBoxes();
146  // Retypes all the blobs referenced by the partitions in the grid.
147  // Image blobs are sliced on the grid boundaries to give the tab finder
148  // a better handle on the edges of the images, and the actual blobs are
149  // returned in the im_blobs list, as they are not owned by the block.
150  void ReTypeBlobs(BLOBNBOX_LIST* im_blobs);
152  // The boxes within the partitions have changed (by deskew) so recompute
153  // the bounds of all the partitions and reinsert them into the grid.
154  void RecomputeBounds(int gridsize, const ICOORD& bleft,
155  const ICOORD& tright, const ICOORD& vertical);
157  // Improves the margins of the ColPartitions in the grid by calling
158  // FindPartitionMargins on each.
159  void GridFindMargins(ColPartitionSet** best_columns);
161  // Improves the margins of the ColPartitions in the list by calling
162  // FindPartitionMargins on each.
163  void ListFindMargins(ColPartitionSet** best_columns,
164  ColPartition_LIST* parts);
166  // Deletes all the partitions in the grid after disowning all the blobs.
167  void DeleteParts();
169  // Deletes all the partitions in the grid that are of type BRT_UNKNOWN and
170  // all the blobs in them.
171  void DeleteUnknownParts(TO_BLOCK* block);
173  // Deletes all the partitions in the grid that are NOT of flow type
174  // BTFT_LEADER.
175  void DeleteNonLeaderParts();
177  // Finds and marks text partitions that represent figure captions.
178  void FindFigureCaptions();
182  // For every ColPartition in the grid, finds its upper and lower neighbours.
183  void FindPartitionPartners();
184  // Finds the best partner in the given direction for the given partition.
185  // Stores the result with AddPartner.
186  void FindPartitionPartners(bool upper, ColPartition* part);
187  // Finds the best partner in the given direction for the given partition.
188  // Stores the result with AddPartner.
189  void FindVPartitionPartners(bool to_the_left, ColPartition* part);
190  // For every ColPartition with multiple partners in the grid, reduces the
191  // number of partners to 0 or 1. If get_desperate is true, goes to more
192  // desperate merge methods to merge flowing text before breaking partnerships.
193  void RefinePartitionPartners(bool get_desperate);
195  private:
196  // Finds and returns a list of candidate ColPartitions to merge with part.
197  // The candidates must overlap search_box, and when merged must not
198  // overlap any other partitions that are not overlapped by each individually.
199  void FindMergeCandidates(const ColPartition* part, const TBOX& search_box,
200  bool debug, ColPartition_CLIST* candidates);
202  // Smoothes the region type/flow type of the given part by looking at local
203  // neigbours and the given image mask. Searches a padded rectangle with the
204  // padding truncated on one size of the part's box in turn for each side,
205  // using the result (if any) that has the least distance to all neighbours
206  // that contribute to the decision. This biases in favor of rectangular
207  // regions without completely enforcing them.
208  // If a good decision cannot be reached, the part is left unchanged.
209  // im_box and rerotation are used to map blob coordinates onto the
210  // nontext_map, which is used to prevent the spread of text neighbourhoods
211  // into images.
212  // Returns true if the partition was changed.
213  bool SmoothRegionType(Pix* nontext_map,
214  const TBOX& im_box,
215  const FCOORD& rerotation,
216  bool debug,
217  ColPartition* part);
218  // Executes the search for SmoothRegionType in a single direction.
219  // Creates a bounding box that is padded in all directions except direction,
220  // and searches it for other partitions. Finds the nearest collection of
221  // partitions that makes a decisive result (if any) and returns the type
222  // and the distance of the collection. If there are any pixels in the
223  // nontext_map, then the decision is biased towards image.
224  BlobRegionType SmoothInOneDirection(BlobNeighbourDir direction,
225  Pix* nontext_map,
226  const TBOX& im_box,
227  const FCOORD& rerotation,
228  bool debug,
229  const ColPartition& part,
230  int* best_distance);
231  // Counts the partitions in the given search_box by appending the gap
232  // distance (scaled by dist_scaling) of the part from the base_part to the
233  // vector of the appropriate type for the partition. Prior to return, the
234  // vectors in the dists array are sorted in increasing order.
235  // dists must be an array of GenericVectors of size NPT_COUNT.
236  void AccumulatePartDistances(const ColPartition& base_part,
237  const ICOORD& dist_scaling,
238  const TBOX& search_box,
239  Pix* nontext_map,
240  const TBOX& im_box,
241  const FCOORD& rerotation,
242  bool debug,
243  GenericVector<int>* dists);
245  // Improves the margins of the ColPartition by searching for
246  // neighbours that vertically overlap significantly.
247  void FindPartitionMargins(ColPartitionSet* columns, ColPartition* part);
249  // Starting at x, and going in the specified direction, upto x_limit, finds
250  // the margin for the given y range by searching sideways,
251  // and ignoring not_this.
252  int FindMargin(int x, bool right_to_left, int x_limit,
253  int y_bottom, int y_top, const ColPartition* not_this);
254 };
256 } // namespace tesseract.
Definition: blobbox.h:57
Definition: blobbox.h:99
void ExtractPartitionsAsBlocks(BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
bool GridSmoothNeighbours(BlobTextFlowType source_type, Pix *nontext_map, const TBOX &im_box, const FCOORD &rerotation)
void FindVPartitionPartners(bool to_the_left, ColPartition *part)
void SetTabStops(TabFind *tabgrid)
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
void FindOverlappingPartitions(const TBOX &box, const ColPartition *not_this, ColPartition_CLIST *parts)
void GridFindMargins(ColPartitionSet **best_columns)
void ComputePartitionColors(Pix *scaled_color, int scaled_factor, const FCOORD &rerotation)
Definition: blobbox.h:72
void SplitOverlappingPartitions(ColPartition_LIST *big_parts)
void DeleteUnknownParts(TO_BLOCK *block)
int gridsize() const
Definition: bbgrid.h:63
void Deskew(const FCOORD &deskew)
void HandleClick(int x, int y)
void RecomputeBounds(int gridsize, const ICOORD &bleft, const ICOORD &tright, const ICOORD &vertical)
const ICOORD & bleft() const
Definition: bbgrid.h:72
int ComputeTotalOverlap(ColPartitionGrid **overlap_grid)
void ReTypeBlobs(BLOBNBOX_LIST *im_blobs)
integer coordinate
Definition: points.h:30
ColPartitionSet * MakeSingleColumnSet(WidthCallback *cb)
void Merges(TessResultCallback2< bool, ColPartition *, TBOX * > *box_cb, TessResultCallback2< bool, const ColPartition *, const ColPartition * > *confirm_cb)
void ListFindMargins(ColPartitionSet **best_columns, ColPartition_LIST *parts)
Definition: rect.h:30
const ICOORD & tright() const
Definition: bbgrid.h:75
ColPartition * BestMergeCandidate(const ColPartition *part, ColPartition_CLIST *candidates, bool debug, TessResultCallback2< bool, const ColPartition *, const ColPartition * > *confirm_cb, int *overlap_increase)
bool MergePart(TessResultCallback2< bool, ColPartition *, TBOX * > *box_cb, TessResultCallback2< bool, const ColPartition *, const ColPartition * > *confirm_cb, ColPartition *part)
bool MakeColPartSets(PartSetVector *part_sets)
Definition: points.h:189
void RefinePartitionPartners(bool get_desperate)