Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
colpartitiongrid.h
Go to the documentation of this file.
1 
2 // 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 // http://www.apache.org/licenses/LICENSE-2.0
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 //
19 
20 #ifndef TESSERACT_TEXTORD_COLPARTITIONGRID_H__
21 #define TESSERACT_TEXTORD_COLPARTITIONGRID_H__
22 
23 #include "bbgrid.h"
24 #include "colpartition.h"
25 #include "colpartitionset.h"
26 
27 namespace tesseract {
28 
29 class TabFind;
30 
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);
39 
40  virtual ~ColPartitionGrid();
41 
42  // Handles a click event in a display window.
43  void HandleClick(int x, int y);
44 
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);
55 
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);
65 
66  // Finds all the ColPartitions in the grid that overlap with the given
67  // box and returns them SortByBoxLeft(ed) and uniqued in the given list.
68  // Any partition equal to not_this (may be NULL) is excluded.
69  void FindOverlappingPartitions(const TBOX& box, const ColPartition* not_this,
70  ColPartition_CLIST* parts);
71 
72  // Finds and returns the best candidate ColPartition to merge with part,
73  // selected from the candidates list, based on the minimum increase in
74  // pairwise overlap among all the partitions overlapped by the combined box.
75  // If overlap_increase is not NULL then it returns the increase in overlap
76  // that would result from the merge.
77  // See colpartitiongrid.cpp for a diagram.
79  const ColPartition* part, ColPartition_CLIST* candidates, bool debug,
80  TessResultCallback2<bool, const ColPartition*,
81  const ColPartition*>* confirm_cb,
82  int* overlap_increase);
83 
84  // Split partitions where it reduces overlap between their bounding boxes.
85  // ColPartitions are after all supposed to be a partitioning of the blobs
86  // AND of the space on the page!
87  // Blobs that cause overlaps get removed, put in individual partitions
88  // and added to the big_parts list. They are most likely characters on
89  // 2 textlines that touch, or something big like a dropcap.
90  void SplitOverlappingPartitions(ColPartition_LIST* big_parts);
91 
92  // Filters partitions of source_type by looking at local neighbours.
93  // Where a majority of neighbours have a text type, the partitions are
94  // changed to text, where the neighbours have image type, they are changed
95  // to image, and partitions that have no definite neighbourhood type are
96  // left unchanged.
97  // im_box and rerotation are used to map blob coordinates onto the
98  // nontext_map, which is used to prevent the spread of text neighbourhoods
99  // into images.
100  // Returns true if anything was changed.
101  bool GridSmoothNeighbours(BlobTextFlowType source_type, Pix* nontext_map,
102  const TBOX& im_box, const FCOORD& rerotation);
103 
104  // Compute the mean RGB of the light and dark pixels in each ColPartition
105  // and also the rms error in the linearity of color.
106  void ComputePartitionColors(Pix* scaled_color, int scaled_factor,
107  const FCOORD& rerotation);
108 
109  // Reflects the grid and its colpartitions in the y-axis, assuming that
110  // all blob boxes have already been done.
111  void ReflectInYAxis();
112 
113  // Rotates the grid and its colpartitions by the given angle, assuming that
114  // all blob boxes have already been done.
115  void Deskew(const FCOORD& deskew);
116 
117  // Sets the left and right tabs of the partitions in the grid.
118  void SetTabStops(TabFind* tabgrid);
119 
120  // Makes the ColPartSets and puts them in the PartSetVector ready
121  // for finding column bounds. Returns false if no partitions were found.
122  // Each ColPartition in the grid is placed in a single ColPartSet based
123  // on the bottom-left of its bounding box.
124  bool MakeColPartSets(PartSetVector* part_sets);
125 
126  // Makes a single ColPartitionSet consisting of a single ColPartition that
127  // represents the total horizontal extent of the significant content on the
128  // page. Used for the single column setting in place of automatic detection.
129  // Returns NULL if the page is empty of significant content.
131 
132  // Mark the BLOBNBOXes in each partition as being owned by that partition.
133  void ClaimBoxes();
134 
135  // Retypes all the blobs referenced by the partitions in the grid.
136  // Image blobs are sliced on the grid boundaries to give the tab finder
137  // a better handle on the edges of the images, and the actual blobs are
138  // returned in the im_blobs list, as they are not owned by the block.
139  void ReTypeBlobs(BLOBNBOX_LIST* im_blobs);
140 
141  // The boxes within the partitions have changed (by deskew) so recompute
142  // the bounds of all the partitions and reinsert them into the grid.
143  void RecomputeBounds(int gridsize, const ICOORD& bleft,
144  const ICOORD& tright, const ICOORD& vertical);
145 
146  // Improves the margins of the ColPartitions in the grid by calling
147  // FindPartitionMargins on each.
148  void GridFindMargins(ColPartitionSet** best_columns);
149 
150  // Improves the margins of the ColPartitions in the list by calling
151  // FindPartitionMargins on each.
152  void ListFindMargins(ColPartitionSet** best_columns,
153  ColPartition_LIST* parts);
154 
155  // Deletes all the partitions in the grid after disowning all the blobs.
156  void DeleteParts();
157 
158  // Deletes all the partitions in the grid that are of type BRT_UNKNOWN and
159  // all the blobs in them.
160  void DeleteUnknownParts(TO_BLOCK* block);
161 
162  // Finds and marks text partitions that represent figure captions.
163  void FindFigureCaptions();
164 
167  // For every ColPartition in the grid, finds its upper and lower neighbours.
168  void FindPartitionPartners();
169  // Finds the best partner in the given direction for the given partition.
170  // Stores the result with AddPartner.
171  void FindPartitionPartners(bool upper, ColPartition* part);
172  // Finds the best partner in the given direction for the given partition.
173  // Stores the result with AddPartner.
174  void FindVPartitionPartners(bool to_the_left, ColPartition* part);
175  // For every ColPartition with multiple partners in the grid, reduces the
176  // number of partners to 0 or 1. If get_desperate is true, goes to more
177  // desperate merge methods to merge flowing text before breaking partnerships.
178  void RefinePartitionPartners(bool get_desperate);
179 
180  private:
181  // Finds and returns a list of candidate ColPartitions to merge with part.
182  // The candidates must overlap search_box, and when merged must not
183  // overlap any other partitions that are not overlapped by each individually.
184  void FindMergeCandidates(const ColPartition* part, const TBOX& search_box,
185  bool debug, ColPartition_CLIST* candidates);
186 
187  // Smoothes the region type/flow type of the given part by looking at local
188  // neigbours and the given image mask. Searches a padded rectangle with the
189  // padding truncated on one size of the part's box in turn for each side,
190  // using the result (if any) that has the least distance to all neighbours
191  // that contribute to the decision. This biases in favor of rectangular
192  // regions without completely enforcing them.
193  // If a good decision cannot be reached, the part is left unchanged.
194  // im_box and rerotation are used to map blob coordinates onto the
195  // nontext_map, which is used to prevent the spread of text neighbourhoods
196  // into images.
197  // Returns true if the partition was changed.
198  bool SmoothRegionType(Pix* nontext_map,
199  const TBOX& im_box,
200  const FCOORD& rerotation,
201  bool debug,
202  ColPartition* part);
203  // Executes the search for SmoothRegionType in a single direction.
204  // Creates a bounding box that is padded in all directions except direction,
205  // and searches it for other partitions. Finds the nearest collection of
206  // partitions that makes a decisive result (if any) and returns the type
207  // and the distance of the collection. If there are any pixels in the
208  // nontext_map, then the decision is biased towards image.
209  BlobRegionType SmoothInOneDirection(BlobNeighbourDir direction,
210  Pix* nontext_map,
211  const TBOX& im_box,
212  const FCOORD& rerotation,
213  bool debug,
214  const ColPartition& part,
215  int* best_distance);
216  // Counts the partitions in the given search_box by appending the gap
217  // distance (scaled by dist_scaling) of the part from the base_part to the
218  // vector of the appropriate type for the partition. Prior to return, the
219  // vectors in the dists array are sorted in increasing order.
220  // dists must be an array of GenericVectors of size NPT_COUNT.
221  void AccumulatePartDistances(const ColPartition& base_part,
222  const ICOORD& dist_scaling,
223  const TBOX& search_box,
224  Pix* nontext_map,
225  const TBOX& im_box,
226  const FCOORD& rerotation,
227  bool debug,
228  GenericVector<int>* dists);
229 
230  // Improves the margins of the ColPartition by searching for
231  // neighbours that vertically overlap significantly.
232  void FindPartitionMargins(ColPartitionSet* columns, ColPartition* part);
233 
234  // Starting at x, and going in the specified direction, upto x_limit, finds
235  // the margin for the given y range by searching sideways,
236  // and ignoring not_this.
237  int FindMargin(int x, bool right_to_left, int x_limit,
238  int y_bottom, int y_top, const ColPartition* not_this);
239 };
240 
241 } // namespace tesseract.
242 
243 #endif // TESSERACT_TEXTORD_COLPARTITIONGRID_H__