tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/textord/strokewidth.h
Go to the documentation of this file.
00001 
00002 // File:        strokewidth.h
00003 // Description: Subclass of BBGrid to find uniformity of strokewidth.
00004 // Author:      Ray Smith
00005 // Created:     Mon Mar 31 16:17: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_STROKEWIDTH_H__
00021 #define TESSERACT_TEXTORD_STROKEWIDTH_H__
00022 
00023 #include "blobbox.h"        // BlobNeighourDir.
00024 #include "blobgrid.h"         // Base class.
00025 #include "colpartitiongrid.h"
00026 #include "textlineprojection.h"
00027 
00028 class DENORM;
00029 class ScrollView;
00030 class TO_BLOCK;
00031 
00032 namespace tesseract {
00033 
00034 class ColPartition_LIST;
00035 class TabFind;
00036 class TextlineProjection;
00037 
00038 // Misc enums to clarify bool arguments for direction-controlling args.
00039 enum LeftOrRight {
00040   LR_LEFT,
00041   LR_RIGHT
00042 };
00043 
00049 class StrokeWidth : public BlobGrid {
00050  public:
00051   StrokeWidth(int gridsize, const ICOORD& bleft, const ICOORD& tright);
00052   virtual ~StrokeWidth();
00053 
00054   // Sets the neighbours member of the medium-sized blobs in the block.
00055   // Searches on 4 sides of each blob for similar-sized, similar-strokewidth
00056   // blobs and sets pointers to the good neighbours.
00057   void SetNeighboursOnMediumBlobs(TO_BLOCK* block);
00058 
00059   // Sets the neighbour/textline writing direction members of the medium
00060   // and large blobs with optional repair of broken CJK characters first.
00061   // Repair of broken CJK is needed here because broken CJK characters
00062   // can fool the textline direction detection algorithm.
00063   void FindTextlineDirectionAndFixBrokenCJK(bool cjk_merge,
00064                                             TO_BLOCK* input_block);
00065 
00066   // To save computation, the process of generating partitions is broken
00067   // into the following 4 steps:
00068   // TestVerticalTextDirection
00069   // CorrectForRotation (used only if a rotation is to be applied)
00070   // FindLeaderPartitions
00071   // GradeBlobsIntoPartitions.
00072   // These functions are all required, in sequence, except for
00073   // CorrectForRotation, which is not needed if no rotation is applied.
00074 
00075   // Types all the blobs as vertical or horizontal text or unknown and
00076   // returns true if the majority are vertical.
00077   // If the blobs are rotated, it is necessary to call CorrectForRotation
00078   // after rotating everything, otherwise the work done here will be enough.
00079   // If osd_blobs is not null, a list of blobs from the dominant textline
00080   // direction are returned for use in orientation and script detection.
00081   bool TestVerticalTextDirection(TO_BLOCK* block,
00082                                  BLOBNBOX_CLIST* osd_blobs);
00083 
00084   // Corrects the data structures for the given rotation.
00085   void CorrectForRotation(const FCOORD& rerotation,
00086                           ColPartitionGrid* part_grid);
00087 
00088   // Finds leader partitions and inserts them into the give grid.
00089   void FindLeaderPartitions(TO_BLOCK* block,
00090                             ColPartitionGrid* part_grid);
00091 
00092   // Finds and marks noise those blobs that look like bits of vertical lines
00093   // that would otherwise screw up layout analysis.
00094   void RemoveLineResidue(ColPartition_LIST* big_part_list);
00095 
00096   // Types all the blobs as vertical text or horizontal text or unknown and
00097   // puts them into initial ColPartitions in the supplied part_grid.
00098   // rerotation determines how to get back to the image coordinates from the
00099   // blob coordinates (since they may have been rotated for vertical text).
00100   // block is the single block for the whole page or rectangle to be OCRed.
00101   // nontext_pix (full-size), is a binary mask used to prevent merges across
00102   // photo/text boundaries. It is not kept beyond this function.
00103   // denorm provides a mapping back to the image from the current blob
00104   // coordinate space.
00105   // projection provides a measure of textline density over the image and
00106   // provides functions to assist with diacritic detection. It should be a
00107   // pointer to a new TextlineProjection, and will be setup here.
00108   // part_grid is the output grid of textline partitions.
00109   // Large blobs that cause overlap are put in separate partitions and added
00110   // to the big_parts list.
00111   void GradeBlobsIntoPartitions(const FCOORD& rerotation,
00112                                 TO_BLOCK* block,
00113                                 Pix* nontext_pix,
00114                                 const DENORM* denorm,
00115                                 bool cjk_script,
00116                                 TextlineProjection* projection,
00117                                 ColPartitionGrid* part_grid,
00118                                 ColPartition_LIST* big_parts);
00119 
00120   // Handles a click event in a display window.
00121   virtual void HandleClick(int x, int y);
00122 
00123  private:
00124   // Computes the noise_density_ by summing the number of elements in a
00125   // neighbourhood of each grid cell.
00126   void ComputeNoiseDensity(TO_BLOCK* block, TabFind* line_grid);
00127 
00128   // Detects and marks leader dots/dashes.
00129   //    Leaders are horizontal chains of small or noise blobs that look
00130   //    monospace according to ColPartition::MarkAsLeaderIfMonospaced().
00131   // Detected leaders become the only occupants of the block->small_blobs list.
00132   // Non-leader small blobs get moved to the blobs list.
00133   // Non-leader noise blobs remain singletons in the noise list.
00134   // All small and noise blobs in high density regions are marked BTFT_NONTEXT.
00135   // block is the single block for the whole page or rectangle to be OCRed.
00136   // leader_parts is the output.
00137   void FindLeadersAndMarkNoise(TO_BLOCK* block,
00138                                ColPartition_LIST* leader_parts);
00139 
00142   void InsertBlobs(TO_BLOCK* block);
00143 
00144   // Fix broken CJK characters, using the fake joined blobs mechanism.
00145   // Blobs are really merged, ie the master takes all the outlines and the
00146   // others are deleted.
00147   // Returns true if sufficient blobs are merged that it may be worth running
00148   // again, due to a better estimate of character size.
00149   bool FixBrokenCJK(TO_BLOCK* block);
00150 
00151   // Collect blobs that overlap or are within max_dist of the input bbox.
00152   // Return them in the list of blobs and expand the bbox to be the union
00153   // of all the boxes. not_this is excluded from the search, as are blobs
00154   // that cause the merged box to exceed max_size in either dimension.
00155   void AccumulateOverlaps(const BLOBNBOX* not_this, bool debug,
00156                           int max_size, int max_dist,
00157                           TBOX* bbox, BLOBNBOX_CLIST* blobs);
00158 
00159   // For each blob in this grid, Finds the textline direction to be horizontal
00160   // or vertical according to distance to neighbours and 1st and 2nd order
00161   // neighbours. Non-text tends to end up without a definite direction.
00162   // Result is setting of the neighbours and vert_possible/horz_possible
00163   // flags in the BLOBNBOXes currently in this grid.
00164   // This function is called more than once if page orientation is uncertain,
00165   // so display_if_debugging is true on the final call to display the results.
00166   void FindTextlineFlowDirection(bool display_if_debugging);
00167 
00168   // Sets the neighbours and good_stroke_neighbours members of the blob by
00169   // searching close on all 4 sides.
00170   // When finding leader dots/dashes, there is a slightly different rule for
00171   // what makes a good neighbour.
00172   // If activate_line_trap, then line-like objects are found and isolated.
00173   void SetNeighbours(bool leaders, bool activate_line_trap, BLOBNBOX* blob);
00174 
00175   // Sets the good_stroke_neighbours member of the blob if it has a
00176   // GoodNeighbour on the given side.
00177   // Also sets the neighbour in the blob, whether or not a good one is found.
00178   // Return value is the number of neighbours in the line trap size range.
00179   // Leaders get extra special lenient treatment.
00180   int FindGoodNeighbour(BlobNeighbourDir dir, bool leaders, BLOBNBOX* blob);
00181 
00182   // Makes the blob to be only horizontal or vertical where evidence
00183   // is clear based on gaps of 2nd order neighbours.
00184   void SetNeighbourFlows(BLOBNBOX* blob);
00185 
00186   // Nullify the neighbours in the wrong directions where the direction
00187   // is clear-cut based on a distance margin. Good for isolating vertical
00188   // text from neighbouring horizontal text.
00189   void SimplifyObviousNeighbours(BLOBNBOX* blob);
00190 
00191   // Smoothes the vertical/horizontal type of the blob based on the
00192   // 2nd-order neighbours. If reset_all is true, then all blobs are
00193   // changed. Otherwise, only ambiguous blobs are processed.
00194   void SmoothNeighbourTypes(BLOBNBOX* blob, bool desperate);
00195 
00196   // Checks the left or right side of the given leader partition and sets the
00197   // (opposite) leader_on_right or leader_on_left flags for blobs
00198   // that are next to the given side of the given leader partition.
00199   void MarkLeaderNeighbours(const ColPartition* part, LeftOrRight side);
00200 
00201   // Partition creation. Accumulates vertical and horizontal text chains,
00202   // puts the remaining blobs in as unknowns, and then merges/splits to
00203   // minimize overlap and smoothes the types with neighbours and the color
00204   // image if provided. rerotation is used to rotate the coordinate space
00205   // back to the nontext_map_ image.
00206   void FindInitialPartitions(const FCOORD& rerotation,
00207                              TO_BLOCK* block,
00208                              ColPartitionGrid* part_grid,
00209                              ColPartition_LIST* big_parts);
00210   // Finds vertical chains of text-like blobs and puts them in ColPartitions.
00211   void FindVerticalTextChains(ColPartitionGrid* part_grid);
00212   // Finds horizontal chains of text-like blobs and puts them in ColPartitions.
00213   void FindHorizontalTextChains(ColPartitionGrid* part_grid);
00214   // Finds diacritics and saves their base character in the blob.
00215   void TestDiacritics(ColPartitionGrid* part_grid, TO_BLOCK* block);
00216   // Searches this grid for an appropriately close and sized neighbour of the
00217   // given [small] blob. If such a blob is found, the diacritic base is saved
00218   // in the blob and true is returned.
00219   // The small_grid is a secondary grid that contains the small/noise objects
00220   // that are not in this grid, but may be useful for determining a connection
00221   // between blob and its potential base character. (See DiacriticXGapFilled.)
00222   bool DiacriticBlob(BlobGrid* small_grid, BLOBNBOX* blob);
00223   // Returns true if there is no gap between the base char and the diacritic
00224   // bigger than a fraction of the height of the base char:
00225   // Eg: line end.....'
00226   // The quote is a long way from the end of the line, yet it needs to be a
00227   // diacritic. To determine that the quote is not part of an image, or
00228   // a different text block, we check for other marks in the gap between
00229   // the base char and the diacritic.
00230   //                          '<--Diacritic
00231   // |---------|
00232   // |         |<-toobig-gap->
00233   // | Base    |<ok gap>
00234   // |---------|        x<-----Dot occupying gap
00235   // The grid is const really.
00236   bool DiacriticXGapFilled(BlobGrid* grid, const TBOX& diacritic_box,
00237                            const TBOX& base_box);
00238   // Merges diacritics with the ColPartition of the base character blob.
00239   void MergeDiacritics(TO_BLOCK* block, ColPartitionGrid* part_grid);
00240   // Any blobs on the large_blobs list of block that are still unowned by a
00241   // ColPartition, are probably drop-cap or vertically touching so the blobs
00242   // are removed to the big_parts list and treated separately.
00243   void RemoveLargeUnusedBlobs(TO_BLOCK* block,
00244                               ColPartitionGrid* part_grid,
00245                               ColPartition_LIST* big_parts);
00246 
00247     // All remaining unused blobs are put in individual ColPartitions.
00248   void PartitionRemainingBlobs(ColPartitionGrid* part_grid);
00249 
00250   // If combine, put all blobs in the cell_list into a single partition,
00251   // otherwise put each one into its own partition.
00252   void MakePartitionsFromCellList(bool combine,
00253                                   ColPartitionGrid* part_grid,
00254                                   BLOBNBOX_CLIST* cell_list);
00255 
00256   // Helper function to finish setting up a ColPartition and insert into
00257   // part_grid.
00258   void CompletePartition(ColPartition* part, ColPartitionGrid* part_grid);
00259 
00260   // Merge partitions where the merge appears harmless.
00261   void EasyMerges(ColPartitionGrid* part_grid);
00262 
00263   // Compute a search box based on the orientation of the partition.
00264   // Returns true if a suitable box can be calculated.
00265   // Callback for EasyMerges.
00266   bool OrientationSearchBox(ColPartition* part, TBOX* box);
00267 
00268   // Merge confirmation callback for EasyMerges.
00269   bool ConfirmEasyMerge(const ColPartition* p1, const ColPartition* p2);
00270 
00271   // Returns true if there is no significant noise in between the boxes.
00272   bool NoNoiseInBetween(const TBOX& box1, const TBOX& box2) const;
00273 
00274   // Displays the blobs colored according to the number of good neighbours
00275   // and the vertical/horizontal flow.
00276   ScrollView* DisplayGoodBlobs(const char* window_name, int x, int y);
00277 
00278   // Displays blobs colored according to whether or not they are diacritics.
00279   ScrollView* DisplayDiacritics(const char* window_name,
00280                                 int x, int y, TO_BLOCK* block);
00281 
00282  private:
00283   // Image map of photo/noise areas on the page. Borrowed pointer (not owned.)
00284   Pix* nontext_map_;
00285   // Textline projection map. Borrowed pointer.
00286   TextlineProjection* projection_;
00287   // DENORM used by projection_ to get back to image coords. Borrowed pointer.
00288   const DENORM* denorm_;
00289   // Bounding box of the grid.
00290   TBOX grid_box_;
00291   // Rerotation to get back to the original image.
00292   FCOORD rerotation_;
00293   // Windows for debug display.
00294   ScrollView* leaders_win_;
00295   ScrollView* initial_widths_win_;
00296   ScrollView* widths_win_;
00297   ScrollView* chains_win_;
00298   ScrollView* diacritics_win_;
00299   ScrollView* textlines_win_;
00300   ScrollView* smoothed_win_;
00301 };
00302 
00303 }  // namespace tesseract.
00304 
00305 #endif  // TESSERACT_TEXTORD_STROKEWIDTH_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines