Tesseract  3.02
tesseract-ocr/textord/tabfind.h
Go to the documentation of this file.
00001 
00002 // File:        tabfind.h
00003 // Description: Subclass of BBGrid to find tabstops.
00004 // Author:      Ray Smith
00005 // Created:     Fri Mar 21 15:03:01 PST 2008
00006 //
00007 // (C) Copyright 2008, Google Inc.
00008 // Licensed under the Apache License, Version 2.0 (the "License");
00009 // you may not use this file except in compliance with the License.
00010 // You may obtain a copy of the License at
00011 // http://www.apache.org/licenses/LICENSE-2.0
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 //
00019 
00020 #ifndef TESSERACT_TEXTORD_TABFIND_H__
00021 #define TESSERACT_TEXTORD_TABFIND_H__
00022 
00023 #include "alignedblob.h"
00024 #include "tesscallback.h"
00025 #include "tabvector.h"
00026 #include "linefind.h"
00027 
00028 extern BOOL_VAR_H(textord_tabfind_force_vertical_text, false,
00029        "Force using vertical text page mode");
00030 extern BOOL_VAR_H(textord_tabfind_vertical_horizontal_mix, true,
00031        "find horizontal lines such as headers in vertical page mode");
00032 extern double_VAR_H(textord_tabfind_vertical_text_ratio, 0.5,
00033        "Fraction of textlines deemed vertical to use vertical page mode");
00034 extern double_VAR_H(textord_tabfind_aligned_gap_fraction, 0.75,
00035        "Fraction of height used as a minimum gap for aligned blobs.");
00036 
00037 class BLOBNBOX;
00038 class BLOBNBOX_LIST;
00039 class TO_BLOCK;
00040 class ScrollView;
00041 struct Pix;
00042 
00043 namespace tesseract {
00044 
00045 typedef TessResultCallback1<bool, int> WidthCallback;
00046 
00047 struct AlignedBlobParams;
00048 class ColPartitionGrid;
00049 
00051 const int kColumnWidthFactor = 20;
00052 
00062 class TabFind : public AlignedBlob {
00063  public:
00064   TabFind(int gridsize, const ICOORD& bleft, const ICOORD& tright,
00065           TabVector_LIST* vlines, int vertical_x, int vertical_y,
00066           int resolution);
00067   virtual ~TabFind();
00068 
00077   void InsertBlobsToGrid(bool h_spread, bool v_spread,
00078                          BLOBNBOX_LIST* blobs,
00079                          BBGrid<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT>* grid);
00080 
00088   bool InsertBlob(bool h_spread, bool v_spread, BLOBNBOX* blob,
00089                   BBGrid<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT>* grid);
00090   // Calls SetBlobRuleEdges for all the blobs in the given block.
00091   void SetBlockRuleEdges(TO_BLOCK* block);
00092   // Sets the left and right rule and crossing_rules for the blobs in the given
00093   // list by finding the next outermost tabvectors for each blob.
00094   void SetBlobRuleEdges(BLOBNBOX_LIST* blobs);
00095 
00096   // Returns the gutter width of the given TabVector between the given y limits.
00097   // Also returns x-shift to be added to the vector to clear any intersecting
00098   // blobs. The shift is deducted from the returned gutter.
00099   // If ignore_unmergeables is true, then blobs of UnMergeableType are
00100   // ignored as if they don't exist. (Used for text on image.)
00101   // max_gutter_width is used as the maximum width worth searching for in case
00102   // there is nothing near the TabVector.
00103   int GutterWidth(int bottom_y, int top_y, const TabVector& v,
00104                   bool ignore_unmergeables, int max_gutter_width,
00105                   int* required_shift);
00109   void GutterWidthAndNeighbourGap(int tab_x, int mean_height,
00110                                   int max_gutter, bool left,
00111                                   BLOBNBOX* bbox, int* gutter_width,
00112                                   int* neighbour_gap);
00113 
00120   int RightEdgeForBox(const TBOX& box, bool crossing, bool extended);
00124   int LeftEdgeForBox(const TBOX& box, bool crossing, bool extended);
00125 
00142   TabVector* RightTabForBox(const TBOX& box, bool crossing, bool extended);
00146   TabVector* LeftTabForBox(const TBOX& box, bool crossing, bool extended);
00147 
00152   bool CommonWidth(int width);
00157   static bool DifferentSizes(int size1, int size2);
00162   static bool VeryDifferentSizes(int size1, int size2);
00163 
00167   WidthCallback* WidthCB() {
00168     return width_cb_;
00169   }
00170 
00174   const ICOORD& image_origin() const {
00175     return image_origin_;
00176   }
00177 
00178  protected:
00182   TabVector_LIST* vectors() {
00183     return &vectors_;
00184   }
00185   TabVector_LIST* dead_vectors() {
00186     return &dead_vectors_;
00187   }
00188 
00194   bool FindTabVectors(TabVector_LIST* hlines,
00195                       BLOBNBOX_LIST* image_blobs, TO_BLOCK* block,
00196                       int min_gutter_width,
00197                       ColPartitionGrid* part_grid,
00198                       FCOORD* deskew, FCOORD* reskew);
00199 
00200   // Top-level function to not find TabVectors in an input page block,
00201   // but setup for single column mode.
00202   void DontFindTabVectors(BLOBNBOX_LIST* image_blobs,
00203                           TO_BLOCK* block, FCOORD* deskew, FCOORD* reskew);
00204 
00205   // Cleans up the lists of blobs in the block ready for use by TabFind.
00206   // Large blobs that look like text are moved to the main blobs list.
00207   // Main blobs that are superseded by the image blobs are deleted.
00208   void TidyBlobs(TO_BLOCK* block);
00209 
00210   // Helper function to setup search limits for *TabForBox.
00211   void SetupTabSearch(int x, int y, int* min_key, int* max_key);
00212 
00216   ScrollView* DisplayTabVectors(ScrollView* tab_win);
00217 
00218   // First part of FindTabVectors, which may be used twice if the text
00219   // is mostly of vertical alignment.  If find_vertical_text flag is
00220   // true, this finds vertical textlines in possibly rotated blob space.
00221   // In other words, when the page has mostly vertical lines and is rotated,
00222   // setting this to true will find horizontal lines on the page.
00223   ScrollView* FindInitialTabVectors(BLOBNBOX_LIST* image_blobs,
00224                                     int min_gutter_width, TO_BLOCK* block);
00225 
00226   // Apply the given rotation to the given list of blobs.
00227   static void RotateBlobList(const FCOORD& rotation, BLOBNBOX_LIST* blobs);
00228 
00229   // Flip the vertical and horizontal lines and rotate the grid ready
00230   // for working on the rotated image.
00231   // The min_gutter_width will be adjusted to the median gutter width between
00232   // vertical tabs to set a better threshold for tabboxes in the 2nd pass.
00233   void ResetForVerticalText(const FCOORD& rotate, const FCOORD& rerotate,
00234                             TabVector_LIST* horizontal_lines,
00235                             int* min_gutter_width);
00236 
00237   // Clear the grid and get rid of the tab vectors, but not separators,
00238   // ready to start again.
00239   void Reset();
00240 
00241   // Reflect the separator tab vectors and the grids in the y-axis.
00242   // Can only be called after Reset!
00243   void ReflectInYAxis();
00244 
00245  private:
00246   // For each box in the grid, decide whether it is a candidate tab-stop,
00247   // and if so add it to the left and right tab boxes.
00248   ScrollView* FindTabBoxes(int min_gutter_width);
00249 
00250   // Return true if this box looks like a candidate tab stop, and set
00251   // the appropriate tab type(s) to TT_UNCONFIRMED.
00252   bool TestBoxForTabs(BLOBNBOX* bbox, int min_gutter_width);
00253 
00254   // Returns true if there is nothing in the rectangle of width min_gutter to
00255   // the left of bbox.
00256   bool ConfirmRaggedLeft(BLOBNBOX* bbox, int min_gutter);
00257   // Returns true if there is nothing in the rectangle of width min_gutter to
00258   // the right of bbox.
00259   bool ConfirmRaggedRight(BLOBNBOX* bbox, int min_gutter);
00260   // Returns true if there is nothing in the given search_box that vertically
00261   // overlaps target_box other than target_box itself.
00262   bool NothingYOverlapsInBox(const TBOX& search_box, const TBOX& target_box);
00263 
00264   // Fills the list of TabVector with the tabstops found in the grid,
00265   // and estimates the logical vertical direction.
00266   void FindAllTabVectors(int min_gutter_width);
00267   // Helper for FindAllTabVectors finds the vectors of a particular type.
00268   int FindTabVectors(int search_size_multiple,
00269                      TabAlignment alignment,
00270                      int min_gutter_width,
00271                      TabVector_LIST* vectors,
00272                      int* vertical_x, int* vertical_y);
00273   // Finds a vector corresponding to a tabstop running through the
00274   // given box of the given alignment type.
00275   // search_size_multiple is a multiple of height used to control
00276   // the size of the search.
00277   // vertical_x and y are updated with an estimate of the real
00278   // vertical direction. (skew finding.)
00279   // Returns NULL if no decent tabstop can be found.
00280   TabVector* FindTabVector(int search_size_multiple, int min_gutter_width,
00281                            TabAlignment alignment,
00282                            BLOBNBOX* bbox,
00283                            int* vertical_x, int* vertical_y);
00284 
00285   // Set the vertical_skew_ member from the given vector and refit
00286   // all vectors parallel to the skew vector.
00287   void SetVerticalSkewAndParellelize(int vertical_x, int vertical_y);
00288 
00289   // Sort all the current vectors using the vertical_skew_ vector.
00290   void SortVectors();
00291 
00292   // Evaluate all the current tab vectors.
00293   void EvaluateTabs();
00294 
00295   // Trace textlines from one side to the other of each tab vector, saving
00296   // the most frequent column widths found in a list so that a given width
00297   // can be tested for being a common width with a simple callback function.
00298   void ComputeColumnWidths(ScrollView* tab_win,
00299                            ColPartitionGrid* part_grid);
00300 
00301   // Find column width and pair-up tab vectors with existing ColPartitions.
00302   void ApplyPartitionsToColumnWidths(ColPartitionGrid* part_grid,
00303                                      STATS* col_widths);
00304 
00305   // Helper makes the list of common column widths in column_widths_ from the
00306   // input col_widths. Destroys the content of col_widths by repeatedly
00307   // finding the mode and erasing the peak.
00308   void MakeColumnWidths(int col_widths_size, STATS* col_widths);
00309 
00310   // Mark blobs as being in a vertical text line where that is the case.
00311   void MarkVerticalText();
00312 
00313   // Returns the median gutter width between pairs of matching tab vectors
00314   // assuming they are sorted left-to-right.  If there are too few data
00315   // points (< kMinLinesInColumn), then 0 is returned.
00316   int FindMedianGutterWidth(TabVector_LIST* tab_vectors);
00317 
00318   // Find the next adjacent (to left or right) blob on this text line,
00319   // with the constraint that it must vertically significantly overlap
00320   // the [top_y, bottom_y] range.
00321   // If ignore_images is true, then blobs with aligned_text() < 0 are treated
00322   // as if they do not exist.
00323   BLOBNBOX* AdjacentBlob(const BLOBNBOX* bbox,
00324                          bool look_left, bool ignore_images,
00325                          double min_overlap_fraction,
00326                          int gap_limit, int top_y, int bottom_y);
00327 
00328   // Add a bi-directional partner relationship between the left
00329   // and the right. If one (or both) of the vectors is a separator,
00330   // extend a nearby extendable vector or create a new one of the
00331   // correct type, using the given left or right blob as a guide.
00332   void AddPartnerVector(BLOBNBOX* left_blob, BLOBNBOX* right_blob,
00333                         TabVector* left, TabVector* right);
00334 
00339   void CleanupTabs();
00340 
00346   bool Deskew(TabVector_LIST* hlines, BLOBNBOX_LIST* image_blobs,
00347               TO_BLOCK* block, FCOORD* deskew, FCOORD* reskew);
00348 
00349   // Compute the rotation required to deskew, and its inverse rotation.
00350   void ComputeDeskewVectors(FCOORD* deskew, FCOORD* reskew);
00351 
00356   void ApplyTabConstraints();
00357 
00358  protected:
00359   ICOORD vertical_skew_;          //< Estimate of true vertical in this image.
00360   int resolution_;                //< Of source image in pixels per inch.
00361  private:
00362   ICOORD image_origin_;           //< Top-left of image in deskewed coords
00363   TabVector_LIST vectors_;        //< List of rule line and tabstops.
00364   TabVector_IT v_it_;             //< Iterator for searching vectors_.
00365   TabVector_LIST dead_vectors_;   //< Separators and unpartnered tab vectors.
00366   ICOORDELT_LIST column_widths_;  //< List of commonly occurring widths.
00368   WidthCallback* width_cb_;
00369   // Sets of bounding boxes that are candidate tab stops.
00370   GenericVector<BLOBNBOX*> left_tab_boxes_;
00371   GenericVector<BLOBNBOX*> right_tab_boxes_;
00372 };
00373 
00374 }  // namespace tesseract.
00375 
00376 #endif  // TESSERACT_TEXTORD_TABFIND_H__