tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/textord/makerow.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        makerow.h  (Formerly makerows.h)
00003  * Description: Code to arrange blobs into rows of text.
00004  * Author:              Ray Smith
00005  * Created:             Mon Sep 21 14:34:48 BST 1992
00006  *
00007  * (C) Copyright 1992, Hewlett-Packard Ltd.
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  *
00018  **********************************************************************/
00019 
00020 #ifndef           MAKEROW_H
00021 #define           MAKEROW_H
00022 
00023 #include          "params.h"
00024 #include          "ocrblock.h"
00025 #include          "blobs.h"
00026 #include          "blobbox.h"
00027 #include          "statistc.h"
00028 
00029 enum OVERLAP_STATE
00030 {
00031   ASSIGN,                        //assign it to row
00032   REJECT,                        //reject it - dual overlap
00033   NEW_ROW
00034 };
00035 
00036 enum ROW_CATEGORY {
00037   ROW_ASCENDERS_FOUND,
00038   ROW_DESCENDERS_FOUND,
00039   ROW_UNKNOWN,
00040   ROW_INVALID,
00041 };
00042 
00043 extern BOOL_VAR_H(textord_heavy_nr, FALSE, "Vigorously remove noise");
00044 extern BOOL_VAR_H (textord_show_initial_rows, FALSE,
00045 "Display row accumulation");
00046 extern BOOL_VAR_H (textord_show_parallel_rows, FALSE,
00047 "Display page correlated rows");
00048 extern BOOL_VAR_H (textord_show_expanded_rows, FALSE,
00049 "Display rows after expanding");
00050 extern BOOL_VAR_H (textord_show_final_rows, FALSE,
00051 "Display rows after final fitting");
00052 extern BOOL_VAR_H (textord_show_final_blobs, FALSE,
00053 "Display blob bounds after pre-ass");
00054 extern BOOL_VAR_H (textord_test_landscape, FALSE, "Tests refer to land/port");
00055 extern BOOL_VAR_H (textord_parallel_baselines, TRUE,
00056 "Force parallel baselines");
00057 extern BOOL_VAR_H (textord_straight_baselines, FALSE,
00058 "Force straight baselines");
00059 extern BOOL_VAR_H (textord_quadratic_baselines, FALSE,
00060 "Use quadratic splines");
00061 extern BOOL_VAR_H (textord_old_baselines, TRUE, "Use old baseline algorithm");
00062 extern BOOL_VAR_H (textord_old_xheight, TRUE, "Use old xheight algorithm");
00063 extern BOOL_VAR_H (textord_fix_xheight_bug, TRUE, "Use spline baseline");
00064 extern BOOL_VAR_H (textord_fix_makerow_bug, TRUE,
00065 "Prevent multiple baselines");
00066 extern BOOL_VAR_H (textord_cblob_blockocc, TRUE,
00067 "Use new projection for underlines");
00068 extern BOOL_VAR_H (textord_debug_xheights, FALSE, "Test xheight algorithms");
00069 extern INT_VAR_H (textord_test_x, -MAX_INT32, "coord of test pt");
00070 extern INT_VAR_H (textord_test_y, -MAX_INT32, "coord of test pt");
00071 extern INT_VAR_H (textord_min_blobs_in_row, 4,
00072 "Min blobs before gradient counted");
00073 extern INT_VAR_H (textord_spline_minblobs, 8,
00074 "Min blobs in each spline segment");
00075 extern INT_VAR_H (textord_spline_medianwin, 6,
00076 "Size of window for spline segmentation");
00077 extern INT_VAR_H (textord_min_xheight, 10, "Min credible pixel xheight");
00078 extern double_VAR_H (textord_spline_shift_fraction, 0.02,
00079 "Fraction of line spacing for quad");
00080 extern double_VAR_H (textord_spline_outlier_fraction, 0.1,
00081 "Fraction of line spacing for outlier");
00082 extern double_VAR_H (textord_skew_ile, 0.5, "Ile of gradients for page skew");
00083 extern double_VAR_H (textord_skew_lag, 0.75,
00084 "Lag for skew on row accumulation");
00085 extern double_VAR_H (textord_linespace_iqrlimit, 0.2,
00086 "Max iqr/median for linespace");
00087 extern double_VAR_H (textord_width_limit, 8,
00088 "Max width of blobs to make rows");
00089 extern double_VAR_H (textord_chop_width, 1.5, "Max width before chopping");
00090 extern double_VAR_H (textord_minxh, 0.25,
00091 "fraction of linesize for min xheight");
00092 extern double_VAR_H (textord_min_linesize, 1.25,
00093 "* blob height for initial linesize");
00094 extern double_VAR_H (textord_excess_blobsize, 1.3,
00095 "New row made if blob makes row this big");
00096 extern double_VAR_H (textord_occupancy_threshold, 0.4,
00097 "Fraction of neighbourhood");
00098 extern double_VAR_H (textord_underline_width, 2.0,
00099 "Multiple of line_size for underline");
00100 extern double_VAR_H(textord_min_blob_height_fraction, 0.75,
00101 "Min blob height/top to include blob top into xheight stats");
00102 extern double_VAR_H (textord_xheight_mode_fraction, 0.4,
00103 "Min pile height to make xheight");
00104 extern double_VAR_H (textord_ascheight_mode_fraction, 0.15,
00105 "Min pile height to make ascheight");
00106 extern double_VAR_H (textord_ascx_ratio_min, 1.2, "Min cap/xheight");
00107 extern double_VAR_H (textord_ascx_ratio_max, 1.7, "Max cap/xheight");
00108 extern double_VAR_H (textord_descx_ratio_min, 0.15, "Min desc/xheight");
00109 extern double_VAR_H (textord_descx_ratio_max, 0.6, "Max desc/xheight");
00110 extern double_VAR_H (textord_xheight_error_margin, 0.1, "Accepted variation");
00111 extern INT_VAR_H (textord_lms_line_trials, 12, "Number of linew fits to do");
00112 extern BOOL_VAR_H (textord_new_initial_xheight, TRUE,
00113 "Use test xheight mechanism");
00114 extern BOOL_VAR_H(textord_debug_blob, FALSE, "Print test blob information");
00115 
00116 inline void get_min_max_xheight(int block_linesize,
00117                                 int *min_height, int *max_height) {
00118   *min_height = static_cast<inT32>(floor(block_linesize * textord_minxh));
00119   if (*min_height < textord_min_xheight) *min_height = textord_min_xheight;
00120   *max_height = static_cast<inT32>(ceil(block_linesize * 3.0));
00121 }
00122 
00123 inline ROW_CATEGORY get_row_category(const TO_ROW *row) {
00124   if (row->xheight <= 0) return ROW_INVALID;
00125   return (row->ascrise > 0) ? ROW_ASCENDERS_FOUND :
00126     (row->descdrop != 0) ? ROW_DESCENDERS_FOUND : ROW_UNKNOWN;
00127 }
00128 
00129 inline bool within_error_margin(float test, float num, float margin) {
00130   return (test >= num * (1 - margin) && test <= num * (1 + margin));
00131 }
00132 
00133 void fill_heights(TO_ROW *row, float gradient, int min_height,
00134                   int max_height, STATS *heights, STATS *floating_heights);
00135 
00136 float make_single_row(ICOORD page_tr, TO_BLOCK* block,
00137                       TO_BLOCK_LIST* blocks);
00138 float make_rows(ICOORD page_tr,              // top right
00139                 TO_BLOCK_LIST *port_blocks);
00140 void make_initial_textrows(ICOORD page_tr,
00141                            TO_BLOCK *block,  // block to do
00142                            FCOORD rotation,  // for drawing
00143                            BOOL8 testing_on);  // correct orientation
00144 void fit_lms_line(TO_ROW *row);
00145 void compute_page_skew(TO_BLOCK_LIST *blocks,  // list of blocks
00146                        float &page_m,          // average gradient
00147                        float &page_err);       // average error
00148 void vigorous_noise_removal(TO_BLOCK* block);
00149 void cleanup_rows_making(ICOORD page_tr,     // top right
00150                          TO_BLOCK *block,    // block to do
00151                          float gradient,     // gradient to fit
00152                          FCOORD rotation,    // for drawing
00153                          inT32 block_edge,   // edge of block
00154                          BOOL8 testing_on);  // correct orientation
00155 void delete_non_dropout_rows(                   //find lines
00156                              TO_BLOCK *block,   //block to do
00157                              float gradient,    //global skew
00158                              FCOORD rotation,   //deskew vector
00159                              inT32 block_edge,  //left edge
00160                              BOOL8 testing_on   //correct orientation
00161                             );
00162 BOOL8 find_best_dropout_row(                    //find neighbours
00163                             TO_ROW *row,        //row to test
00164                             inT32 distance,     //dropout dist
00165                             float dist_limit,   //threshold distance
00166                             inT32 line_index,   //index of row
00167                             TO_ROW_IT *row_it,  //current position
00168                             BOOL8 testing_on    //correct orientation
00169                            );
00170 TBOX deskew_block_coords(                  //block box
00171                         TO_BLOCK *block,  //block to do
00172                         float gradient    //global skew
00173                        );
00174 void compute_line_occupation(                    //project blobs
00175                              TO_BLOCK *block,    //block to do
00176                              float gradient,     //global skew
00177                              inT32 min_y,        //min coord in block
00178                              inT32 max_y,        //in block
00179                              inT32 *occupation,  //output projection
00180                              inT32 *deltas       //derivative
00181                             );
00182 void compute_occupation_threshold(                    //project blobs
00183                                   inT32 low_window,   //below result point
00184                                   inT32 high_window,  //above result point
00185                                   inT32 line_count,   //array sizes
00186                                   inT32 *occupation,  //input projection
00187                                   inT32 *thresholds   //output thresholds
00188                                  );
00189 void compute_dropout_distances(                    //project blobs
00190                                inT32 *occupation,  //input projection
00191                                inT32 *thresholds,  //output thresholds
00192                                inT32 line_count    //array sizes
00193                               );
00194 void expand_rows(                   //find lines
00195                  ICOORD page_tr,    //top right
00196                  TO_BLOCK *block,   //block to do
00197                  float gradient,    //gradient to fit
00198                  FCOORD rotation,   //for drawing
00199                  inT32 block_edge,  //edge of block
00200                  BOOL8 testing_on   //correct orientation
00201                 );
00202 void adjust_row_limits(                 //tidy limits
00203                        TO_BLOCK *block  //block to do
00204                       );
00205 void compute_row_stats(                  //find lines
00206                        TO_BLOCK *block,  //block to do
00207                        BOOL8 testing_on  //correct orientation
00208                       );
00209 float median_block_xheight(                  //find lines
00210                            TO_BLOCK *block,  //block to do
00211                            float gradient    //global skew
00212                           );
00213 
00214 int compute_xheight_from_modes(
00215     STATS *heights, STATS *floating_heights, bool cap_only, int min_height,
00216     int max_height, float *xheight, float *ascrise);
00217 
00218 inT32 compute_row_descdrop(TO_ROW *row,     // row to do
00219                            float gradient,  // global skew
00220                            int xheight_blob_count,
00221                            STATS *heights);
00222 inT32 compute_height_modes(STATS *heights,    // stats to search
00223                            inT32 min_height,  // bottom of range
00224                            inT32 max_height,  // top of range
00225                            inT32 *modes,      // output array
00226                            inT32 maxmodes);   // size of modes
00227 void correct_row_xheight(TO_ROW *row,    // row to fix
00228                          float xheight,  // average values
00229                          float ascrise,
00230                          float descdrop);
00231 void separate_underlines(TO_BLOCK *block,  // block to do
00232                          float gradient,   // skew angle
00233                          FCOORD rotation,  // inverse landscape
00234                          BOOL8 testing_on);  // correct orientation
00235 void pre_associate_blobs( ICOORD page_tr,   // top right
00236                          TO_BLOCK *block,  // block to do
00237                          FCOORD rotation,  // inverse landscape
00238                          BOOL8 testing_on);  // correct orientation
00239 void fit_parallel_rows(TO_BLOCK *block,   // block to do
00240                        float gradient,    // gradient to fit
00241                        FCOORD rotation,   // for drawing
00242                        inT32 block_edge,  // edge of block
00243                        BOOL8 testing_on);  // correct orientation
00244 void fit_parallel_lms(float gradient,  // forced gradient
00245                       TO_ROW *row);      // row to fit
00246 void make_baseline_spline(TO_ROW *row,     // row to fit
00247                           TO_BLOCK *block);  // block it came from
00248 BOOL8 segment_baseline (         //split baseline
00249 TO_ROW * row,                    //row to fit
00250 TO_BLOCK * block,                //block it came from
00251 inT32 & segments,                //no fo segments
00252 inT32 xstarts[]                  //coords of segments
00253 );
00254 double *linear_spline_baseline ( //split baseline
00255 TO_ROW * row,                    //row to fit
00256 TO_BLOCK * block,                //block it came from
00257 inT32 & segments,                //no fo segments
00258 inT32 xstarts[]                  //coords of segments
00259 );
00260 void assign_blobs_to_rows(                      //find lines
00261                           TO_BLOCK *block,      //block to do
00262                           float *gradient,      //block skew
00263                           int pass,             //identification
00264                           BOOL8 reject_misses,  //chuck big ones out
00265                           BOOL8 make_new_rows,  //add rows for unmatched
00266                           BOOL8 drawing_skew    //draw smoothed skew
00267                          );
00268                                  //find best row
00269 OVERLAP_STATE most_overlapping_row(TO_ROW_IT *row_it,  //iterator
00270                                    TO_ROW *&best_row,  //output row
00271                                    float top,          //top of blob
00272                                    float bottom,       //bottom of blob
00273                                    float rowsize,      //max row size
00274                                    BOOL8 testing_blob  //test stuff
00275                                   );
00276 int blob_x_order(                    //sort function
00277                  const void *item1,  //items to compare
00278                  const void *item2);
00279 int row_y_order(                    //sort function
00280                 const void *item1,  //items to compare
00281                 const void *item2);
00282 int row_spacing_order(                    //sort function
00283                       const void *item1,  //items to compare
00284                       const void *item2);
00285 
00286 void mark_repeated_chars(TO_ROW *row);
00287 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines