Tesseract
3.02
|
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 TextlineProjection* projection, 00116 ColPartitionGrid* part_grid, 00117 ColPartition_LIST* big_parts); 00118 00119 // Handles a click event in a display window. 00120 virtual void HandleClick(int x, int y); 00121 00122 private: 00123 // Computes the noise_density_ by summing the number of elements in a 00124 // neighbourhood of each grid cell. 00125 void ComputeNoiseDensity(TO_BLOCK* block, TabFind* line_grid); 00126 00127 // Detects and marks leader dots/dashes. 00128 // Leaders are horizontal chains of small or noise blobs that look 00129 // monospace according to ColPartition::MarkAsLeaderIfMonospaced(). 00130 // Detected leaders become the only occupants of the block->small_blobs list. 00131 // Non-leader small blobs get moved to the blobs list. 00132 // Non-leader noise blobs remain singletons in the noise list. 00133 // All small and noise blobs in high density regions are marked BTFT_NONTEXT. 00134 // block is the single block for the whole page or rectangle to be OCRed. 00135 // leader_parts is the output. 00136 void FindLeadersAndMarkNoise(TO_BLOCK* block, 00137 ColPartition_LIST* leader_parts); 00138 00141 void InsertBlobs(TO_BLOCK* block); 00142 00143 // Fix broken CJK characters, using the fake joined blobs mechanism. 00144 // Blobs are really merged, ie the master takes all the outlines and the 00145 // others are deleted. 00146 // Returns true if sufficient blobs are merged that it may be worth running 00147 // again, due to a better estimate of character size. 00148 bool FixBrokenCJK(TO_BLOCK* block); 00149 00150 // Collect blobs that overlap or are within max_dist of the input bbox. 00151 // Return them in the list of blobs and expand the bbox to be the union 00152 // of all the boxes. not_this is excluded from the search, as are blobs 00153 // that cause the merged box to exceed max_size in either dimension. 00154 void AccumulateOverlaps(const BLOBNBOX* not_this, bool debug, 00155 int max_size, int max_dist, 00156 TBOX* bbox, BLOBNBOX_CLIST* blobs); 00157 00158 // For each blob in this grid, Finds the textline direction to be horizontal 00159 // or vertical according to distance to neighbours and 1st and 2nd order 00160 // neighbours. Non-text tends to end up without a definite direction. 00161 // Result is setting of the neighbours and vert_possible/horz_possible 00162 // flags in the BLOBNBOXes currently in this grid. 00163 // This function is called more than once if page orientation is uncertain, 00164 // so display_if_debugging is true on the final call to display the results. 00165 void FindTextlineFlowDirection(bool display_if_debugging); 00166 00167 // Sets the neighbours and good_stroke_neighbours members of the blob by 00168 // searching close on all 4 sides. 00169 // When finding leader dots/dashes, there is a slightly different rule for 00170 // what makes a good neighbour. 00171 // If activate_line_trap, then line-like objects are found and isolated. 00172 void SetNeighbours(bool leaders, bool activate_line_trap, BLOBNBOX* blob); 00173 00174 // Sets the good_stroke_neighbours member of the blob if it has a 00175 // GoodNeighbour on the given side. 00176 // Also sets the neighbour in the blob, whether or not a good one is found. 00177 // Return value is the number of neighbours in the line trap size range. 00178 // Leaders get extra special lenient treatment. 00179 int FindGoodNeighbour(BlobNeighbourDir dir, bool leaders, BLOBNBOX* blob); 00180 00181 // Makes the blob to be only horizontal or vertical where evidence 00182 // is clear based on gaps of 2nd order neighbours. 00183 void SetNeighbourFlows(BLOBNBOX* blob); 00184 00185 // Nullify the neighbours in the wrong directions where the direction 00186 // is clear-cut based on a distance margin. Good for isolating vertical 00187 // text from neighbouring horizontal text. 00188 void SimplifyObviousNeighbours(BLOBNBOX* blob); 00189 00190 // Smoothes the vertical/horizontal type of the blob based on the 00191 // 2nd-order neighbours. If reset_all is true, then all blobs are 00192 // changed. Otherwise, only ambiguous blobs are processed. 00193 void SmoothNeighbourTypes(BLOBNBOX* blob, bool desperate); 00194 00195 // Checks the left or right side of the given leader partition and sets the 00196 // (opposite) leader_on_right or leader_on_left flags for blobs 00197 // that are next to the given side of the given leader partition. 00198 void MarkLeaderNeighbours(const ColPartition* part, LeftOrRight side); 00199 00200 // Partition creation. Accumulates vertical and horizontal text chains, 00201 // puts the remaining blobs in as unknowns, and then merges/splits to 00202 // minimize overlap and smoothes the types with neighbours and the color 00203 // image if provided. rerotation is used to rotate the coordinate space 00204 // back to the nontext_map_ image. 00205 void FindInitialPartitions(const FCOORD& rerotation, 00206 TO_BLOCK* block, 00207 ColPartitionGrid* part_grid, 00208 ColPartition_LIST* big_parts); 00209 // Finds vertical chains of text-like blobs and puts them in ColPartitions. 00210 void FindVerticalTextChains(ColPartitionGrid* part_grid); 00211 // Finds horizontal chains of text-like blobs and puts them in ColPartitions. 00212 void FindHorizontalTextChains(ColPartitionGrid* part_grid); 00213 // Finds diacritics and saves their base character in the blob. 00214 void TestDiacritics(ColPartitionGrid* part_grid, TO_BLOCK* block); 00215 // Searches this grid for an appropriately close and sized neighbour of the 00216 // given [small] blob. If such a blob is found, the diacritic base is saved 00217 // in the blob and true is returned. 00218 // The small_grid is a secondary grid that contains the small/noise objects 00219 // that are not in this grid, but may be useful for determining a connection 00220 // between blob and its potential base character. (See DiacriticXGapFilled.) 00221 bool DiacriticBlob(BlobGrid* small_grid, BLOBNBOX* blob); 00222 // Returns true if there is no gap between the base char and the diacritic 00223 // bigger than a fraction of the height of the base char: 00224 // Eg: line end.....' 00225 // The quote is a long way from the end of the line, yet it needs to be a 00226 // diacritic. To determine that the quote is not part of an image, or 00227 // a different text block, we check for other marks in the gap between 00228 // the base char and the diacritic. 00229 // '<--Diacritic 00230 // |---------| 00231 // | |<-toobig-gap-> 00232 // | Base |<ok gap> 00233 // |---------| x<-----Dot occupying gap 00234 // The grid is const really. 00235 bool DiacriticXGapFilled(BlobGrid* grid, const TBOX& diacritic_box, 00236 const TBOX& base_box); 00237 // Merges diacritics with the ColPartition of the base character blob. 00238 void MergeDiacritics(TO_BLOCK* block, ColPartitionGrid* part_grid); 00239 // Any blobs on the large_blobs list of block that are still unowned by a 00240 // ColPartition, are probably drop-cap or vertically touching so the blobs 00241 // are removed to the big_parts list and treated separately. 00242 void RemoveLargeUnusedBlobs(TO_BLOCK* block, 00243 ColPartitionGrid* part_grid, 00244 ColPartition_LIST* big_parts); 00245 00246 // All remaining unused blobs are put in individual ColPartitions. 00247 void PartitionRemainingBlobs(ColPartitionGrid* part_grid); 00248 00249 // If combine, put all blobs in the cell_list into a single partition, 00250 // otherwise put each one into its own partition. 00251 void MakePartitionsFromCellList(bool combine, 00252 ColPartitionGrid* part_grid, 00253 BLOBNBOX_CLIST* cell_list); 00254 00255 // Helper function to finish setting up a ColPartition and insert into 00256 // part_grid. 00257 void CompletePartition(ColPartition* part, ColPartitionGrid* part_grid); 00258 00259 // Merge partitions where the merge appears harmless. 00260 void EasyMerges(ColPartitionGrid* part_grid); 00261 00262 // Compute a search box based on the orientation of the partition. 00263 // Returns true if a suitable box can be calculated. 00264 // Callback for EasyMerges. 00265 bool OrientationSearchBox(ColPartition* part, TBOX* box); 00266 00267 // Merge confirmation callback for EasyMerges. 00268 bool ConfirmEasyMerge(const ColPartition* p1, const ColPartition* p2); 00269 00270 // Returns true if there is no significant noise in between the boxes. 00271 bool NoNoiseInBetween(const TBOX& box1, const TBOX& box2) const; 00272 00273 // Displays the blobs colored according to the number of good neighbours 00274 // and the vertical/horizontal flow. 00275 ScrollView* DisplayGoodBlobs(const char* window_name, int x, int y); 00276 00277 // Displays blobs colored according to whether or not they are diacritics. 00278 ScrollView* DisplayDiacritics(const char* window_name, 00279 int x, int y, TO_BLOCK* block); 00280 00281 private: 00282 // Image map of photo/noise areas on the page. Borrowed pointer (not owned.) 00283 Pix* nontext_map_; 00284 // Textline projection map. Borrowed pointer. 00285 TextlineProjection* projection_; 00286 // DENORM used by projection_ to get back to image coords. Borrowed pointer. 00287 const DENORM* denorm_; 00288 // Bounding box of the grid. 00289 TBOX grid_box_; 00290 // Rerotation to get back to the original image. 00291 FCOORD rerotation_; 00292 // Windows for debug display. 00293 ScrollView* leaders_win_; 00294 ScrollView* initial_widths_win_; 00295 ScrollView* widths_win_; 00296 ScrollView* chains_win_; 00297 ScrollView* diacritics_win_; 00298 ScrollView* textlines_win_; 00299 ScrollView* smoothed_win_; 00300 }; 00301 00302 } // namespace tesseract. 00303 00304 #endif // TESSERACT_TEXTORD_STROKEWIDTH_H__