Tesseract
3.02
|
00001 00002 // File: alignedblob.h 00003 // Description: A class to find vertically aligned blobs in a BBGrid, 00004 // and a struct to hold control parameters. 00005 // Author: Ray Smith 00006 // Created: Fri Mar 21 15:03:01 PST 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_ALIGNEDBLOB_H__ 00022 #define TESSERACT_TEXTORD_ALIGNEDBLOB_H__ 00023 00024 #include "bbgrid.h" 00025 #include "blobbox.h" 00026 #include "strngs.h" 00027 #include "tabvector.h" 00028 00029 extern INT_VAR_H(textord_debug_bugs, 0, 00030 "Turn on output related to bugs in tab finding"); 00031 extern INT_VAR_H(textord_debug_tabfind, 2, "Debug tab finding"); 00032 extern BOOL_VAR_H(textord_debug_images, false, 00033 "Use greyed image background for debug"); 00034 extern BOOL_VAR_H(textord_debug_printable, false, 00035 "Make debug windows printable"); 00036 00037 namespace tesseract { 00038 00039 // Simple structure to hold the search parameters for AlignedBlob. 00040 // The members are mostly derived from constants, which are 00041 // conditioned on the alignment parameter. 00042 // For finding vertical lines, a different set of constants are 00043 // used, conditioned on the different constructor. 00044 struct AlignedBlobParams { 00045 // Constructor to set the parameters for finding aligned and ragged tabs. 00046 // Vertical_x and vertical_y are the current estimates of the true vertical 00047 // direction (up) in the image. Height is the height of the starter blob. 00048 // v_gap_multiple is the multiple of height that will be used as a limit 00049 // on vertical gap before giving up and calling the line ended. 00050 // resolution is the original image resolution, and align0 indicates the 00051 // type of tab stop to be found. 00052 AlignedBlobParams(int vertical_x, int vertical_y, int height, 00053 int v_gap_multiple, int min_gutter_width, int resolution, 00054 TabAlignment alignment0); 00055 // Constructor to set the parameters for finding vertical lines. 00056 // Vertical_x and vertical_y are the current estimates of the true vertical 00057 // direction (up) in the image. Width is the width of the starter blob. 00058 AlignedBlobParams(int vertical_x, int vertical_y, int width); 00059 00060 // Fit the vertical vector into an ICOORD, which is 16 bit. 00061 void set_vertical(int vertical_x, int vertical_y); 00062 00063 double gutter_fraction; // Multiple of height used for min_gutter. 00064 bool right_tab; // We are looking at right edges. 00065 bool ragged; // We are looking for a ragged (vs aligned) edge. 00066 TabAlignment alignment; // The type we are trying to produce. 00067 TabType confirmed_type; // Type to flag blobs if accepted. 00068 int max_v_gap; // Max vertical gap to be tolerated. 00069 int min_gutter; // Minimum gutter between columns. 00070 // Tolerances allowed on horizontal alignment of aligned edges. 00071 int l_align_tolerance; // Left edges. 00072 int r_align_tolerance; // Right edges. 00073 // Conditions for accepting a line. 00074 int min_points; // Minimum number of points to be OK. 00075 int min_length; // Min length of completed line. 00076 00077 ICOORD vertical; // Current estimate of logical vertical. 00078 }; 00079 00080 // The AlignedBlob class contains code to find vertically aligned blobs. 00081 // This is factored out into a separate class, so it can be used by both 00082 // vertical line finding (LineFind) and tabstop finding (TabFind). 00083 class AlignedBlob : public BlobGrid { 00084 public: 00085 AlignedBlob(int gridsize, const ICOORD& bleft, const ICOORD& tright); 00086 virtual ~AlignedBlob(); 00087 00088 // Return true if the given coordinates are within the test rectangle 00089 // and the debug level is at least the given detail level. 00090 static bool WithinTestRegion(int detail_level, int x, int y); 00091 00092 // Display the tab codes of the BLOBNBOXes in this grid. 00093 ScrollView* DisplayTabs(const char* window_name, ScrollView* tab_win); 00094 00095 // Finds a vector corresponding to a set of vertically aligned blob edges 00096 // running through the given box. The type of vector returned and the 00097 // search parameters are determined by the AlignedBlobParams. 00098 // vertical_x and y are updated with an estimate of the real 00099 // vertical direction. (skew finding.) 00100 // Returns NULL if no decent vector can be found. 00101 TabVector* FindVerticalAlignment(AlignedBlobParams align_params, 00102 BLOBNBOX* bbox, 00103 int* vertical_x, int* vertical_y); 00104 00105 // Increment the serial number counter and set the string to use 00106 // for a filename if textord_debug_images is true. 00107 static void IncrementDebugPix(); 00108 00109 // Return the string to use for a filename if textord_debug_images is true. 00110 // Use IncrementDebugPix first to set the filename, and each time is 00111 // to be incremented. 00112 static const STRING& textord_debug_pix() { 00113 return textord_debug_pix_; 00114 } 00115 00116 private: 00117 // Find a set of blobs that are aligned in the given vertical 00118 // direction with the given blob. Returns a list of aligned 00119 // blobs and the number in the list. 00120 // For other parameters see FindAlignedBlob below. 00121 int AlignTabs(const AlignedBlobParams& params, 00122 bool top_to_bottom, BLOBNBOX* bbox, 00123 BLOBNBOX_CLIST* good_points, int* end_y); 00124 00125 // Search vertically for a blob that is aligned with the input bbox. 00126 // The search parameters are determined by AlignedBlobParams. 00127 // top_to_bottom tells whether to search down or up. 00128 // The return value is NULL if nothing was found in the search box 00129 // or if a blob was found in the gutter. On a NULL return, end_y 00130 // is set to the edge of the search box or the leading edge of the 00131 // gutter blob if one was found. 00132 BLOBNBOX* FindAlignedBlob(const AlignedBlobParams& p, 00133 bool top_to_bottom, BLOBNBOX* bbox, 00134 int x_start, int* end_y); 00135 00136 // Name of image file to use if textord_debug_images is true. 00137 static STRING textord_debug_pix_; 00138 // Index to image file to use if textord_debug_images is true. 00139 static int debug_pix_index_; 00140 }; 00141 00142 } // namespace tesseract. 00143 00144 #endif // TESSERACT_TEXTORD_ALIGNEDBLOB_H__ 00145