tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
makerow.h
1 /**********************************************************************
2  * File: makerow.h (Formerly makerows.h)
3  * Description: Code to arrange blobs into rows of text.
4  * Author: Ray Smith
5  *
6  * (C) Copyright 1992, Hewlett-Packard Ltd.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *
17  **********************************************************************/
18 
19 #ifndef MAKEROW_H
20 #define MAKEROW_H
21 
22 #include "params.h"
23 #include "ocrblock.h"
24 #include "blobs.h"
25 #include "blobbox.h"
26 #include "statistc.h"
27 
28 enum OVERLAP_STATE
29 {
30  ASSIGN, //assign it to row
31  REJECT, //reject it - dual overlap
32  NEW_ROW
33 };
34 
35 enum ROW_CATEGORY {
36  ROW_ASCENDERS_FOUND,
37  ROW_DESCENDERS_FOUND,
38  ROW_UNKNOWN,
39  ROW_INVALID,
40 };
41 
42 extern BOOL_VAR_H(textord_heavy_nr, FALSE, "Vigorously remove noise");
43 extern BOOL_VAR_H (textord_show_initial_rows, FALSE,
44 "Display row accumulation");
45 extern BOOL_VAR_H (textord_show_parallel_rows, FALSE,
46 "Display page correlated rows");
47 extern BOOL_VAR_H (textord_show_expanded_rows, FALSE,
48 "Display rows after expanding");
49 extern BOOL_VAR_H (textord_show_final_rows, FALSE,
50 "Display rows after final fitting");
51 extern BOOL_VAR_H (textord_show_final_blobs, FALSE,
52 "Display blob bounds after pre-ass");
53 extern BOOL_VAR_H (textord_test_landscape, FALSE, "Tests refer to land/port");
54 extern BOOL_VAR_H (textord_parallel_baselines, TRUE,
55 "Force parallel baselines");
56 extern BOOL_VAR_H (textord_straight_baselines, FALSE,
57 "Force straight baselines");
58 extern BOOL_VAR_H (textord_quadratic_baselines, FALSE,
59 "Use quadratic splines");
60 extern BOOL_VAR_H (textord_old_baselines, TRUE, "Use old baseline algorithm");
61 extern BOOL_VAR_H (textord_old_xheight, TRUE, "Use old xheight algorithm");
62 extern BOOL_VAR_H (textord_fix_xheight_bug, TRUE, "Use spline baseline");
63 extern BOOL_VAR_H (textord_fix_makerow_bug, TRUE,
64 "Prevent multiple baselines");
65 extern BOOL_VAR_H (textord_cblob_blockocc, TRUE,
66 "Use new projection for underlines");
67 extern BOOL_VAR_H (textord_debug_xheights, FALSE, "Test xheight algorithms");
68 extern INT_VAR_H (textord_test_x, -INT32_MAX, "coord of test pt");
69 extern INT_VAR_H (textord_test_y, -INT32_MAX, "coord of test pt");
70 extern INT_VAR_H (textord_min_blobs_in_row, 4,
71 "Min blobs before gradient counted");
72 extern INT_VAR_H (textord_spline_minblobs, 8,
73 "Min blobs in each spline segment");
74 extern INT_VAR_H (textord_spline_medianwin, 6,
75 "Size of window for spline segmentation");
76 extern INT_VAR_H (textord_min_xheight, 10, "Min credible pixel xheight");
77 extern double_VAR_H (textord_spline_shift_fraction, 0.02,
78 "Fraction of line spacing for quad");
79 extern double_VAR_H (textord_spline_outlier_fraction, 0.1,
80 "Fraction of line spacing for outlier");
81 extern double_VAR_H (textord_skew_ile, 0.5, "Ile of gradients for page skew");
82 extern double_VAR_H (textord_skew_lag, 0.75,
83 "Lag for skew on row accumulation");
84 extern double_VAR_H (textord_linespace_iqrlimit, 0.2,
85 "Max iqr/median for linespace");
86 extern double_VAR_H (textord_width_limit, 8,
87 "Max width of blobs to make rows");
88 extern double_VAR_H (textord_chop_width, 1.5, "Max width before chopping");
89 extern double_VAR_H (textord_minxh, 0.25,
90 "fraction of linesize for min xheight");
91 extern double_VAR_H (textord_min_linesize, 1.25,
92 "* blob height for initial linesize");
93 extern double_VAR_H (textord_excess_blobsize, 1.3,
94 "New row made if blob makes row this big");
95 extern double_VAR_H (textord_occupancy_threshold, 0.4,
96 "Fraction of neighbourhood");
97 extern double_VAR_H (textord_underline_width, 2.0,
98 "Multiple of line_size for underline");
99 extern double_VAR_H(textord_min_blob_height_fraction, 0.75,
100 "Min blob height/top to include blob top into xheight stats");
101 extern double_VAR_H (textord_xheight_mode_fraction, 0.4,
102 "Min pile height to make xheight");
103 extern double_VAR_H (textord_ascheight_mode_fraction, 0.15,
104 "Min pile height to make ascheight");
105 extern double_VAR_H (textord_ascx_ratio_min, 1.2, "Min cap/xheight");
106 extern double_VAR_H (textord_ascx_ratio_max, 1.7, "Max cap/xheight");
107 extern double_VAR_H (textord_descx_ratio_min, 0.15, "Min desc/xheight");
108 extern double_VAR_H (textord_descx_ratio_max, 0.6, "Max desc/xheight");
109 extern double_VAR_H (textord_xheight_error_margin, 0.1, "Accepted variation");
110 extern INT_VAR_H (textord_lms_line_trials, 12, "Number of linew fits to do");
111 extern BOOL_VAR_H (textord_new_initial_xheight, TRUE,
112 "Use test xheight mechanism");
113 extern BOOL_VAR_H(textord_debug_blob, FALSE, "Print test blob information");
114 
115 inline void get_min_max_xheight(int block_linesize,
116  int *min_height, int *max_height) {
117  *min_height = static_cast<int32_t>(floor(block_linesize * textord_minxh));
118  if (*min_height < textord_min_xheight) *min_height = textord_min_xheight;
119  *max_height = static_cast<int32_t>(ceil(block_linesize * 3.0));
120 }
121 
122 inline ROW_CATEGORY get_row_category(const TO_ROW *row) {
123  if (row->xheight <= 0) return ROW_INVALID;
124  return (row->ascrise > 0) ? ROW_ASCENDERS_FOUND :
125  (row->descdrop != 0) ? ROW_DESCENDERS_FOUND : ROW_UNKNOWN;
126 }
127 
128 inline bool within_error_margin(float test, float num, float margin) {
129  return (test >= num * (1 - margin) && test <= num * (1 + margin));
130 }
131 
132 void fill_heights(TO_ROW *row, float gradient, int min_height,
133  int max_height, STATS *heights, STATS *floating_heights);
134 
135 float make_single_row(ICOORD page_tr, bool allow_sub_blobs, TO_BLOCK* block,
136  TO_BLOCK_LIST* blocks);
137 float make_rows(ICOORD page_tr, // top right
138  TO_BLOCK_LIST *port_blocks);
139 void make_initial_textrows(ICOORD page_tr,
140  TO_BLOCK* block, // block to do
141  FCOORD rotation, // for drawing
142  bool testing_on); // correct orientation
143 void fit_lms_line(TO_ROW *row);
144 void compute_page_skew(TO_BLOCK_LIST *blocks, // list of blocks
145  float &page_m, // average gradient
146  float &page_err); // average error
147 void vigorous_noise_removal(TO_BLOCK* block);
148 void cleanup_rows_making(ICOORD page_tr, // top right
149  TO_BLOCK* block, // block to do
150  float gradient, // gradient to fit
151  FCOORD rotation, // for drawing
152  int32_t block_edge, // edge of block
153  bool testing_on); // correct orientation
154 void delete_non_dropout_rows( //find lines
155  TO_BLOCK* block, //block to do
156  float gradient, //global skew
157  FCOORD rotation, //deskew vector
158  int32_t block_edge, //left edge
159  bool testing_on //correct orientation
160 );
161 bool find_best_dropout_row( //find neighbours
162  TO_ROW* row, //row to test
163  int32_t distance, //dropout dist
164  float dist_limit, //threshold distance
165  int32_t line_index, //index of row
166  TO_ROW_IT* row_it, //current position
167  bool testing_on //correct orientation
168 );
169 TBOX deskew_block_coords( //block box
170  TO_BLOCK *block, //block to do
171  float gradient //global skew
172  );
173 void compute_line_occupation( //project blobs
174  TO_BLOCK *block, //block to do
175  float gradient, //global skew
176  int32_t min_y, //min coord in block
177  int32_t max_y, //in block
178  int32_t *occupation, //output projection
179  int32_t *deltas //derivative
180  );
181 void compute_occupation_threshold( //project blobs
182  int32_t low_window, //below result point
183  int32_t high_window, //above result point
184  int32_t line_count, //array sizes
185  int32_t *occupation, //input projection
186  int32_t *thresholds //output thresholds
187  );
188 void compute_dropout_distances( //project blobs
189  int32_t *occupation, //input projection
190  int32_t *thresholds, //output thresholds
191  int32_t line_count //array sizes
192  );
193 void expand_rows( //find lines
194  ICOORD page_tr, //top right
195  TO_BLOCK* block, //block to do
196  float gradient, //gradient to fit
197  FCOORD rotation, //for drawing
198  int32_t block_edge, //edge of block
199  bool testing_on //correct orientation
200 );
201 void adjust_row_limits( //tidy limits
202  TO_BLOCK *block //block to do
203  );
204 void compute_row_stats( //find lines
205  TO_BLOCK* block, //block to do
206  bool testing_on //correct orientation
207 );
208 float median_block_xheight( //find lines
209  TO_BLOCK *block, //block to do
210  float gradient //global skew
211  );
212 
213 int compute_xheight_from_modes(
214  STATS *heights, STATS *floating_heights, bool cap_only, int min_height,
215  int max_height, float *xheight, float *ascrise);
216 
217 int32_t compute_row_descdrop(TO_ROW *row, // row to do
218  float gradient, // global skew
219  int xheight_blob_count,
220  STATS *heights);
221 int32_t compute_height_modes(STATS *heights, // stats to search
222  int32_t min_height, // bottom of range
223  int32_t max_height, // top of range
224  int32_t *modes, // output array
225  int32_t maxmodes); // size of modes
226 void correct_row_xheight(TO_ROW *row, // row to fix
227  float xheight, // average values
228  float ascrise,
229  float descdrop);
230 void separate_underlines(TO_BLOCK* block, // block to do
231  float gradient, // skew angle
232  FCOORD rotation, // inverse landscape
233  bool testing_on); // correct orientation
234 void pre_associate_blobs(ICOORD page_tr, // top right
235  TO_BLOCK* block, // block to do
236  FCOORD rotation, // inverse landscape
237  bool testing_on); // correct orientation
238 void fit_parallel_rows(TO_BLOCK* block, // block to do
239  float gradient, // gradient to fit
240  FCOORD rotation, // for drawing
241  int32_t block_edge, // edge of block
242  bool testing_on); // correct orientation
243 void fit_parallel_lms(float gradient, // forced gradient
244  TO_ROW *row); // row to fit
245 void make_baseline_spline(TO_ROW *row, // row to fit
246  TO_BLOCK *block); // block it came from
247 bool segment_baseline( //split baseline
248  TO_ROW* row, //row to fit
249  TO_BLOCK* block, //block it came from
250  int32_t& segments, //no fo segments
251  int32_t* xstarts //coords of segments
252 );
253 double *linear_spline_baseline ( //split baseline
254 TO_ROW * row, //row to fit
255 TO_BLOCK * block, //block it came from
256 int32_t & segments, //no fo segments
257 int32_t xstarts[] //coords of segments
258 );
259 void assign_blobs_to_rows( //find lines
260  TO_BLOCK* block, //block to do
261  float* gradient, //block skew
262  int pass, //identification
263  bool reject_misses, //chuck big ones out
264  bool make_new_rows, //add rows for unmatched
265  bool drawing_skew //draw smoothed skew
266 );
267  //find best row
268 OVERLAP_STATE most_overlapping_row(TO_ROW_IT* row_it, //iterator
269  TO_ROW*& best_row, //output row
270  float top, //top of blob
271  float bottom, //bottom of blob
272  float rowsize, //max row size
273  bool testing_blob //test stuff
274  );
275 int blob_x_order( //sort function
276  const void *item1, //items to compare
277  const void *item2);
278 int row_y_order( //sort function
279  const void *item1, //items to compare
280  const void *item2);
281 int row_spacing_order( //sort function
282  const void *item1, //items to compare
283  const void *item2);
284 
285 void mark_repeated_chars(TO_ROW *row);
286 #endif
Definition: rect.h:34
float ascrise
Definition: blobbox.h:672
Definition: blobbox.h:556
Definition: statistc.h:33
integer coordinate
Definition: points.h:32
float descdrop
Definition: blobbox.h:673
Definition: blobbox.h:705
Definition: points.h:189
float xheight
Definition: blobbox.h:670