tesseract  5.0.0-alpha-619-ge9db
makerow.h
Go to the documentation of this file.
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 
40 };
41 
42 extern BOOL_VAR_H(textord_heavy_nr, false, "Vigorously remove noise");
44 "Display row accumulation");
46 "Display page correlated rows");
48 "Display rows after expanding");
49 extern BOOL_VAR_H (textord_show_final_rows, false,
50 "Display rows after final fitting");
52 "Display blob bounds after pre-ass");
53 extern BOOL_VAR_H (textord_test_landscape, false, "Tests refer to land/port");
55 "Force parallel baselines");
57 "Force straight baselines");
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");
64 "Prevent multiple baselines");
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");
71 "Min blobs before gradient counted");
73 "Min blobs in each spline segment");
75 "Size of window for spline segmentation");
76 extern INT_VAR_H (textord_min_xheight, 10, "Min credible pixel xheight");
78 "Fraction of line spacing for quad");
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");
85 "Max iqr/median for linespace");
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");
92 "* blob height for initial linesize");
94 "New row made if blob makes row this big");
96 "Fraction of neighbourhood");
98 "Multiple of line_size for underline");
100 "Min blob height/top to include blob top into xheight stats");
102 "Min pile height to make xheight");
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");
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 
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
compute_page_skew
void compute_page_skew(TO_BLOCK_LIST *blocks, float &page_m, float &page_err)
Definition: makerow.cpp:286
textord_old_xheight
bool textord_old_xheight
Definition: makerow.cpp:52
textord_width_limit
double textord_width_limit
Definition: makerow.cpp:75
textord_ascx_ratio_max
double textord_ascx_ratio_max
Definition: makerow.cpp:95
vigorous_noise_removal
void vigorous_noise_removal(TO_BLOCK *block)
Definition: makerow.cpp:466
compute_line_occupation
void compute_line_occupation(TO_BLOCK *block, float gradient, int32_t min_y, int32_t max_y, int32_t *occupation, int32_t *deltas)
Definition: makerow.cpp:768
pre_associate_blobs
void pre_associate_blobs(ICOORD page_tr, TO_BLOCK *block, FCOORD rotation, bool testing_on)
Definition: makerow.cpp:1845
textord_lms_line_trials
int textord_lms_line_trials
Definition: makerow.cpp:99
textord_spline_shift_fraction
double textord_spline_shift_fraction
Definition: makerow.cpp:69
textord_chop_width
double textord_chop_width
Definition: makerow.cpp:76
textord_test_x
int textord_test_x
Definition: makerow.cpp:60
textord_quadratic_baselines
bool textord_quadratic_baselines
textord_debug_xheights
bool textord_debug_xheights
Definition: makerow.cpp:55
most_overlapping_row
OVERLAP_STATE most_overlapping_row(TO_ROW_IT *row_it, TO_ROW *&best_row, float top, float bottom, float rowsize, bool testing_blob)
Definition: makerow.cpp:2478
textord_xheight_mode_fraction
double textord_xheight_mode_fraction
Definition: makerow.cpp:89
textord_xheight_error_margin
double textord_xheight_error_margin
Definition: makerow.cpp:98
make_single_row
float make_single_row(ICOORD page_tr, bool allow_sub_blobs, TO_BLOCK *block, TO_BLOCK_LIST *blocks)
Definition: makerow.cpp:163
ROW_DESCENDERS_FOUND
Definition: makerow.h:36
compute_occupation_threshold
void compute_occupation_threshold(int32_t low_window, int32_t high_window, int32_t line_count, int32_t *occupation, int32_t *thresholds)
Definition: makerow.cpp:821
blobbox.h
params.h
make_initial_textrows
void make_initial_textrows(ICOORD page_tr, TO_BLOCK *block, FCOORD rotation, bool testing_on)
Definition: makerow.cpp:226
compute_row_descdrop
int32_t compute_row_descdrop(TO_ROW *row, float gradient, int xheight_blob_count, STATS *heights)
Definition: makerow.cpp:1563
ICOORD
integer coordinate
Definition: points.h:30
textord_heavy_nr
bool textord_heavy_nr
Definition: makerow.cpp:42
textord_excess_blobsize
double textord_excess_blobsize
Definition: makerow.cpp:83
textord_ascheight_mode_fraction
double textord_ascheight_mode_fraction
Definition: makerow.cpp:91
TO_BLOCK
Definition: blobbox.h:691
textord_descx_ratio_max
double textord_descx_ratio_max
Definition: makerow.cpp:97
correct_row_xheight
void correct_row_xheight(TO_ROW *row, float xheight, float ascrise, float descdrop)
Definition: makerow.cpp:1685
fit_parallel_lms
void fit_parallel_lms(float gradient, TO_ROW *row)
Definition: makerow.cpp:1973
textord_show_parallel_rows
bool textord_show_parallel_rows
Definition: makerow.cpp:44
ROW_ASCENDERS_FOUND
Definition: makerow.h:35
find_best_dropout_row
bool find_best_dropout_row(TO_ROW *row, int32_t distance, float dist_limit, int32_t line_index, TO_ROW_IT *row_it, bool testing_on)
Definition: makerow.cpp:652
ROW_INVALID
Definition: makerow.h:38
textord_min_blobs_in_row
int textord_min_blobs_in_row
Definition: makerow.cpp:62
FCOORD
Definition: points.h:187
blobs.h
mark_repeated_chars
void mark_repeated_chars(TO_ROW *row)
Definition: makerow.cpp:2639
deskew_block_coords
TBOX deskew_block_coords(TO_BLOCK *block, float gradient)
Definition: makerow.cpp:732
OVERLAP_STATE
OVERLAP_STATE
Definition: makerow.h:27
double_VAR_H
#define double_VAR_H(name, val, comment)
Definition: params.h:298
textord_descx_ratio_min
double textord_descx_ratio_min
Definition: makerow.cpp:96
textord_straight_baselines
bool textord_straight_baselines
Definition: makerow.cpp:50
textord_debug_blob
bool textord_debug_blob
Definition: makerow.cpp:101
row_spacing_order
int row_spacing_order(const void *item1, const void *item2)
Definition: makerow.cpp:2617
textord_new_initial_xheight
bool textord_new_initial_xheight
Definition: makerow.cpp:100
adjust_row_limits
void adjust_row_limits(TO_BLOCK *block)
Definition: makerow.cpp:1107
get_row_category
ROW_CATEGORY get_row_category(const TO_ROW *row)
Definition: makerow.h:121
statistc.h
fit_parallel_rows
void fit_parallel_rows(TO_BLOCK *block, float gradient, FCOORD rotation, int32_t block_edge, bool testing_on)
Definition: makerow.cpp:1931
textord_min_xheight
int textord_min_xheight
Definition: makerow.cpp:67
ROW_CATEGORY
ROW_CATEGORY
Definition: makerow.h:34
expand_rows
void expand_rows(ICOORD page_tr, TO_BLOCK *block, float gradient, FCOORD rotation, int32_t block_edge, bool testing_on)
Definition: makerow.cpp:949
textord_ascx_ratio_min
double textord_ascx_ratio_min
Definition: makerow.cpp:94
blob_x_order
int blob_x_order(const void *item1, const void *item2)
Definition: makerow.cpp:2573
fill_heights
void fill_heights(TO_ROW *row, float gradient, int min_height, int max_height, STATS *heights, STATS *floating_heights)
Definition: makerow.cpp:1406
compute_xheight_from_modes
int compute_xheight_from_modes(STATS *heights, STATS *floating_heights, bool cap_only, int min_height, int max_height, float *xheight, float *ascrise)
Definition: makerow.cpp:1467
row_y_order
int row_y_order(const void *item1, const void *item2)
Definition: makerow.cpp:2595
within_error_margin
bool within_error_margin(float test, float num, float margin)
Definition: makerow.h:127
NEW_ROW
Definition: makerow.h:46
textord_parallel_baselines
bool textord_parallel_baselines
Definition: makerow.cpp:49
textord_skew_lag
double textord_skew_lag
Definition: makerow.cpp:73
textord_min_linesize
double textord_min_linesize
Definition: makerow.cpp:81
assign_blobs_to_rows
void assign_blobs_to_rows(TO_BLOCK *block, float *gradient, int pass, bool reject_misses, bool make_new_rows, bool drawing_skew)
Definition: makerow.cpp:2278
compute_row_stats
void compute_row_stats(TO_BLOCK *block, bool testing_on)
Definition: makerow.cpp:1143
TO_ROW::xheight
float xheight
Definition: blobbox.h:656
distance
UnicodeText::const_iterator::difference_type distance(const UnicodeText::const_iterator &first, const UnicodeText::const_iterator &last)
Definition: unicodetext.cc:44
textord_old_baselines
bool textord_old_baselines
Definition: makerow.cpp:51
INT_VAR_H
#define INT_VAR_H(name, val, comment)
Definition: params.h:292
textord_test_landscape
bool textord_test_landscape
Definition: makerow.cpp:48
textord_spline_outlier_fraction
double textord_spline_outlier_fraction
Definition: makerow.cpp:71
STATS
Definition: statistc.h:30
textord_skew_ile
double textord_skew_ile
Definition: makerow.cpp:72
get_min_max_xheight
void get_min_max_xheight(int block_linesize, int *min_height, int *max_height)
Definition: makerow.h:114
ROW_UNKNOWN
Definition: makerow.h:37
textord_fix_makerow_bug
bool textord_fix_makerow_bug
Definition: makerow.cpp:54
textord_spline_medianwin
int textord_spline_medianwin
Definition: makerow.cpp:64
segment_baseline
bool segment_baseline(TO_ROW *row, TO_BLOCK *block, int32_t &segments, int32_t *xstarts)
Definition: makerow.cpp:2088
textord_spline_minblobs
int textord_spline_minblobs
Definition: makerow.cpp:63
BOOL_VAR_H
#define BOOL_VAR_H(name, val, comment)
Definition: params.h:294
textord_test_y
int textord_test_y
Definition: makerow.cpp:61
ocrblock.h
make_rows
float make_rows(ICOORD page_tr, TO_BLOCK_LIST *port_blocks)
Definition: makerow.cpp:200
textord_show_final_blobs
bool textord_show_final_blobs
Definition: makerow.cpp:47
median_block_xheight
float median_block_xheight(TO_BLOCK *block, float gradient)
TO_ROW
Definition: blobbox.h:543
textord_show_expanded_rows
bool textord_show_expanded_rows
Definition: makerow.cpp:45
delete_non_dropout_rows
void delete_non_dropout_rows(TO_BLOCK *block, float gradient, FCOORD rotation, int32_t block_edge, bool testing_on)
Definition: makerow.cpp:570
TO_ROW::ascrise
float ascrise
Definition: blobbox.h:658
fit_lms_line
void fit_lms_line(TO_ROW *row)
Definition: makerow.cpp:266
compute_dropout_distances
void compute_dropout_distances(int32_t *occupation, int32_t *thresholds, int32_t line_count)
Definition: makerow.cpp:902
textord_show_final_rows
bool textord_show_final_rows
Definition: makerow.cpp:46
textord_minxh
double textord_minxh
Definition: makerow.cpp:80
TO_ROW::descdrop
float descdrop
Definition: blobbox.h:659
separate_underlines
void separate_underlines(TO_BLOCK *block, float gradient, FCOORD rotation, bool testing_on)
Definition: makerow.cpp:1772
textord_cblob_blockocc
bool textord_cblob_blockocc
textord_show_initial_rows
bool textord_show_initial_rows
Definition: makerow.cpp:43
textord_underline_width
double textord_underline_width
Definition: makerow.cpp:85
textord_min_blob_height_fraction
double textord_min_blob_height_fraction
Definition: makerow.cpp:87
cleanup_rows_making
void cleanup_rows_making(ICOORD page_tr, TO_BLOCK *block, float gradient, FCOORD rotation, int32_t block_edge, bool testing_on)
Definition: makerow.cpp:517
linear_spline_baseline
double * linear_spline_baseline(TO_ROW *row, TO_BLOCK *block, int32_t &segments, int32_t xstarts[])
Definition: makerow.cpp:2187
textord_fix_xheight_bug
bool textord_fix_xheight_bug
Definition: makerow.cpp:53
ASSIGN
Definition: makerow.h:44
REJECT
Definition: makerow.h:45
compute_height_modes
int32_t compute_height_modes(STATS *heights, int32_t min_height, int32_t max_height, int32_t *modes, int32_t maxmodes)
Definition: makerow.cpp:1623
textord_linespace_iqrlimit
double textord_linespace_iqrlimit
Definition: makerow.cpp:74
TBOX
Definition: rect.h:33
make_baseline_spline
void make_baseline_spline(TO_ROW *row, TO_BLOCK *block)
Definition: makerow.cpp:2056
textord_occupancy_threshold
double textord_occupancy_threshold
Definition: makerow.cpp:84