Tesseract  3.02
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   // Extract all the parts from the list, relinquishing ownership.
00070   void RelinquishParts();
00071 
00072   // Attempt to improve this by adding partitions or expanding partitions.
00073   void ImproveColumnCandidate(WidthCallback* cb, PartSetVector* src_sets);
00074 
00075   // If this set is good enough to represent a new partitioning into columns,
00076   // add it to the vector of sets, otherwise delete it.
00077   void AddToColumnSetsIfUnique(PartSetVector* column_sets, WidthCallback* cb);
00078 
00079   // Return true if the partitions in other are all compatible with the columns
00080   // in this.
00081   bool CompatibleColumns(bool debug, ColPartitionSet* other, WidthCallback* cb);
00082 
00083   // Returns the total width of all blobs in the part_set that do not lie
00084   // within an approved column. Used as a cost measure for using this
00085   // column set over another that might be compatible.
00086   int UnmatchedWidth(ColPartitionSet* part_set);
00087 
00088   // Return true if this ColPartitionSet makes a legal column candidate by
00089   // having legal individual partitions and non-overlapping adjacent pairs.
00090   bool LegalColumnCandidate();
00091 
00092   // Return a copy of this. If good_only will only copy the Good ColPartitions.
00093   ColPartitionSet* Copy(bool good_only);
00094 
00095   // Display the edges of the columns at the given y coords.
00096   void DisplayColumnEdges(int y_bottom, int y_top, ScrollView* win);
00097 
00098   // Return the ColumnSpanningType that best explains the columns overlapped
00099   // by the given coords(left,right,y), with the given margins.
00100   // Also return the first and last column index touched by the coords and
00101   // the leftmost spanned column.
00102   // Column indices are 2n + 1 for real colums (0 based) and even values
00103   // represent the gaps in between columns, with 0 being left of the leftmost.
00104   // resolution refers to the ppi resolution of the image. It may be 0 if only
00105   // the first_col and last_col are required.
00106   ColumnSpanningType SpanningType(int resolution,
00107                                   int left, int right, int y,
00108                                   int left_margin, int right_margin,
00109                                   int* first_col, int* last_col,
00110                                   int* first_spanned_col);
00111 
00112   // The column_set has changed. Close down all in-progress WorkingPartSets in
00113   // columns that do not match and start new ones for the new columns in this.
00114   // As ColPartitions are turned into BLOCKs, the used ones are put in
00115   // used_parts, as they still need to be referenced in the grid.
00116   void ChangeWorkColumns(const ICOORD& bleft, const ICOORD& tright,
00117                          int resolution, ColPartition_LIST* used_parts,
00118                          WorkingPartSet_LIST* working_set);
00119 
00120   // Accumulate the widths and gaps into the given variables.
00121   void AccumulateColumnWidthsAndGaps(int* total_width, int* width_samples,
00122                                      int* total_gap, int* gap_samples);
00123 
00124   // Provide debug output for this ColPartitionSet and all the ColPartitions.
00125   void Print();
00126 
00127  private:
00128   // Add the given partition to the list in the appropriate place.
00129   void AddPartition(ColPartition* new_part, ColPartition_IT* it);
00130 
00131   // Compute the coverage and good column count. Coverage is the amount of the
00132   // width of the page (in pixels) that is covered by ColPartitions, which are
00133   // used to provide candidate column layouts.
00134   // Coverage is split into good and bad. Good coverage is provided by
00135   // ColPartitions of a frequent width (according to the callback function
00136   // provided by TabFinder::WidthCB, which accesses stored statistics on the
00137   // widths of ColParititions) and bad coverage is provided by all other
00138   // ColPartitions, even if they have tab vectors at both sides. Thus:
00139   // |-----------------------------------------------------------------|
00140   // |        Double     width    heading                              |
00141   // |-----------------------------------------------------------------|
00142   // |-------------------------------| |-------------------------------|
00143   // |   Common width ColParition    | |  Common width ColPartition    |
00144   // |-------------------------------| |-------------------------------|
00145   // the layout with two common-width columns has better coverage than the
00146   // double width heading, because the coverage is "good," even though less in
00147   // total coverage than the heading, because the heading coverage is "bad."
00148   void ComputeCoverage();
00149 
00150   // Adds the coverage, column count and box for a single partition,
00151   // without adding it to the list. (Helper factored from ComputeCoverage.)
00152   void AddPartitionCoverageAndBox(const ColPartition& part);
00153 
00154   // The partitions in this column candidate.
00155   ColPartition_LIST parts_;
00156   // The number of partitions that have a frequent column width.
00157   int good_column_count_;
00158   // Total width of all the good ColPartitions.
00159   int good_coverage_;
00160   // Total width of all the bad ColPartitions.
00161   int bad_coverage_;
00162   // Bounding box of all partitions in the set.
00163   TBOX bounding_box_;
00164 };
00165 
00166 ELISTIZEH(ColPartitionSet)
00167 
00168 }  // namespace tesseract.
00169 
00170 #endif  // TESSERACT_TEXTORD_COLPARTITION_H__