Tesseract 3.01
/data/source/tesseract-ocr/textord/colpartitionset.h
Go to the documentation of this file.
00001 
00002 // File:        colpartitionset.h
00003 // Description: Class to hold a list of ColPartitions of the page that
00004 //              correspond roughly to columns.
00005 // Author:      Ray Smith
00006 // Created:     Thu Aug 14 10:50:01 PDT 2008
00007 //
00008 // (C) Copyright 2008, Google Inc.
00009 // Licensed under the Apache License, Version 2.0 (the "License");
00010 // you may not use this file except in compliance with the License.
00011 // You may obtain a copy of the License at
00012 // http://www.apache.org/licenses/LICENSE-2.0
00013 // Unless required by applicable law or agreed to in writing, software
00014 // distributed under the License is distributed on an "AS IS" BASIS,
00015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016 // See the License for the specific language governing permissions and
00017 // limitations under the License.
00018 //
00020 
00021 #ifndef TESSERACT_TEXTORD_COLPARTITIONSET_H__
00022 #define TESSERACT_TEXTORD_COLPARTITIONSET_H__
00023 
00024 #include "colpartition.h"   // For ColPartition_LIST.
00025 #include "genericvector.h"  // For GenericVector.
00026 #include "rect.h"           // For TBOX.
00027 #include "tabvector.h"      // For BLOBNBOX_CLIST.
00028 
00029 namespace tesseract {
00030 
00031 class WorkingPartSet_LIST;
00032 class ColSegment_LIST;
00033 class ColPartitionSet;
00034 typedef GenericVector<ColPartitionSet*> PartSetVector;
00035 
00036 // ColPartitionSet is a class that holds a list of ColPartitions.
00037 // Its main use is in holding a candidate partitioning of the width of the
00038 // image into columns, where each member ColPartition is a single column.
00039 // ColPartitionSets are used in building the column layout of a page.
00040 class ColPartitionSet : public ELIST_LINK {
00041  public:
00042   ColPartitionSet() {
00043   }
00044   explicit ColPartitionSet(ColPartition_LIST* partitions);
00045   explicit ColPartitionSet(ColPartition* partition);
00046 
00047   ~ColPartitionSet();
00048 
00049   // Simple accessors.
00050   const TBOX& bounding_box() const {
00051     return bounding_box_;
00052   }
00053   bool Empty() {
00054     return parts_.empty();
00055   }
00056   int ColumnCount() {
00057     return parts_.length();
00058   }
00059 
00060   // Return an element of the parts_ list from its index.
00061   ColPartition* GetColumnByIndex(int index);
00062 
00063   // Return the ColPartition that contains the given coords, if any, else NULL.
00064   ColPartition* ColumnContaining(int x, int y);
00065 
00066   // Return the bounding boxes of columns at the given y-range
00067   void GetColumnBoxes(int y_bottom, int y_top, ColSegment_LIST *segments);
00068 
00069   // Move the parts to the output list, giving up ownership.
00070   void ReturnParts(ColPartition_LIST* parts);
00071 
00072   // Merge any significantly overlapping partitions within the this and other,
00073   // and unique the boxes so that no two partitions use the same box.
00074   // Return true if any changes were made to either set.
00075   bool MergeOverlaps(ColPartitionSet* other, WidthCallback* cb);
00076 
00077   // Attempt to improve this by adding partitions or expanding partitions.
00078   void ImproveColumnCandidate(WidthCallback* cb, PartSetVector* src_sets);
00079 
00080   // If this set is good enough to represent a new partitioning into columns,
00081   // add it to the vector of sets, otherwise delete it.
00082   void AddToColumnSetsIfUnique(PartSetVector* column_sets, WidthCallback* cb);
00083 
00084   // Return true if the partitions in other are all compatible with the columns
00085   // in this.
00086   bool CompatibleColumns(bool debug, ColPartitionSet* other, WidthCallback* cb);
00087 
00088   // Returns the total width of all blobs in the part_set that do not lie
00089   // within an approved column. Used as a cost measure for using this
00090   // column set over another that might be compatible.
00091   int UnmatchedWidth(ColPartitionSet* part_set);
00092 
00093   // Return true if this ColPartitionSet makes a legal column candidate by
00094   // having legal individual partitions and non-overlapping adjacent pairs.
00095   bool LegalColumnCandidate();
00096 
00097   // Return a copy of this. If good_only will only copy the Good ColPartitions.
00098   ColPartitionSet* Copy(bool good_only);
00099 
00100   // Display the edges of the columns at the given y coords.
00101   void DisplayColumnEdges(int y_bottom, int y_top, ScrollView* win);
00102 
00103   // Return the ColumnSpanningType that best explains the columns overlapped
00104   // by the given coords(left,right,y), with the given margins.
00105   // Also return the first and last column index touched by the coords and
00106   // the leftmost spanned column.
00107   // Column indices are 2n + 1 for real colums (0 based) and even values
00108   // represent the gaps in between columns, with 0 being left of the leftmost.
00109   // resolution refers to the ppi resolution of the image. It may be 0 if only
00110   // the first_col and last_col are required.
00111   ColumnSpanningType SpanningType(int resolution,
00112                                   int left, int right, int y,
00113                                   int left_margin, int right_margin,
00114                                   int* first_col, int* last_col,
00115                                   int* first_spanned_col);
00116 
00117   // The column_set has changed. Close down all in-progress WorkingPartSets in
00118   // columns that do not match and start new ones for the new columns in this.
00119   // As ColPartitions are turned into BLOCKs, the used ones are put in
00120   // used_parts, as they still need to be referenced in the grid.
00121   void ChangeWorkColumns(const ICOORD& bleft, const ICOORD& tright,
00122                          int resolution, ColPartition_LIST* used_parts,
00123                          WorkingPartSet_LIST* working_set);
00124 
00125   // Accumulate the widths and gaps into the given variables.
00126   void AccumulateColumnWidthsAndGaps(int* total_width, int* width_samples,
00127                                      int* total_gap, int* gap_samples);
00128 
00129   // Provide debug output for this ColPartitionSet and all the ColPartitions.
00130   void Print();
00131 
00132  private:
00133   // Add the given partition to the list in the appropriate place.
00134   void AddPartition(ColPartition* new_part, ColPartition_IT* it);
00135 
00136   // Compute the coverage and good column count.
00137   void ComputeCoverage();
00138 
00139   // The partitions in this column candidate.
00140   ColPartition_LIST parts_;
00141   // The number of partitions that have a frequent column width.
00142   int good_column_count_;
00143   // Total width of all the ColPartitions.
00144   int total_coverage_;
00145   // Bounding box of all partitions in the set.
00146   TBOX bounding_box_;
00147 };
00148 
00149 ELISTIZEH(ColPartitionSet)
00150 
00151 }  // namespace tesseract.
00152 
00153 #endif  // TESSERACT_TEXTORD_COLPARTITION_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines