Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gradechop.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2 ********************************************************************************
3 *
4 * File: gradechop.c (Formerly gradechop.c)
5 * Description:
6 * Author: Mark Seaman, OCR Technology
7 * Created: Fri Oct 16 14:37:00 1987
8 * Modified: Tue Jul 30 16:06:27 1991 (Mark Seaman) marks@hpgrlt
9 * Language: C
10 * Package: N/A
11 * Status: Reusable Software Component
12 *
13 * (c) Copyright 1987, Hewlett-Packard Company.
14 ** Licensed under the Apache License, Version 2.0 (the "License");
15 ** you may not use this file except in compliance with the License.
16 ** You may obtain a copy of the License at
17 ** http://www.apache.org/licenses/LICENSE-2.0
18 ** Unless required by applicable law or agreed to in writing, software
19 ** distributed under the License is distributed on an "AS IS" BASIS,
20 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 ** See the License for the specific language governing permissions and
22 ** limitations under the License.
23 *
24 *********************************************************************************/
25 /*----------------------------------------------------------------------
26 I n c l u d e s
27 ----------------------------------------------------------------------*/
28 #include "gradechop.h"
29 #include "wordrec.h"
30 #include "olutil.h"
31 #include "chop.h"
32 #include "ndminx.h"
33 #include <math.h>
34
35 /*----------------------------------------------------------------------
36 T y p e s
37 ----------------------------------------------------------------------*/
38 #define CENTER_GRADE_CAP 25.0
39
40 /*----------------------------------------------------------------------
41 M a c r o s
42 ----------------------------------------------------------------------*/
43 /**********************************************************************
44 * find_bounds_loop
45 *
46 * This is a macro to be used by set_outline_bounds.
47 **********************************************************************/
48
49 #define find_bounds_loop(point1,point2,x_min,x_max) \
50 x_min = point2->pos.x; \
51 x_max = point2->pos.x; \
52 \
53 this_point = point1; \
54 do { \
55 x_min = MIN (this_point->pos.x, x_min); \
56 x_max = MAX (this_point->pos.x, x_max); \
57 this_point = this_point->next; \
58 } \
59 while (this_point != point2 && this_point != point1) \
60
61
62 namespace tesseract {
63
64 /*----------------------------------------------------------------------
65 F u n c t i o n s
66 ----------------------------------------------------------------------*/
67 /**********************************************************************
68 * full_split_priority
69 *
70 * Assign a priority to this split based on the features that it has.
71 * Part of the priority has already been calculated so just return the
72 * additional amount for the bounding box type information.
73 **********************************************************************/
74 PRIORITY Wordrec::full_split_priority(SPLIT *split, inT16 xmin, inT16 xmax) {
75 BOUNDS_RECT rect;
76
77 set_outline_bounds (split->point1, split->point2, rect);
78
79 if (xmin < MIN (rect[0], rect[2]) && xmax > MAX (rect[1], rect[3]))
80 return (999.0);
81
82 return (grade_overlap (rect) +
83 grade_center_of_blob (rect) + grade_width_change (rect));
84 }
85
86
87 /**********************************************************************
88 * grade_center_of_blob
89 *
90 * Return a grade for the a split. Rank it on closeness to the center
91 * of the original blob
92 * 0 = "perfect"
93 * 100 = "no way jay"
94 **********************************************************************/
95 PRIORITY Wordrec::grade_center_of_blob(register BOUNDS_RECT rect) {
96 register PRIORITY grade;
97
98 grade = (rect[1] - rect[0]) - (rect[3] - rect[2]);
99 if (grade < 0)
100 grade = -grade;
101
102 grade *= chop_center_knob;
103 grade = MIN (CENTER_GRADE_CAP, grade);
104 return (MAX (0.0, grade));
105 }
106
107
108 /**********************************************************************
109 * grade_overlap
110 *
111 * Return a grade for this split for the overlap of the resultant blobs.
112 * 0 = "perfect"
113 * 100 = "no way jay"
114 **********************************************************************/
115 PRIORITY Wordrec::grade_overlap(register BOUNDS_RECT rect) {
116 register PRIORITY grade;
117 register inT16 width1;
118 register inT16 width2;
119 register inT16 overlap;
120
121 width1 = rect[3] - rect[2];
122 width2 = rect[1] - rect[0];
123
124 overlap = MIN (rect[1], rect[3]) - MAX (rect[0], rect[2]);
125 width1 = MIN (width1, width2);
126 if (overlap == width1)
127 return (100.0); /* Total overlap */
128
129 width1 = 2 * overlap - width1; /* Extra penalty for too */
130 overlap += MAX (0, width1); /* much overlap */
131
132 grade = overlap * chop_overlap_knob;
133
134 return (MAX (0.0, grade));
135 }
136
137
138 /**********************************************************************
139 * grade_split_length
140 *
141 * Return a grade for the length of this split.
142 * 0 = "perfect"
143 * 100 = "no way jay"
144 **********************************************************************/
145 PRIORITY Wordrec::grade_split_length(register SPLIT *split) {
146 register PRIORITY grade;
147 register float split_length;
148
149 split_length = weighted_edgept_dist (split->point1, split->point2,
150 chop_x_y_weight);
151
152 if (split_length <= 0)
153 grade = 0;
154 else
155 grade = sqrt (split_length) * chop_split_dist_knob;
156
157 return (MAX (0.0, grade));
158 }
159
160
161 /**********************************************************************
162 * grade_sharpness
163 *
164 * Return a grade for the sharpness of this split.
165 * 0 = "perfect"
166 * 100 = "no way jay"
167 **********************************************************************/
168 PRIORITY Wordrec::grade_sharpness(register SPLIT *split) {
169 register PRIORITY grade;
170
171 grade = point_priority (split->point1) + point_priority (split->point2);
172
173 if (grade < -360.0)
174 grade = 0;
175 else
176 grade += 360.0;
177
178 grade *= chop_sharpness_knob; /* Values 0 to -360 */
179
180 return (grade);
181 }
182
183
184 /**********************************************************************
185 * grade_width_change
186 *
187 * Return a grade for the change in width of the resultant blobs.
188 * 0 = "perfect"
189 * 100 = "no way jay"
190 **********************************************************************/
191 PRIORITY Wordrec::grade_width_change(register BOUNDS_RECT rect) {
192 register PRIORITY grade;
193 register inT32 width1;
194 register inT32 width2;
195
196 width1 = rect[3] - rect[2];
197 width2 = rect[1] - rect[0];
198
199 grade = 20 - (MAX (rect[1], rect[3])
200 - MIN (rect[0], rect[2]) - MAX (width1, width2));
201
202 grade *= chop_width_change_knob;
203
204 return (MAX (0.0, grade));
205 }
206
207
208 /**********************************************************************
209 * set_outline_bounds
210 *
211 * Set up the limits for the x coordinate of the outline.
212 **********************************************************************/
213 void Wordrec::set_outline_bounds(register EDGEPT *point1,
214 register EDGEPT *point2,
215 BOUNDS_RECT rect) {
216 register EDGEPT *this_point;
217 register inT16 x_min;
218 register inT16 x_max;
219
220 find_bounds_loop(point1, point2, x_min, x_max);
221
222 rect[0] = x_min;
223 rect[1] = x_max;
224
225 find_bounds_loop(point2, point1, x_min, x_max);
226
227 rect[2] = x_min;
228 rect[3] = x_max;
229 }
230
231 } // namespace tesseract