Tesseract 3.01
|
00001 00002 // File: textord.h 00003 // Description: The Textord class definition gathers text line and word 00004 // finding functionality. 00005 // Author: Ray Smith 00006 // Created: Fri Mar 13 14:29:01 PDT 2009 00007 // 00008 // (C) Copyright 2009, 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_TEXTORD_H__ 00022 #define TESSERACT_TEXTORD_TEXTORD_H__ 00023 00024 #include "ccstruct.h" 00025 #include "blobbox.h" 00026 #include "gap_map.h" 00027 #include "notdll.h" 00028 #include "publictypes.h" // For PageSegMode. 00029 00030 class FCOORD; 00031 class BLOCK_LIST; 00032 class PAGE_RES; 00033 class TO_BLOCK; 00034 class TO_BLOCK_LIST; 00035 class ScrollView; 00036 00037 namespace tesseract { 00038 00039 class Textord { 00040 public: 00041 explicit Textord(CCStruct* ccstruct); 00042 ~Textord(); 00043 00044 // Make the textlines and words inside each block. 00045 void TextordPage(PageSegMode pageseg_mode, 00046 int width, int height, Pix* pix, 00047 BLOCK_LIST* blocks, TO_BLOCK_LIST* to_blocks); 00048 00049 // If we were supposed to return only a single textline, and there is more 00050 // than one, clean up and leave only the best. 00051 void CleanupSingleRowResult(PageSegMode pageseg_mode, PAGE_RES* page_res); 00052 00053 // tospace.cpp /////////////////////////////////////////// 00054 void to_spacing( 00055 ICOORD page_tr, //topright of page 00056 TO_BLOCK_LIST *blocks //blocks on page 00057 ); 00058 ROW *make_prop_words(TO_ROW *row, // row to make 00059 FCOORD rotation // for drawing 00060 ); 00061 ROW *make_blob_words(TO_ROW *row, // row to make 00062 FCOORD rotation // for drawing 00063 ); 00064 // tordmain.cpp /////////////////////////////////////////// 00065 void find_components(Pix* pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks); 00066 void filter_blobs(ICOORD page_tr, TO_BLOCK_LIST *blocks, BOOL8 testing_on); 00067 private: 00068 // For underlying memory management and other utilities. 00069 CCStruct* ccstruct_; 00070 00071 // The size of the input image. 00072 ICOORD page_tr_; 00073 00074 // makerow.cpp /////////////////////////////////////////// 00075 // Make the textlines inside each block. 00076 void MakeRows(PageSegMode pageseg_mode, const FCOORD& skew, 00077 int width, int height, TO_BLOCK_LIST* to_blocks); 00078 // Make the textlines inside a single block. 00079 void MakeBlockRows(int min_spacing, int max_spacing, 00080 const FCOORD& skew, TO_BLOCK* block, 00081 ScrollView* win); 00082 00083 void fit_rows(float gradient, ICOORD page_tr, TO_BLOCK_LIST *blocks); 00084 void cleanup_rows_fitting(ICOORD page_tr, // top right 00085 TO_BLOCK *block, // block to do 00086 float gradient, // gradient to fit 00087 FCOORD rotation, // for drawing 00088 inT32 block_edge, // edge of block 00089 BOOL8 testing_on); // correct orientation 00090 void compute_block_xheight(TO_BLOCK *block, float gradient); 00091 void compute_row_xheight(TO_ROW *row, // row to do 00092 const FCOORD& rotation, 00093 float gradient, // global skew 00094 int block_line_size); 00095 void make_spline_rows(TO_BLOCK *block, // block to do 00096 float gradient, // gradient to fit 00097 FCOORD rotation, // for drawing 00098 inT32 block_edge, // edge of block 00099 BOOL8 testing_on); 00100 00102 void make_old_baselines(TO_BLOCK *block, // block to do 00103 BOOL8 testing_on, // correct orientation 00104 float gradient); 00105 void correlate_lines(TO_BLOCK *block, float gradient); 00106 void correlate_neighbours(TO_BLOCK *block, // block rows are in. 00107 TO_ROW **rows, // rows of block. 00108 int rowcount); // no of rows to do. 00109 int correlate_with_stats(TO_ROW **rows, // rows of block. 00110 int rowcount, // no of rows to do. 00111 TO_BLOCK* block); 00112 void find_textlines(TO_BLOCK *block, // block row is in 00113 TO_ROW *row, // row to do 00114 int degree, // required approximation 00115 QSPLINE *spline); // starting spline 00116 // tospace.cpp /////////////////////////////////////////// 00117 //DEBUG USE ONLY 00118 void block_spacing_stats(TO_BLOCK *block, 00119 GAPMAP *gapmap, 00120 BOOL8 &old_text_ord_proportional, 00121 //resulting estimate 00122 inT16 &block_space_gap_width, 00123 //resulting estimate 00124 inT16 &block_non_space_gap_width 00125 ); 00126 void row_spacing_stats(TO_ROW *row, 00127 GAPMAP *gapmap, 00128 inT16 block_idx, 00129 inT16 row_idx, 00130 //estimate for block 00131 inT16 block_space_gap_width, 00132 //estimate for block 00133 inT16 block_non_space_gap_width 00134 ); 00135 void old_to_method(TO_ROW *row, 00136 STATS *all_gap_stats, 00137 STATS *space_gap_stats, 00138 STATS *small_gap_stats, 00139 inT16 block_space_gap_width, 00140 //estimate for block 00141 inT16 block_non_space_gap_width 00142 ); 00143 BOOL8 isolated_row_stats(TO_ROW *row, 00144 GAPMAP *gapmap, 00145 STATS *all_gap_stats, 00146 BOOL8 suspected_table, 00147 inT16 block_idx, 00148 inT16 row_idx); 00149 inT16 stats_count_under(STATS *stats, inT16 threshold); 00150 void improve_row_threshold(TO_ROW *row, STATS *all_gap_stats); 00151 BOOL8 make_a_word_break(TO_ROW *row, // row being made 00152 TBOX blob_box, // for next_blob // how many blanks? 00153 inT16 prev_gap, 00154 TBOX prev_blob_box, 00155 inT16 real_current_gap, 00156 inT16 within_xht_current_gap, 00157 TBOX next_blob_box, 00158 inT16 next_gap, 00159 uinT8 &blanks, 00160 BOOL8 &fuzzy_sp, 00161 BOOL8 &fuzzy_non, 00162 BOOL8& prev_gap_was_a_space, 00163 BOOL8& break_at_next_gap); 00164 BOOL8 narrow_blob(TO_ROW *row, TBOX blob_box); 00165 BOOL8 wide_blob(TO_ROW *row, TBOX blob_box); 00166 BOOL8 suspected_punct_blob(TO_ROW *row, TBOX box); 00167 void peek_at_next_gap(TO_ROW *row, 00168 BLOBNBOX_IT box_it, 00169 TBOX &next_blob_box, 00170 inT16 &next_gap, 00171 inT16 &next_within_xht_gap); 00172 void mark_gap(TBOX blob, //blob following gap 00173 inT16 rule, // heuristic id 00174 inT16 prev_gap, 00175 inT16 prev_blob_width, 00176 inT16 current_gap, 00177 inT16 next_blob_width, 00178 inT16 next_gap); 00179 float find_mean_blob_spacing(WERD *word); 00180 BOOL8 ignore_big_gap(TO_ROW *row, 00181 inT32 row_length, 00182 GAPMAP *gapmap, 00183 inT16 left, 00184 inT16 right); 00185 //get bounding box 00186 TBOX reduced_box_next(TO_ROW *row, //current row 00187 BLOBNBOX_IT *it //iterator to blobds 00188 ); 00189 TBOX reduced_box_for_blob(BLOBNBOX *blob, TO_ROW *row, inT16 *left_above_xht); 00190 // tordmain.cpp /////////////////////////////////////////// 00191 float filter_noise_blobs(BLOBNBOX_LIST *src_list, 00192 BLOBNBOX_LIST *noise_list, 00193 BLOBNBOX_LIST *small_list, 00194 BLOBNBOX_LIST *large_list); 00195 void cleanup_blocks(BLOCK_LIST *blocks); 00196 BOOL8 clean_noise_from_row(ROW *row); 00197 void clean_noise_from_words(ROW *row); 00198 // Remove outlines that are a tiny fraction in either width or height 00199 // of the word height. 00200 void clean_small_noise_from_words(ROW *row); 00201 public: 00202 // makerow.cpp /////////////////////////////////////////// 00203 BOOL_VAR_H(textord_single_height_mode, false, 00204 "Script has no xheight, so use a single mode for horizontal text"); 00205 // tospace.cpp /////////////////////////////////////////// 00206 BOOL_VAR_H(tosp_old_to_method, false, "Space stats use prechopping?"); 00207 BOOL_VAR_H(tosp_old_to_constrain_sp_kn, false, 00208 "Constrain relative values of inter and intra-word gaps for " 00209 "old_to_method."); 00210 BOOL_VAR_H(tosp_only_use_prop_rows, true, 00211 "Block stats to use fixed pitch rows?"); 00212 BOOL_VAR_H(tosp_force_wordbreak_on_punct, false, 00213 "Force word breaks on punct to break long lines in non-space " 00214 "delimited langs"); 00215 BOOL_VAR_H(tosp_use_pre_chopping, false, 00216 "Space stats use prechopping?"); 00217 BOOL_VAR_H(tosp_old_to_bug_fix, false, 00218 "Fix suspected bug in old code"); 00219 BOOL_VAR_H(tosp_block_use_cert_spaces, true, 00220 "Only stat OBVIOUS spaces"); 00221 BOOL_VAR_H(tosp_row_use_cert_spaces, true, 00222 "Only stat OBVIOUS spaces"); 00223 BOOL_VAR_H(tosp_narrow_blobs_not_cert, true, 00224 "Only stat OBVIOUS spaces"); 00225 BOOL_VAR_H(tosp_row_use_cert_spaces1, true, 00226 "Only stat OBVIOUS spaces"); 00227 BOOL_VAR_H(tosp_recovery_isolated_row_stats, true, 00228 "Use row alone when inadequate cert spaces"); 00229 BOOL_VAR_H(tosp_only_small_gaps_for_kern, false, "Better guess"); 00230 BOOL_VAR_H(tosp_all_flips_fuzzy, false, "Pass ANY flip to context?"); 00231 BOOL_VAR_H(tosp_fuzzy_limit_all, true, 00232 "Dont restrict kn->sp fuzzy limit to tables"); 00233 BOOL_VAR_H(tosp_stats_use_xht_gaps, true, 00234 "Use within xht gap for wd breaks"); 00235 BOOL_VAR_H(tosp_use_xht_gaps, true, 00236 "Use within xht gap for wd breaks"); 00237 BOOL_VAR_H(tosp_only_use_xht_gaps, false, 00238 "Only use within xht gap for wd breaks"); 00239 BOOL_VAR_H(tosp_rule_9_test_punct, false, 00240 "Dont chng kn to space next to punct"); 00241 BOOL_VAR_H(tosp_flip_fuzz_kn_to_sp, true, "Default flip"); 00242 BOOL_VAR_H(tosp_flip_fuzz_sp_to_kn, true, "Default flip"); 00243 BOOL_VAR_H(tosp_improve_thresh, false, 00244 "Enable improvement heuristic"); 00245 INT_VAR_H(tosp_debug_level, 0, "Debug data"); 00246 INT_VAR_H(tosp_enough_space_samples_for_median, 3, 00247 "or should we use mean"); 00248 INT_VAR_H(tosp_redo_kern_limit, 10, 00249 "No.samples reqd to reestimate for row"); 00250 INT_VAR_H(tosp_few_samples, 40, 00251 "No.gaps reqd with 1 large gap to treat as a table"); 00252 INT_VAR_H(tosp_short_row, 20, 00253 "No.gaps reqd with few cert spaces to use certs"); 00254 INT_VAR_H(tosp_sanity_method, 1, "How to avoid being silly"); 00255 double_VAR_H(tosp_old_sp_kn_th_factor, 2.0, 00256 "Factor for defining space threshold in terms of space and " 00257 "kern sizes"); 00258 double_VAR_H(tosp_threshold_bias1, 0, 00259 "how far between kern and space?"); 00260 double_VAR_H(tosp_threshold_bias2, 0, 00261 "how far between kern and space?"); 00262 double_VAR_H(tosp_narrow_fraction, 0.3, 00263 "Fract of xheight for narrow"); 00264 double_VAR_H(tosp_narrow_aspect_ratio, 0.48, 00265 "narrow if w/h less than this"); 00266 double_VAR_H(tosp_wide_fraction, 0.52, "Fract of xheight for wide"); 00267 double_VAR_H(tosp_wide_aspect_ratio, 0.0, 00268 "wide if w/h less than this"); 00269 double_VAR_H(tosp_fuzzy_space_factor, 0.6, 00270 "Fract of xheight for fuzz sp"); 00271 double_VAR_H(tosp_fuzzy_space_factor1, 0.5, 00272 "Fract of xheight for fuzz sp"); 00273 double_VAR_H(tosp_fuzzy_space_factor2, 0.72, 00274 "Fract of xheight for fuzz sp"); 00275 double_VAR_H(tosp_gap_factor, 0.83, "gap ratio to flip sp->kern"); 00276 double_VAR_H(tosp_kern_gap_factor1, 2.0, 00277 "gap ratio to flip kern->sp"); 00278 double_VAR_H(tosp_kern_gap_factor2, 1.3, 00279 "gap ratio to flip kern->sp"); 00280 double_VAR_H(tosp_kern_gap_factor3, 2.5, 00281 "gap ratio to flip kern->sp"); 00282 double_VAR_H(tosp_ignore_big_gaps, -1, "xht multiplier"); 00283 double_VAR_H(tosp_ignore_very_big_gaps, 3.5, "xht multiplier"); 00284 double_VAR_H(tosp_rep_space, 1.6, "rep gap multiplier for space"); 00285 double_VAR_H(tosp_enough_small_gaps, 0.65, 00286 "Fract of kerns reqd for isolated row stats"); 00287 double_VAR_H(tosp_table_kn_sp_ratio, 2.25, 00288 "Min difference of kn & sp in table"); 00289 double_VAR_H(tosp_table_xht_sp_ratio, 0.33, 00290 "Expect spaces bigger than this"); 00291 double_VAR_H(tosp_table_fuzzy_kn_sp_ratio, 3.0, 00292 "Fuzzy if less than this"); 00293 double_VAR_H(tosp_fuzzy_kn_fraction, 0.5, "New fuzzy kn alg"); 00294 double_VAR_H(tosp_fuzzy_sp_fraction, 0.5, "New fuzzy sp alg"); 00295 double_VAR_H(tosp_min_sane_kn_sp, 1.5, 00296 "Dont trust spaces less than this time kn"); 00297 double_VAR_H(tosp_init_guess_kn_mult, 2.2, 00298 "Thresh guess - mult kn by this"); 00299 double_VAR_H(tosp_init_guess_xht_mult, 0.28, 00300 "Thresh guess - mult xht by this"); 00301 double_VAR_H(tosp_max_sane_kn_thresh, 5.0, 00302 "Multiplier on kn to limit thresh"); 00303 double_VAR_H(tosp_flip_caution, 0.0, 00304 "Dont autoflip kn to sp when large separation"); 00305 double_VAR_H(tosp_large_kerning, 0.19, 00306 "Limit use of xht gap with large kns"); 00307 double_VAR_H(tosp_dont_fool_with_small_kerns, -1, 00308 "Limit use of xht gap with odd small kns"); 00309 double_VAR_H(tosp_near_lh_edge, 0, 00310 "Dont reduce box if the top left is non blank"); 00311 double_VAR_H(tosp_silly_kn_sp_gap, 0.2, 00312 "Dont let sp minus kn get too small"); 00313 double_VAR_H(tosp_pass_wide_fuzz_sp_to_context, 0.75, 00314 "How wide fuzzies need context"); 00315 // tordmain.cpp /////////////////////////////////////////// 00316 BOOL_VAR_H(textord_no_rejects, false, "Don't remove noise blobs"); 00317 BOOL_VAR_H(textord_show_blobs, false, "Display unsorted blobs"); 00318 BOOL_VAR_H(textord_show_boxes, false, "Display boxes"); 00319 INT_VAR_H(textord_max_noise_size, 7, "Pixel size of noise"); 00320 double_VAR_H(textord_blob_size_bigile, 95, "Percentile for large blobs"); 00321 double_VAR_H(textord_noise_area_ratio, 0.7, 00322 "Fraction of bounding box for noise"); 00323 double_VAR_H(textord_blob_size_smallile, 20, "Percentile for small blobs"); 00324 double_VAR_H(textord_initialx_ile, 0.75, "Ile of sizes for xheight guess"); 00325 double_VAR_H(textord_initialasc_ile, 0.90, "Ile of sizes for xheight guess"); 00326 INT_VAR_H(textord_noise_sizefraction, 10, "Fraction of size for maxima"); 00327 double_VAR_H(textord_noise_sizelimit, 0.5, "Fraction of x for big t count"); 00328 INT_VAR_H(textord_noise_translimit, 16, "Transitions for normal blob"); 00329 double_VAR_H(textord_noise_normratio, 2.0, "Dot to norm ratio for deletion"); 00330 BOOL_VAR_H(textord_noise_rejwords, true, "Reject noise-like words"); 00331 BOOL_VAR_H(textord_noise_rejrows, true, "Reject noise-like rows"); 00332 double_VAR_H(textord_noise_syfract, 0.2, "xh fract error for norm blobs"); 00333 double_VAR_H(textord_noise_sxfract, 0.4, 00334 "xh fract width error for norm blobs"); 00335 double_VAR_H(textord_noise_hfract, 1.0/64, 00336 "Height fraction to discard outlines as speckle noise"); 00337 INT_VAR_H(textord_noise_sncount, 1, "super norm blobs to save row"); 00338 double_VAR_H(textord_noise_rowratio, 6.0, "Dot to norm ratio for deletion"); 00339 BOOL_VAR_H(textord_noise_debug, FALSE, "Debug row garbage detector"); 00340 double_VAR_H(textord_blshift_maxshift, 0.00, "Max baseline shift"); 00341 double_VAR_H(textord_blshift_xfraction, 9.99, "Min size of baseline shift"); 00342 }; 00343 } // namespace tesseract. 00344 00345 #endif // TESSERACT_TEXTORD_TEXTORD_H__