All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
findseam.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2  ********************************************************************************
3  *
4  * File: findseam.c (Formerly findseam.c)
5  * Description:
6  * Author: Mark Seaman, OCR Technology
7  * Created: Fri Oct 16 14:37:00 1987
8  * Modified: Tue Jul 30 15:44:59 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 "findseam.h"
29 #include "gradechop.h"
30 #include "plotedges.h"
31 #include "outlines.h"
32 #include "freelist.h"
33 #include "seam.h"
34 #include "wordrec.h"
35 
36 // Include automatically generated configuration file if running autoconf.
37 #ifdef HAVE_CONFIG_H
38 #include "config_auto.h"
39 #endif
40 
41 /*----------------------------------------------------------------------
42  T y p e s
43 ----------------------------------------------------------------------*/
44 #define SPLIT_CLOSENESS 20/* Difference in x value */
45  /* How many to keep */
46 #define MAX_NUM_SEAMS 150
47  /* How many to keep */
48 #define MAX_OLD_SEAMS 150
49 #define NO_FULL_PRIORITY -1/* Special marker for pri. */
50  /* Evalute right away */
51 #define BAD_PRIORITY 9999.0
52 
53 /*----------------------------------------------------------------------
54  F u n c t i o n s
55 ----------------------------------------------------------------------*/
56 namespace tesseract {
57 
58 /**********************************************************************
59  * add_seam_to_queue
60  *
61  * Adds the given new_seam to the seams priority queue, unless it is full
62  * and the new seam is worse than the worst.
63  **********************************************************************/
64 void Wordrec::add_seam_to_queue(float new_priority, SEAM *new_seam,
65  SeamQueue* seams) {
66  if (new_seam == NULL) return;
67  if (chop_debug) {
68  tprintf("Pushing new seam with priority %g :", new_priority);
69  new_seam->Print("seam: ");
70  }
71  if (seams->size() >= MAX_NUM_SEAMS) {
72  SeamPair old_pair(0, NULL);
73  if (seams->PopWorst(&old_pair) && old_pair.key() <= new_priority) {
74  if (chop_debug) {
75  tprintf("Old seam staying with priority %g\n", old_pair.key());
76  }
77  delete new_seam;
78  seams->Push(&old_pair);
79  return;
80  } else if (chop_debug) {
81  tprintf("New seam with priority %g beats old worst seam with %g\n",
82  new_priority, old_pair.key());
83  }
84  }
85  SeamPair new_pair(new_priority, new_seam);
86  seams->Push(&new_pair);
87 }
88 
89 
90 /**********************************************************************
91  * choose_best_seam
92  *
93  * Choose the best seam that can be created by assembling this a
94  * collection of splits. A queue of all the possible seams is
95  * maintained. Each new split received is placed in that queue with
96  * its partial priority value. These values in the seam queue are
97  * evaluated and combined until a good enough seam is found. If no
98  * further good seams are being found then this function returns to the
99  * caller, who will send more splits. If this function is called with
100  * a split of NULL, then no further splits can be supplied by the
101  * caller.
102  **********************************************************************/
103 void Wordrec::choose_best_seam(SeamQueue *seam_queue, const SPLIT *split,
104  PRIORITY priority, SEAM **seam_result,
105  TBLOB *blob, SeamPile *seam_pile) {
106  SEAM *seam;
107  char str[80];
108  float my_priority;
109  /* Add seam of split */
110  my_priority = priority;
111  if (split != NULL) {
112  TPOINT split_point = split->point1->pos;
113  split_point += split->point2->pos;
114  split_point /= 2;
115  seam = new SEAM(my_priority, split_point, *split);
116  if (chop_debug > 1) seam->Print("Partial priority ");
117  add_seam_to_queue(my_priority, seam, seam_queue);
118 
119  if (my_priority > chop_good_split)
120  return;
121  }
122 
123  TBOX bbox = blob->bounding_box();
124  /* Queue loop */
125  while (!seam_queue->empty()) {
126  SeamPair seam_pair;
127  seam_queue->Pop(&seam_pair);
128  seam = seam_pair.extract_data();
129  /* Set full priority */
130  my_priority = seam->FullPriority(bbox.left(), bbox.right(),
133  if (chop_debug) {
134  sprintf (str, "Full my_priority %0.0f, ", my_priority);
135  seam->Print(str);
136  }
137 
138  if ((*seam_result == NULL || (*seam_result)->priority() > my_priority) &&
139  my_priority < chop_ok_split) {
140  /* No crossing */
141  if (seam->IsHealthy(*blob, chop_min_outline_points,
143  delete *seam_result;
144  *seam_result = new SEAM(*seam);
145  (*seam_result)->set_priority(my_priority);
146  } else {
147  delete seam;
148  seam = NULL;
149  my_priority = BAD_PRIORITY;
150  }
151  }
152 
153  if (my_priority < chop_good_split) {
154  if (seam)
155  delete seam;
156  return; /* Made good answer */
157  }
158 
159  if (seam) {
160  /* Combine with others */
161  if (seam_pile->size() < chop_seam_pile_size) {
162  combine_seam(*seam_pile, seam, seam_queue);
163  SeamDecPair pair(seam_pair.key(), seam);
164  seam_pile->Push(&pair);
165  } else if (chop_new_seam_pile &&
166  seam_pile->size() == chop_seam_pile_size &&
167  seam_pile->PeekTop().key() > seam_pair.key()) {
168  combine_seam(*seam_pile, seam, seam_queue);
169  SeamDecPair pair;
170  seam_pile->Pop(&pair); // pop the worst.
171  // Replace the seam in pair (deleting the old one) with
172  // the new seam and score, then push back into the heap.
173  pair.set_key(seam_pair.key());
174  pair.set_data(seam);
175  seam_pile->Push(&pair);
176  } else {
177  delete seam;
178  }
179  }
180 
181  my_priority = seam_queue->empty() ? NO_FULL_PRIORITY
182  : seam_queue->PeekTop().key();
183  if ((my_priority > chop_ok_split) ||
184  (my_priority > chop_good_split && split))
185  return;
186  }
187 }
188 
189 
190 /**********************************************************************
191  * combine_seam
192  *
193  * Find other seams to combine with this one. The new seams that result
194  * from this union should be added to the seam queue. The return value
195  * tells whether or not any additional seams were added to the queue.
196  **********************************************************************/
197 void Wordrec::combine_seam(const SeamPile& seam_pile,
198  const SEAM* seam, SeamQueue* seam_queue) {
199  for (int x = 0; x < seam_pile.size(); ++x) {
200  const SEAM *this_one = seam_pile.get(x).data();
201  if (seam->CombineableWith(*this_one, SPLIT_CLOSENESS, chop_ok_split)) {
202  SEAM *new_one = new SEAM(*seam);
203  new_one->CombineWith(*this_one);
204  if (chop_debug > 1) new_one->Print("Combo priority ");
205  add_seam_to_queue(new_one->priority(), new_one, seam_queue);
206  }
207  }
208 }
209 
210 /**********************************************************************
211  * pick_good_seam
212  *
213  * Find and return a good seam that will split this blob into two pieces.
214  * Work from the outlines provided.
215  **********************************************************************/
217  SeamPile seam_pile(chop_seam_pile_size);
218  EDGEPT *points[MAX_NUM_POINTS];
219  EDGEPT_CLIST new_points;
220  SEAM *seam = NULL;
221  TESSLINE *outline;
222  inT16 num_points = 0;
223 
224 #ifndef GRAPHICS_DISABLED
225  if (chop_debug > 2)
226  wordrec_display_splits.set_value(true);
227 
228  draw_blob_edges(blob);
229 #endif
230 
231  PointHeap point_heap(MAX_NUM_POINTS);
232  for (outline = blob->outlines; outline; outline = outline->next)
233  prioritize_points(outline, &point_heap);
234 
235  while (!point_heap.empty() && num_points < MAX_NUM_POINTS) {
236  points[num_points++] = point_heap.PeekTop().data;
237  point_heap.Pop(NULL);
238  }
239 
240  /* Initialize queue */
241  SeamQueue seam_queue(MAX_NUM_SEAMS);
242 
243  try_point_pairs(points, num_points, &seam_queue, &seam_pile, &seam, blob);
244  try_vertical_splits(points, num_points, &new_points,
245  &seam_queue, &seam_pile, &seam, blob);
246 
247  if (seam == NULL) {
248  choose_best_seam(&seam_queue, NULL, BAD_PRIORITY, &seam, blob, &seam_pile);
249  } else if (seam->priority() > chop_good_split) {
250  choose_best_seam(&seam_queue, NULL, seam->priority(), &seam, blob,
251  &seam_pile);
252  }
253 
254  EDGEPT_C_IT it(&new_points);
255  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
256  EDGEPT *inserted_point = it.data();
257  if (seam == NULL || !seam->UsesPoint(inserted_point)) {
258  for (outline = blob->outlines; outline; outline = outline->next) {
259  if (outline->loop == inserted_point) {
260  outline->loop = outline->loop->next;
261  }
262  }
263  remove_edgept(inserted_point);
264  }
265  }
266 
267  if (seam) {
268  if (seam->priority() > chop_ok_split) {
269  delete seam;
270  seam = NULL;
271  }
272 #ifndef GRAPHICS_DISABLED
273  else if (wordrec_display_splits) {
274  seam->Mark(edge_window);
275  if (chop_debug > 2) {
278  }
279  }
280 #endif
281  }
282 
283  if (chop_debug)
284  wordrec_display_splits.set_value(false);
285 
286  return (seam);
287 }
288 
289 
290 /**********************************************************************
291  * try_point_pairs
292  *
293  * Try all the splits that are produced by pairing critical points
294  * together. See if any of them are suitable for use. Use a seam
295  * queue and seam pile that have already been initialized and used.
296  **********************************************************************/
298  inT16 num_points,
299  SeamQueue* seam_queue,
300  SeamPile* seam_pile,
301  SEAM ** seam,
302  TBLOB * blob) {
303  inT16 x;
304  inT16 y;
305  PRIORITY priority;
306 
307  for (x = 0; x < num_points; x++) {
308  for (y = x + 1; y < num_points; y++) {
309  if (points[y] &&
310  points[x]->WeightedDistance(*points[y], chop_x_y_weight) <
312  points[x] != points[y]->next && points[y] != points[x]->next &&
313  !is_exterior_point(points[x], points[y]) &&
314  !is_exterior_point(points[y], points[x])) {
315  SPLIT split(points[x], points[y]);
316  priority = partial_split_priority(&split);
317 
318  choose_best_seam(seam_queue, &split, priority, seam, blob, seam_pile);
319  }
320  }
321  }
322 }
323 
324 
325 /**********************************************************************
326  * try_vertical_splits
327  *
328  * Try all the splits that are produced by vertical projection to see
329  * if any of them are suitable for use. Use a seam queue and seam pile
330  * that have already been initialized and used.
331  * Return in new_points a collection of points that were inserted into
332  * the blob while examining vertical splits and which may safely be
333  * removed once a seam is chosen if they are not part of the seam.
334  **********************************************************************/
336  inT16 num_points,
337  EDGEPT_CLIST *new_points,
338  SeamQueue* seam_queue,
339  SeamPile* seam_pile,
340  SEAM ** seam,
341  TBLOB * blob) {
342  EDGEPT *vertical_point = NULL;
343  inT16 x;
344  PRIORITY priority;
345  TESSLINE *outline;
346 
347  for (x = 0; x < num_points; x++) {
348  vertical_point = NULL;
349  for (outline = blob->outlines; outline; outline = outline->next) {
350  vertical_projection_point(points[x], outline->loop,
351  &vertical_point, new_points);
352  }
353 
354  if (vertical_point && points[x] != vertical_point->next &&
355  vertical_point != points[x]->next &&
356  points[x]->WeightedDistance(*vertical_point, chop_x_y_weight) <
358  SPLIT split(points[x], vertical_point);
359  priority = partial_split_priority(&split);
360  choose_best_seam(seam_queue, &split, priority, seam, blob, seam_pile);
361  }
362  }
363 }
364 
365 }
#define NO_FULL_PRIORITY
Definition: findseam.cpp:49
Definition: blobs.h:261
bool Pop(Pair *entry)
Definition: genericheap.h:116
#define MAX_NUM_POINTS
Definition: chop.h:39
#define partial_split_priority(split)
Definition: gradechop.h:46
bool wordrec_display_splits
Definition: split.cpp:44
int chop_min_outline_points
Definition: wordrec.h:144
float FullPriority(int xmin, int xmax, double overlap_knob, int centered_maxwidth, double center_knob, double width_change_knob) const
Definition: seam.cpp:245
double chop_ok_split
Definition: wordrec.h:156
#define tprintf(...)
Definition: tprintf.h:31
int WeightedDistance(const EDGEPT &other, int x_factor) const
Definition: blobs.h:99
void Print(const char *label) const
Definition: seam.cpp:160
void remove_edgept(EDGEPT *point)
Definition: split.cpp:203
#define is_exterior_point(edge, point)
Definition: outlines.h:97
double chop_overlap_knob
Definition: wordrec.h:150
double chop_width_change_knob
Definition: wordrec.h:155
void Mark(ScrollView *window) const
Definition: seam.cpp:186
bool IsHealthy(const TBLOB &blob, int min_points, int min_area) const
Definition: seam.cpp:72
TESSLINE * next
Definition: blobs.h:258
double chop_good_split
Definition: wordrec.h:157
bool chop_new_seam_pile
Definition: wordrec.h:146
float PRIORITY
Definition: seam.h:42
const Key & key() const
Definition: kdpair.h:116
int chop_seam_pile_size
Definition: wordrec.h:145
inT16 right() const
Definition: rect.h:75
void add_seam_to_queue(float new_priority, SEAM *new_seam, SeamQueue *seams)
Definition: findseam.cpp:64
void vertical_projection_point(EDGEPT *split_point, EDGEPT *target_point, EDGEPT **best_point, EDGEPT_CLIST *new_points)
Definition: chop.cpp:274
void choose_best_seam(SeamQueue *seam_queue, const SPLIT *split, PRIORITY priority, SEAM **seam_result, TBLOB *blob, SeamPile *seam_pile)
Definition: findseam.cpp:103
bool CombineableWith(const SEAM &other, int max_x_dist, float max_total_priority) const
Definition: seam.cpp:46
#define update_edge_window()
Definition: plotedges.h:45
EDGEPT * point2
Definition: split.h:104
void try_point_pairs(EDGEPT *points[MAX_NUM_POINTS], inT16 num_points, SeamQueue *seam_queue, SeamPile *seam_pile, SEAM **seam, TBLOB *blob)
Definition: findseam.cpp:297
SEAM * pick_good_seam(TBLOB *blob)
Definition: findseam.cpp:216
#define MAX_NUM_SEAMS
Definition: findseam.cpp:46
inT16 left() const
Definition: rect.h:68
double chop_center_knob
Definition: wordrec.h:151
const Pair & get(int index) const
Definition: genericheap.h:87
int chop_centered_maxwidth
Definition: wordrec.h:153
void set_key(const Key &new_key)
Definition: kdpair.h:119
void CombineWith(const SEAM &other)
Definition: seam.cpp:60
Definition: blobs.h:50
EDGEPT * next
Definition: blobs.h:169
void set_data(Data *new_data)
Definition: kdpair.h:126
void Push(Pair *entry)
Definition: genericheap.h:95
const Pair & PeekTop() const
Definition: genericheap.h:108
#define SPLIT_CLOSENESS
Definition: findseam.cpp:44
bool empty() const
Definition: genericheap.h:68
float priority() const
Definition: seam.h:65
bool PopWorst(Pair *entry)
Definition: genericheap.h:138
bool UsesPoint(const EDGEPT *point) const
Definition: seam.h:88
void draw_blob_edges(TBLOB *blob)
Definition: plotedges.cpp:77
void prioritize_points(TESSLINE *outline, PointHeap *points)
Definition: chop.cpp:162
TPOINT pos
Definition: blobs.h:163
Definition: blobs.h:76
Definition: rect.h:30
void try_vertical_splits(EDGEPT *points[MAX_NUM_POINTS], inT16 num_points, EDGEPT_CLIST *new_points, SeamQueue *seam_queue, SeamPile *seam_pile, SEAM **seam, TBLOB *blob)
Definition: findseam.cpp:335
int chop_min_outline_area
Definition: wordrec.h:148
#define NULL
Definition: host.h:144
ScrollView * edge_window
Definition: plotedges.cpp:43
Definition: seam.h:44
TBOX bounding_box() const
Definition: blobs.cpp:482
TESSLINE * outlines
Definition: blobs.h:377
#define BAD_PRIORITY
Definition: findseam.cpp:51
EDGEPT * loop
Definition: blobs.h:257
Data * extract_data()
Definition: kdpair.h:131
#define edge_window_wait()
Definition: plotedges.h:57
EDGEPT * point1
Definition: split.h:103
Definition: split.h:37
void combine_seam(const SeamPile &seam_pile, const SEAM *seam, SeamQueue *seam_queue)
Definition: findseam.cpp:197
short inT16
Definition: host.h:100