tesseract
3.03
|
00001 00002 // File: TabFind.cpp 00003 // Description: Subclass of BBGrid to find vertically aligned blobs. 00004 // Author: Ray Smith 00005 // Created: Fri Mar 21 15:03:01 PST 2008 00006 // 00007 // (C) Copyright 2008, Google Inc. 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 // 00019 00020 #ifdef HAVE_CONFIG_H 00021 #include "config_auto.h" 00022 #endif 00023 00024 #include "tabfind.h" 00025 #include "alignedblob.h" 00026 #include "blobbox.h" 00027 #include "colpartitiongrid.h" 00028 #include "detlinefit.h" 00029 #include "linefind.h" 00030 #include "ndminx.h" 00031 00032 namespace tesseract { 00033 00034 // Multiple of box size to search for initial gaps. 00035 const int kTabRadiusFactor = 5; 00036 // Min and Max multiple of height to search vertically when extrapolating. 00037 const int kMinVerticalSearch = 3; 00038 const int kMaxVerticalSearch = 12; 00039 const int kMaxRaggedSearch = 25; 00040 // Minimum number of lines in a column width to make it interesting. 00041 const int kMinLinesInColumn = 10; 00042 // Minimum width of a column to be interesting. 00043 const int kMinColumnWidth = 200; 00044 // Minimum fraction of total column lines for a column to be interesting. 00045 const double kMinFractionalLinesInColumn = 0.125; 00046 // Fraction of height used as alignment tolerance for aligned tabs. 00047 const double kAlignedFraction = 0.03125; 00048 // Minimum gutter width in absolute inch (multiplied by resolution) 00049 const double kMinGutterWidthAbsolute = 0.02; 00050 // Maximum gutter width (in absolute inch) that we care about 00051 const double kMaxGutterWidthAbsolute = 2.00; 00052 // Multiplier of gridsize for min gutter width of TT_MAYBE_RAGGED blobs. 00053 const int kRaggedGutterMultiple = 5; 00054 // Min aspect ratio of tall objects to be considered a separator line. 00055 // (These will be ignored in searching the gutter for obstructions.) 00056 const double kLineFragmentAspectRatio = 10.0; 00057 // Multiplier of new y positions in running average for skew estimation. 00058 const double kSmoothFactor = 0.25; 00059 // Min coverage for a good baseline between vectors 00060 const double kMinBaselineCoverage = 0.5; 00061 // Minimum overlap fraction when scanning text lines for column widths. 00062 const double kCharVerticalOverlapFraction = 0.375; 00063 // Maximum horizontal gap allowed when scanning for column widths 00064 const double kMaxHorizontalGap = 3.0; 00065 // Maximum upper quartile error allowed on a baseline fit as a fraction 00066 // of height. 00067 const double kMaxBaselineError = 0.4375; 00068 // Min number of points to accept after evaluation. 00069 const int kMinEvaluatedTabs = 3; 00070 // Minimum aspect ratio of a textline to make a good textline blob with a 00071 // single blob. 00072 const int kMaxTextLineBlobRatio = 5; 00073 // Minimum aspect ratio of a textline to make a good textline blob with 00074 // multiple blobs. Target ratio varies according to number of blobs. 00075 const int kMinTextLineBlobRatio = 3; 00076 // Fraction of box area covered by image to make a blob image. 00077 const double kMinImageArea = 0.5; 00078 // Upto 30 degrees is allowed for rotations of diacritic blobs. 00079 // Keep this value slightly larger than kCosSmallAngle in blobbox.cpp 00080 // so that the assert there never fails. 00081 const double kCosMaxSkewAngle = 0.866025; 00082 00083 BOOL_VAR(textord_tabfind_show_initialtabs, false, "Show tab candidates"); 00084 BOOL_VAR(textord_tabfind_show_finaltabs, false, "Show tab vectors"); 00085 double_VAR(textord_tabfind_aligned_gap_fraction, 0.75, 00086 "Fraction of height used as a minimum gap for aligned blobs."); 00087 00088 TabFind::TabFind(int gridsize, const ICOORD& bleft, const ICOORD& tright, 00089 TabVector_LIST* vlines, int vertical_x, int vertical_y, 00090 int resolution) 00091 : AlignedBlob(gridsize, bleft, tright), 00092 resolution_(resolution), 00093 image_origin_(0, tright.y() - 1) { 00094 width_cb_ = NULL; 00095 v_it_.set_to_list(&vectors_); 00096 v_it_.add_list_after(vlines); 00097 SetVerticalSkewAndParellelize(vertical_x, vertical_y); 00098 width_cb_ = NewPermanentTessCallback(this, &TabFind::CommonWidth); 00099 } 00100 00101 TabFind::~TabFind() { 00102 if (width_cb_ != NULL) 00103 delete width_cb_; 00104 } 00105 00107 00108 // Insert a list of blobs into the given grid (not necessarily this). 00109 // If take_ownership is true, then the blobs are removed from the source list. 00110 // See InsertBlob for the other arguments. 00111 // It would seem to make more sense to swap this and grid, but this way 00112 // around allows grid to not be derived from TabFind, eg a ColPartitionGrid, 00113 // while the grid that provides the tab stops(this) has to be derived from 00114 // TabFind. 00115 void TabFind::InsertBlobsToGrid(bool h_spread, bool v_spread, 00116 BLOBNBOX_LIST* blobs, 00117 BBGrid<BLOBNBOX, BLOBNBOX_CLIST, 00118 BLOBNBOX_C_IT>* grid) { 00119 BLOBNBOX_IT blob_it(blobs); 00120 int b_count = 0; 00121 int reject_count = 0; 00122 for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) { 00123 BLOBNBOX* blob = blob_it.data(); 00124 // if (InsertBlob(true, true, blob, grid)) { 00125 if (InsertBlob(h_spread, v_spread, blob, grid)) { 00126 ++b_count; 00127 } else { 00128 ++reject_count; 00129 } 00130 } 00131 if (textord_debug_tabfind) { 00132 tprintf("Inserted %d blobs into grid, %d rejected.\n", 00133 b_count, reject_count); 00134 } 00135 } 00136 00137 // Insert a single blob into the given grid (not necessarily this). 00138 // If h_spread, then all cells covered horizontally by the box are 00139 // used, otherwise, just the bottom-left. Similarly for v_spread. 00140 // A side effect is that the left and right rule edges of the blob are 00141 // set according to the tab vectors in this (not grid). 00142 bool TabFind::InsertBlob(bool h_spread, bool v_spread, BLOBNBOX* blob, 00143 BBGrid<BLOBNBOX, BLOBNBOX_CLIST, 00144 BLOBNBOX_C_IT>* grid) { 00145 TBOX box = blob->bounding_box(); 00146 blob->set_left_rule(LeftEdgeForBox(box, false, false)); 00147 blob->set_right_rule(RightEdgeForBox(box, false, false)); 00148 blob->set_left_crossing_rule(LeftEdgeForBox(box, true, false)); 00149 blob->set_right_crossing_rule(RightEdgeForBox(box, true, false)); 00150 if (blob->joined_to_prev()) 00151 return false; 00152 grid->InsertBBox(h_spread, v_spread, blob); 00153 return true; 00154 } 00155 00156 // Calls SetBlobRuleEdges for all the blobs in the given block. 00157 void TabFind::SetBlockRuleEdges(TO_BLOCK* block) { 00158 SetBlobRuleEdges(&block->blobs); 00159 SetBlobRuleEdges(&block->small_blobs); 00160 SetBlobRuleEdges(&block->noise_blobs); 00161 SetBlobRuleEdges(&block->large_blobs); 00162 } 00163 00164 // Sets the left and right rule and crossing_rules for the blobs in the given 00165 // list by fiding the next outermost tabvectors for each blob. 00166 void TabFind::SetBlobRuleEdges(BLOBNBOX_LIST* blobs) { 00167 BLOBNBOX_IT blob_it(blobs); 00168 for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) { 00169 BLOBNBOX* blob = blob_it.data(); 00170 TBOX box = blob->bounding_box(); 00171 blob->set_left_rule(LeftEdgeForBox(box, false, false)); 00172 blob->set_right_rule(RightEdgeForBox(box, false, false)); 00173 blob->set_left_crossing_rule(LeftEdgeForBox(box, true, false)); 00174 blob->set_right_crossing_rule(RightEdgeForBox(box, true, false)); 00175 } 00176 } 00177 00178 // Returns the gutter width of the given TabVector between the given y limits. 00179 // Also returns x-shift to be added to the vector to clear any intersecting 00180 // blobs. The shift is deducted from the returned gutter. 00181 // If ignore_unmergeables is true, then blobs of UnMergeableType are 00182 // ignored as if they don't exist. (Used for text on image.) 00183 // max_gutter_width is used as the maximum width worth searching for in case 00184 // there is nothing near the TabVector. 00185 int TabFind::GutterWidth(int bottom_y, int top_y, const TabVector& v, 00186 bool ignore_unmergeables, int max_gutter_width, 00187 int* required_shift) { 00188 bool right_to_left = v.IsLeftTab(); 00189 int bottom_x = v.XAtY(bottom_y); 00190 int top_x = v.XAtY(top_y); 00191 int start_x = right_to_left ? MAX(top_x, bottom_x) : MIN(top_x, bottom_x); 00192 BlobGridSearch sidesearch(this); 00193 sidesearch.StartSideSearch(start_x, bottom_y, top_y); 00194 int min_gap = max_gutter_width; 00195 *required_shift = 0; 00196 BLOBNBOX* blob = NULL; 00197 while ((blob = sidesearch.NextSideSearch(right_to_left)) != NULL) { 00198 const TBOX& box = blob->bounding_box(); 00199 if (box.bottom() >= top_y || box.top() <= bottom_y) 00200 continue; // Doesn't overlap enough. 00201 if (box.height() >= gridsize() * 2 && 00202 box.height() > box.width() * kLineFragmentAspectRatio) { 00203 // Skip likely separator line residue. 00204 continue; 00205 } 00206 if (ignore_unmergeables && BLOBNBOX::UnMergeableType(blob->region_type())) 00207 continue; // Skip non-text if required. 00208 int mid_y = (box.bottom() + box.top()) / 2; 00209 // We use the x at the mid-y so that the required_shift guarantees 00210 // to clear all the blobs on the tab-stop. If we use the min/max 00211 // of x at top/bottom of the blob, then exactness would be required, 00212 // which is not a good thing. 00213 int tab_x = v.XAtY(mid_y); 00214 int gap; 00215 if (right_to_left) { 00216 gap = tab_x - box.right(); 00217 if (gap < 0 && box.left() - tab_x < *required_shift) 00218 *required_shift = box.left() - tab_x; 00219 } else { 00220 gap = box.left() - tab_x; 00221 if (gap < 0 && box.right() - tab_x > *required_shift) 00222 *required_shift = box.right() - tab_x; 00223 } 00224 if (gap > 0 && gap < min_gap) 00225 min_gap = gap; 00226 } 00227 // Result may be negative, in which case, this is a really bad tabstop. 00228 return min_gap - abs(*required_shift); 00229 } 00230 00231 // Find the gutter width and distance to inner neighbour for the given blob. 00232 void TabFind::GutterWidthAndNeighbourGap(int tab_x, int mean_height, 00233 int max_gutter, bool left, 00234 BLOBNBOX* bbox, int* gutter_width, 00235 int* neighbour_gap ) { 00236 const TBOX& box = bbox->bounding_box(); 00237 // The gutter and internal sides of the box. 00238 int gutter_x = left ? box.left() : box.right(); 00239 int internal_x = left ? box.right() : box.left(); 00240 // On ragged edges, the gutter side of the box is away from the tabstop. 00241 int tab_gap = left ? gutter_x - tab_x : tab_x - gutter_x; 00242 *gutter_width = max_gutter; 00243 // If the box is away from the tabstop, we need to increase 00244 // the allowed gutter width. 00245 if (tab_gap > 0) 00246 *gutter_width += tab_gap; 00247 bool debug = WithinTestRegion(2, box.left(), box.bottom()); 00248 if (debug) 00249 tprintf("Looking in gutter\n"); 00250 // Find the nearest blob on the outside of the column. 00251 BLOBNBOX* gutter_bbox = AdjacentBlob(bbox, left, 00252 bbox->flow() == BTFT_TEXT_ON_IMAGE, 0.0, 00253 *gutter_width, box.top(), box.bottom()); 00254 if (gutter_bbox != NULL) { 00255 TBOX gutter_box = gutter_bbox->bounding_box(); 00256 *gutter_width = left ? tab_x - gutter_box.right() 00257 : gutter_box.left() - tab_x; 00258 } 00259 if (*gutter_width >= max_gutter) { 00260 // If there is no box because a tab was in the way, get the tab coord. 00261 TBOX gutter_box(box); 00262 if (left) { 00263 gutter_box.set_left(tab_x - max_gutter - 1); 00264 gutter_box.set_right(tab_x - max_gutter); 00265 int tab_gutter = RightEdgeForBox(gutter_box, true, false); 00266 if (tab_gutter < tab_x - 1) 00267 *gutter_width = tab_x - tab_gutter; 00268 } else { 00269 gutter_box.set_left(tab_x + max_gutter); 00270 gutter_box.set_right(tab_x + max_gutter + 1); 00271 int tab_gutter = LeftEdgeForBox(gutter_box, true, false); 00272 if (tab_gutter > tab_x + 1) 00273 *gutter_width = tab_gutter - tab_x; 00274 } 00275 } 00276 if (*gutter_width > max_gutter) 00277 *gutter_width = max_gutter; 00278 // Now look for a neighbour on the inside. 00279 if (debug) 00280 tprintf("Looking for neighbour\n"); 00281 BLOBNBOX* neighbour = AdjacentBlob(bbox, !left, 00282 bbox->flow() == BTFT_TEXT_ON_IMAGE, 0.0, 00283 *gutter_width, box.top(), box.bottom()); 00284 int neighbour_edge = left ? RightEdgeForBox(box, true, false) 00285 : LeftEdgeForBox(box, true, false); 00286 if (neighbour != NULL) { 00287 TBOX n_box = neighbour->bounding_box(); 00288 if (debug) { 00289 tprintf("Found neighbour:"); 00290 n_box.print(); 00291 } 00292 if (left && n_box.left() < neighbour_edge) 00293 neighbour_edge = n_box.left(); 00294 else if (!left && n_box.right() > neighbour_edge) 00295 neighbour_edge = n_box.right(); 00296 } 00297 *neighbour_gap = left ? neighbour_edge - internal_x 00298 : internal_x - neighbour_edge; 00299 } 00300 00301 // Return the x-coord that corresponds to the right edge for the given 00302 // box. If there is a rule line to the right that vertically overlaps it, 00303 // then return the x-coord of the rule line, otherwise return the right 00304 // edge of the page. For details see RightTabForBox below. 00305 int TabFind::RightEdgeForBox(const TBOX& box, bool crossing, bool extended) { 00306 TabVector* v = RightTabForBox(box, crossing, extended); 00307 return v == NULL ? tright_.x() : v->XAtY((box.top() + box.bottom()) / 2); 00308 } 00309 // As RightEdgeForBox, but finds the left Edge instead. 00310 int TabFind::LeftEdgeForBox(const TBOX& box, bool crossing, bool extended) { 00311 TabVector* v = LeftTabForBox(box, crossing, extended); 00312 return v == NULL ? bleft_.x() : v->XAtY((box.top() + box.bottom()) / 2); 00313 } 00314 00315 // This comment documents how this function works. 00316 // For its purpose and arguments, see the comment in tabfind.h. 00317 // TabVectors are stored sorted by perpendicular distance of middle from 00318 // the global mean vertical vector. Since the individual vectors can have 00319 // differing directions, their XAtY for a given y is not necessarily in the 00320 // right order. Therefore the search has to be run with a margin. 00321 // The middle of a vector that passes through (x,y) cannot be higher than 00322 // halfway from y to the top, or lower than halfway from y to the bottom 00323 // of the coordinate range; therefore, the search margin is the range of 00324 // sort keys between these halfway points. Any vector with a sort key greater 00325 // than the upper margin must be to the right of x at y, and likewise any 00326 // vector with a sort key less than the lower margin must pass to the left 00327 // of x at y. 00328 TabVector* TabFind::RightTabForBox(const TBOX& box, bool crossing, 00329 bool extended) { 00330 if (v_it_.empty()) 00331 return NULL; 00332 int top_y = box.top(); 00333 int bottom_y = box.bottom(); 00334 int mid_y = (top_y + bottom_y) / 2; 00335 int right = crossing ? (box.left() + box.right()) / 2 : box.right(); 00336 int min_key, max_key; 00337 SetupTabSearch(right, mid_y, &min_key, &max_key); 00338 // Position the iterator at the first TabVector with sort_key >= min_key. 00339 while (!v_it_.at_first() && v_it_.data()->sort_key() >= min_key) 00340 v_it_.backward(); 00341 while (!v_it_.at_last() && v_it_.data()->sort_key() < min_key) 00342 v_it_.forward(); 00343 // Find the leftmost tab vector that overlaps and has XAtY(mid_y) >= right. 00344 TabVector* best_v = NULL; 00345 int best_x = -1; 00346 int key_limit = -1; 00347 do { 00348 TabVector* v = v_it_.data(); 00349 int x = v->XAtY(mid_y); 00350 if (x >= right && 00351 (v->VOverlap(top_y, bottom_y) > 0 || 00352 (extended && v->ExtendedOverlap(top_y, bottom_y) > 0))) { 00353 if (best_v == NULL || x < best_x) { 00354 best_v = v; 00355 best_x = x; 00356 // We can guarantee that no better vector can be found if the 00357 // sort key exceeds that of the best by max_key - min_key. 00358 key_limit = v->sort_key() + max_key - min_key; 00359 } 00360 } 00361 // Break when the search is done to avoid wrapping the iterator and 00362 // thereby potentially slowing the next search. 00363 if (v_it_.at_last() || 00364 (best_v != NULL && v->sort_key() > key_limit)) 00365 break; // Prevent restarting list for next call. 00366 v_it_.forward(); 00367 } while (!v_it_.at_first()); 00368 return best_v; 00369 } 00370 00371 // As RightTabForBox, but finds the left TabVector instead. 00372 TabVector* TabFind::LeftTabForBox(const TBOX& box, bool crossing, 00373 bool extended) { 00374 if (v_it_.empty()) 00375 return NULL; 00376 int top_y = box.top(); 00377 int bottom_y = box.bottom(); 00378 int mid_y = (top_y + bottom_y) / 2; 00379 int left = crossing ? (box.left() + box.right()) / 2 : box.left(); 00380 int min_key, max_key; 00381 SetupTabSearch(left, mid_y, &min_key, &max_key); 00382 // Position the iterator at the last TabVector with sort_key <= max_key. 00383 while (!v_it_.at_last() && v_it_.data()->sort_key() <= max_key) 00384 v_it_.forward(); 00385 while (!v_it_.at_first() && v_it_.data()->sort_key() > max_key) { 00386 v_it_.backward(); 00387 } 00388 // Find the rightmost tab vector that overlaps and has XAtY(mid_y) <= left. 00389 TabVector* best_v = NULL; 00390 int best_x = -1; 00391 int key_limit = -1; 00392 do { 00393 TabVector* v = v_it_.data(); 00394 int x = v->XAtY(mid_y); 00395 if (x <= left && 00396 (v->VOverlap(top_y, bottom_y) > 0 || 00397 (extended && v->ExtendedOverlap(top_y, bottom_y) > 0))) { 00398 if (best_v == NULL || x > best_x) { 00399 best_v = v; 00400 best_x = x; 00401 // We can guarantee that no better vector can be found if the 00402 // sort key is less than that of the best by max_key - min_key. 00403 key_limit = v->sort_key() - (max_key - min_key); 00404 } 00405 } 00406 // Break when the search is done to avoid wrapping the iterator and 00407 // thereby potentially slowing the next search. 00408 if (v_it_.at_first() || 00409 (best_v != NULL && v->sort_key() < key_limit)) 00410 break; // Prevent restarting list for next call. 00411 v_it_.backward(); 00412 } while (!v_it_.at_last()); 00413 return best_v; 00414 } 00415 00416 // Return true if the given width is close to one of the common 00417 // widths in column_widths_. 00418 bool TabFind::CommonWidth(int width) { 00419 width /= kColumnWidthFactor; 00420 ICOORDELT_IT it(&column_widths_); 00421 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { 00422 ICOORDELT* w = it.data(); 00423 if (NearlyEqual<int>(width, w->x(), 1)) 00424 return true; 00425 } 00426 return false; 00427 } 00428 00429 // Return true if the sizes are more than a 00430 // factor of 2 different. 00431 bool TabFind::DifferentSizes(int size1, int size2) { 00432 return size1 > size2 * 2 || size2 > size1 * 2; 00433 } 00434 00435 // Return true if the sizes are more than a 00436 // factor of 5 different. 00437 bool TabFind::VeryDifferentSizes(int size1, int size2) { 00438 return size1 > size2 * 5 || size2 > size1 * 5; 00439 } 00440 00442 00443 // Top-level function to find TabVectors in an input page block. 00444 // Returns false if the detected skew angle is impossible. 00445 // Applies the detected skew angle to deskew the tabs, blobs and part_grid. 00446 bool TabFind::FindTabVectors(TabVector_LIST* hlines, 00447 BLOBNBOX_LIST* image_blobs, TO_BLOCK* block, 00448 int min_gutter_width, 00449 ColPartitionGrid* part_grid, 00450 FCOORD* deskew, FCOORD* reskew) { 00451 ScrollView* tab_win = FindInitialTabVectors(image_blobs, min_gutter_width, 00452 block); 00453 ComputeColumnWidths(tab_win, part_grid); 00454 TabVector::MergeSimilarTabVectors(vertical_skew_, &vectors_, this); 00455 SortVectors(); 00456 CleanupTabs(); 00457 if (!Deskew(hlines, image_blobs, block, deskew, reskew)) 00458 return false; // Skew angle is too large. 00459 part_grid->Deskew(*deskew); 00460 ApplyTabConstraints(); 00461 #ifndef GRAPHICS_DISABLED 00462 if (textord_tabfind_show_finaltabs) { 00463 tab_win = MakeWindow(640, 50, "FinalTabs"); 00464 if (textord_debug_images) { 00465 tab_win->Image(AlignedBlob::textord_debug_pix().string(), 00466 image_origin_.x(), image_origin_.y()); 00467 } else { 00468 DisplayBoxes(tab_win); 00469 DisplayTabs("FinalTabs", tab_win); 00470 } 00471 tab_win = DisplayTabVectors(tab_win); 00472 } 00473 #endif // GRAPHICS_DISABLED 00474 return true; 00475 } 00476 00477 // Top-level function to not find TabVectors in an input page block, 00478 // but setup for single column mode. 00479 void TabFind::DontFindTabVectors(BLOBNBOX_LIST* image_blobs, TO_BLOCK* block, 00480 FCOORD* deskew, FCOORD* reskew) { 00481 InsertBlobsToGrid(false, false, image_blobs, this); 00482 InsertBlobsToGrid(true, false, &block->blobs, this); 00483 deskew->set_x(1.0f); 00484 deskew->set_y(0.0f); 00485 reskew->set_x(1.0f); 00486 reskew->set_y(0.0f); 00487 } 00488 00489 // Cleans up the lists of blobs in the block ready for use by TabFind. 00490 // Large blobs that look like text are moved to the main blobs list. 00491 // Main blobs that are superseded by the image blobs are deleted. 00492 void TabFind::TidyBlobs(TO_BLOCK* block) { 00493 BLOBNBOX_IT large_it = &block->large_blobs; 00494 BLOBNBOX_IT blob_it = &block->blobs; 00495 int b_count = 0; 00496 for (large_it.mark_cycle_pt(); !large_it.cycled_list(); large_it.forward()) { 00497 BLOBNBOX* large_blob = large_it.data(); 00498 if (large_blob->owner() != NULL) { 00499 blob_it.add_to_end(large_it.extract()); 00500 ++b_count; 00501 } 00502 } 00503 if (textord_debug_tabfind) { 00504 tprintf("Moved %d large blobs to normal list\n", 00505 b_count); 00506 #ifndef GRAPHICS_DISABLED 00507 ScrollView* rej_win = MakeWindow(500, 300, "Image blobs"); 00508 block->plot_graded_blobs(rej_win); 00509 block->plot_noise_blobs(rej_win); 00510 rej_win->Update(); 00511 #endif // GRAPHICS_DISABLED 00512 } 00513 block->DeleteUnownedNoise(); 00514 } 00515 00516 // Helper function to setup search limits for *TabForBox. 00517 void TabFind::SetupTabSearch(int x, int y, int* min_key, int* max_key) { 00518 int key1 = TabVector::SortKey(vertical_skew_, x, (y + tright_.y()) / 2); 00519 int key2 = TabVector::SortKey(vertical_skew_, x, (y + bleft_.y()) / 2); 00520 *min_key = MIN(key1, key2); 00521 *max_key = MAX(key1, key2); 00522 } 00523 00524 ScrollView* TabFind::DisplayTabVectors(ScrollView* tab_win) { 00525 #ifndef GRAPHICS_DISABLED 00526 // For every vector, display it. 00527 TabVector_IT it(&vectors_); 00528 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { 00529 TabVector* vector = it.data(); 00530 vector->Display(tab_win); 00531 } 00532 tab_win->Update(); 00533 #endif 00534 return tab_win; 00535 } 00536 00537 // PRIVATE CODE. 00538 // 00539 // First part of FindTabVectors, which may be used twice if the text 00540 // is mostly of vertical alignment. 00541 ScrollView* TabFind::FindInitialTabVectors(BLOBNBOX_LIST* image_blobs, 00542 int min_gutter_width, 00543 TO_BLOCK* block) { 00544 if (textord_tabfind_show_initialtabs) { 00545 ScrollView* line_win = MakeWindow(0, 0, "VerticalLines"); 00546 line_win = DisplayTabVectors(line_win); 00547 } 00548 // Prepare the grid. 00549 if (image_blobs != NULL) 00550 InsertBlobsToGrid(true, false, image_blobs, this); 00551 InsertBlobsToGrid(true, false, &block->blobs, this); 00552 ScrollView* initial_win = FindTabBoxes(min_gutter_width); 00553 FindAllTabVectors(min_gutter_width); 00554 00555 TabVector::MergeSimilarTabVectors(vertical_skew_, &vectors_, this); 00556 SortVectors(); 00557 EvaluateTabs(); 00558 if (textord_tabfind_show_initialtabs && initial_win != NULL) 00559 initial_win = DisplayTabVectors(initial_win); 00560 MarkVerticalText(); 00561 return initial_win; 00562 } 00563 00564 // Helper displays all the boxes in the given vector on the given window. 00565 static void DisplayBoxVector(const GenericVector<BLOBNBOX*>& boxes, 00566 ScrollView* win) { 00567 #ifndef GRAPHICS_DISABLED 00568 for (int i = 0; i < boxes.size(); ++i) { 00569 TBOX box = boxes[i]->bounding_box(); 00570 int left_x = box.left(); 00571 int right_x = box.right(); 00572 int top_y = box.top(); 00573 int bottom_y = box.bottom(); 00574 ScrollView::Color box_color = boxes[i]->BoxColor(); 00575 win->Pen(box_color); 00576 win->Rectangle(left_x, bottom_y, right_x, top_y); 00577 } 00578 win->Update(); 00579 #endif // GRAPHICS_DISABLED 00580 } 00581 00582 // For each box in the grid, decide whether it is a candidate tab-stop, 00583 // and if so add it to the left/right tab boxes. 00584 ScrollView* TabFind::FindTabBoxes(int min_gutter_width) { 00585 left_tab_boxes_.clear(); 00586 right_tab_boxes_.clear(); 00587 // For every bbox in the grid, determine whether it uses a tab on an edge. 00588 GridSearch<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT> gsearch(this); 00589 gsearch.StartFullSearch(); 00590 BLOBNBOX* bbox; 00591 while ((bbox = gsearch.NextFullSearch()) != NULL) { 00592 if (TestBoxForTabs(bbox, min_gutter_width)) { 00593 // If it is any kind of tab, insert it into the vectors. 00594 if (bbox->left_tab_type() != TT_NONE) 00595 left_tab_boxes_.push_back(bbox); 00596 if (bbox->right_tab_type() != TT_NONE) 00597 right_tab_boxes_.push_back(bbox); 00598 } 00599 } 00600 // Sort left tabs by left and right by right to see the outermost one first 00601 // on a ragged tab. 00602 left_tab_boxes_.sort(SortByBoxLeft<BLOBNBOX>); 00603 right_tab_boxes_.sort(SortRightToLeft<BLOBNBOX>); 00604 ScrollView* tab_win = NULL; 00605 #ifndef GRAPHICS_DISABLED 00606 if (textord_tabfind_show_initialtabs) { 00607 tab_win = MakeWindow(0, 100, "InitialTabs"); 00608 tab_win->Pen(ScrollView::BLUE); 00609 tab_win->Brush(ScrollView::NONE); 00610 // Display the left and right tab boxes. 00611 DisplayBoxVector(left_tab_boxes_, tab_win); 00612 DisplayBoxVector(right_tab_boxes_, tab_win); 00613 tab_win = DisplayTabs("Tabs", tab_win); 00614 } 00615 #endif // GRAPHICS_DISABLED 00616 return tab_win; 00617 } 00618 00619 bool TabFind::TestBoxForTabs(BLOBNBOX* bbox, int min_gutter_width) { 00620 GridSearch<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT> radsearch(this); 00621 TBOX box = bbox->bounding_box(); 00622 // If there are separator lines, get the column edges. 00623 int left_column_edge = bbox->left_rule(); 00624 int right_column_edge = bbox->right_rule(); 00625 // The edges of the bounding box of the blob being processed. 00626 int left_x = box.left(); 00627 int right_x = box.right(); 00628 int top_y = box.top(); 00629 int bottom_y = box.bottom(); 00630 int height = box.height(); 00631 bool debug = WithinTestRegion(3, left_x, top_y); 00632 if (debug) { 00633 tprintf("Column edges for blob at (%d,%d)->(%d,%d) are [%d, %d]\n", 00634 left_x, top_y, right_x, bottom_y, 00635 left_column_edge, right_column_edge); 00636 } 00637 // Compute a search radius based on a multiple of the height. 00638 int radius = (height * kTabRadiusFactor + gridsize_ - 1) / gridsize_; 00639 radsearch.StartRadSearch((left_x + right_x)/2, (top_y + bottom_y)/2, radius); 00640 // In Vertical Page mode, once we have an estimate of the vertical line 00641 // spacing, the minimum amount of gutter space before a possible tab is 00642 // increased under the assumption that column partition is always larger 00643 // than line spacing. 00644 int min_spacing = 00645 static_cast<int>(height * textord_tabfind_aligned_gap_fraction); 00646 if (min_gutter_width > min_spacing) 00647 min_spacing = min_gutter_width; 00648 int min_ragged_gutter = kRaggedGutterMultiple * gridsize(); 00649 if (min_gutter_width > min_ragged_gutter) 00650 min_ragged_gutter = min_gutter_width; 00651 int target_right = left_x - min_spacing; 00652 int target_left = right_x + min_spacing; 00653 // We will be evaluating whether the left edge could be a left tab, and 00654 // whether the right edge could be a right tab. 00655 // A box can be a tab if its bool is_(left/right)_tab remains true, meaning 00656 // that no blobs have been found in the gutter during the radial search. 00657 // A box can also be a tab if there are objects in the gutter only above 00658 // or only below, and there are aligned objects on the opposite side, but 00659 // not too many unaligned objects. The maybe_(left/right)_tab_up counts 00660 // aligned objects above and negatively counts unaligned objects above, 00661 // and is set to -MAX_INT32 if a gutter object is found above. 00662 // The other 3 maybe ints work similarly for the other sides. 00663 // These conditions are very strict, to minimize false positives, and really 00664 // only aligned tabs and outermost ragged tab blobs will qualify, so we 00665 // also have maybe_ragged_left/right with less stringent rules. 00666 // A blob that is maybe_ragged_left/right will be further qualified later, 00667 // using the min_ragged_gutter. 00668 bool is_left_tab = true; 00669 bool is_right_tab = true; 00670 bool maybe_ragged_left = true; 00671 bool maybe_ragged_right = true; 00672 int maybe_left_tab_up = 0; 00673 int maybe_right_tab_up = 0; 00674 int maybe_left_tab_down = 0; 00675 int maybe_right_tab_down = 0; 00676 if (bbox->leader_on_left()) { 00677 is_left_tab = false; 00678 maybe_ragged_left = false; 00679 maybe_left_tab_up = -MAX_INT32; 00680 maybe_left_tab_down = -MAX_INT32; 00681 } 00682 if (bbox->leader_on_right()) { 00683 is_right_tab = false; 00684 maybe_ragged_right = false; 00685 maybe_right_tab_up = -MAX_INT32; 00686 maybe_right_tab_down = -MAX_INT32; 00687 } 00688 int alignment_tolerance = static_cast<int>(resolution_ * kAlignedFraction); 00689 BLOBNBOX* neighbour = NULL; 00690 while ((neighbour = radsearch.NextRadSearch()) != NULL) { 00691 if (neighbour == bbox) 00692 continue; 00693 TBOX nbox = neighbour->bounding_box(); 00694 int n_left = nbox.left(); 00695 int n_right = nbox.right(); 00696 if (debug) 00697 tprintf("Neighbour at (%d,%d)->(%d,%d)\n", 00698 n_left, nbox.bottom(), n_right, nbox.top()); 00699 // If the neighbouring blob is the wrong side of a separator line, then it 00700 // "doesn't exist" as far as we are concerned. 00701 if (n_right > right_column_edge || n_left < left_column_edge || 00702 left_x < neighbour->left_rule() || right_x > neighbour->right_rule()) 00703 continue; // Separator line in the way. 00704 int n_mid_x = (n_left + n_right) / 2; 00705 int n_mid_y = (nbox.top() + nbox.bottom()) / 2; 00706 if (n_mid_x <= left_x && n_right >= target_right) { 00707 if (debug) 00708 tprintf("Not a left tab\n"); 00709 is_left_tab = false; 00710 if (n_mid_y < top_y) 00711 maybe_left_tab_down = -MAX_INT32; 00712 if (n_mid_y > bottom_y) 00713 maybe_left_tab_up = -MAX_INT32; 00714 } else if (NearlyEqual(left_x, n_left, alignment_tolerance)) { 00715 if (debug) 00716 tprintf("Maybe a left tab\n"); 00717 if (n_mid_y > top_y && maybe_left_tab_up > -MAX_INT32) 00718 ++maybe_left_tab_up; 00719 if (n_mid_y < bottom_y && maybe_left_tab_down > -MAX_INT32) 00720 ++maybe_left_tab_down; 00721 } else if (n_left < left_x && n_right >= left_x) { 00722 // Overlaps but not aligned so negative points on a maybe. 00723 if (debug) 00724 tprintf("Maybe Not a left tab\n"); 00725 if (n_mid_y > top_y && maybe_left_tab_up > -MAX_INT32) 00726 --maybe_left_tab_up; 00727 if (n_mid_y < bottom_y && maybe_left_tab_down > -MAX_INT32) 00728 --maybe_left_tab_down; 00729 } 00730 if (n_left < left_x && nbox.y_overlap(box) && n_right >= target_right) { 00731 maybe_ragged_left = false; 00732 if (debug) 00733 tprintf("Not a ragged left\n"); 00734 } 00735 if (n_mid_x >= right_x && n_left <= target_left) { 00736 if (debug) 00737 tprintf("Not a right tab\n"); 00738 is_right_tab = false; 00739 if (n_mid_y < top_y) 00740 maybe_right_tab_down = -MAX_INT32; 00741 if (n_mid_y > bottom_y) 00742 maybe_right_tab_up = -MAX_INT32; 00743 } else if (NearlyEqual(right_x, n_right, alignment_tolerance)) { 00744 if (debug) 00745 tprintf("Maybe a right tab\n"); 00746 if (n_mid_y > top_y && maybe_right_tab_up > -MAX_INT32) 00747 ++maybe_right_tab_up; 00748 if (n_mid_y < bottom_y && maybe_right_tab_down > -MAX_INT32) 00749 ++maybe_right_tab_down; 00750 } else if (n_right > right_x && n_left <= right_x) { 00751 // Overlaps but not aligned so negative points on a maybe. 00752 if (debug) 00753 tprintf("Maybe Not a right tab\n"); 00754 if (n_mid_y > top_y && maybe_right_tab_up > -MAX_INT32) 00755 --maybe_right_tab_up; 00756 if (n_mid_y < bottom_y && maybe_right_tab_down > -MAX_INT32) 00757 --maybe_right_tab_down; 00758 } 00759 if (n_right > right_x && nbox.y_overlap(box) && n_left <= target_left) { 00760 maybe_ragged_right = false; 00761 if (debug) 00762 tprintf("Not a ragged right\n"); 00763 } 00764 if (maybe_left_tab_down == -MAX_INT32 && maybe_left_tab_up == -MAX_INT32 && 00765 maybe_right_tab_down == -MAX_INT32 && maybe_right_tab_up == -MAX_INT32) 00766 break; 00767 } 00768 if (is_left_tab || maybe_left_tab_up > 1 || maybe_left_tab_down > 1) { 00769 bbox->set_left_tab_type(TT_MAYBE_ALIGNED); 00770 } else if (maybe_ragged_left && ConfirmRaggedLeft(bbox, min_ragged_gutter)) { 00771 bbox->set_left_tab_type(TT_MAYBE_RAGGED); 00772 } else { 00773 bbox->set_left_tab_type(TT_NONE); 00774 } 00775 if (is_right_tab || maybe_right_tab_up > 1 || maybe_right_tab_down > 1) { 00776 bbox->set_right_tab_type(TT_MAYBE_ALIGNED); 00777 } else if (maybe_ragged_right && 00778 ConfirmRaggedRight(bbox, min_ragged_gutter)) { 00779 bbox->set_right_tab_type(TT_MAYBE_RAGGED); 00780 } else { 00781 bbox->set_right_tab_type(TT_NONE); 00782 } 00783 if (debug) { 00784 tprintf("Left result = %s, Right result=%s\n", 00785 bbox->left_tab_type() == TT_MAYBE_ALIGNED ? "Aligned" : 00786 (bbox->left_tab_type() == TT_MAYBE_RAGGED ? "Ragged" : "None"), 00787 bbox->right_tab_type() == TT_MAYBE_ALIGNED ? "Aligned" : 00788 (bbox->right_tab_type() == TT_MAYBE_RAGGED ? "Ragged" : "None")); 00789 } 00790 return bbox->left_tab_type() != TT_NONE || bbox->right_tab_type() != TT_NONE; 00791 } 00792 00793 // Returns true if there is nothing in the rectangle of width min_gutter to 00794 // the left of bbox. 00795 bool TabFind::ConfirmRaggedLeft(BLOBNBOX* bbox, int min_gutter) { 00796 TBOX search_box(bbox->bounding_box()); 00797 search_box.set_right(search_box.left()); 00798 search_box.set_left(search_box.left() - min_gutter); 00799 return NothingYOverlapsInBox(search_box, bbox->bounding_box()); 00800 } 00801 00802 // Returns true if there is nothing in the rectangle of width min_gutter to 00803 // the right of bbox. 00804 bool TabFind::ConfirmRaggedRight(BLOBNBOX* bbox, int min_gutter) { 00805 TBOX search_box(bbox->bounding_box()); 00806 search_box.set_left(search_box.right()); 00807 search_box.set_right(search_box.right() + min_gutter); 00808 return NothingYOverlapsInBox(search_box, bbox->bounding_box()); 00809 } 00810 00811 // Returns true if there is nothing in the given search_box that vertically 00812 // overlaps target_box other than target_box itself. 00813 bool TabFind::NothingYOverlapsInBox(const TBOX& search_box, 00814 const TBOX& target_box) { 00815 BlobGridSearch rsearch(this); 00816 rsearch.StartRectSearch(search_box); 00817 BLOBNBOX* blob; 00818 while ((blob = rsearch.NextRectSearch()) != NULL) { 00819 const TBOX& box = blob->bounding_box(); 00820 if (box.y_overlap(target_box) && !(box == target_box)) 00821 return false; 00822 } 00823 return true; 00824 } 00825 00826 void TabFind::FindAllTabVectors(int min_gutter_width) { 00827 // A list of vectors that will be created in estimating the skew. 00828 TabVector_LIST dummy_vectors; 00829 // An estimate of the vertical direction, revised as more lines are added. 00830 int vertical_x = 0; 00831 int vertical_y = 1; 00832 // Find an estimate of the vertical direction by finding some tab vectors. 00833 // Slowly up the search size until we get some vectors. 00834 for (int search_size = kMinVerticalSearch; search_size < kMaxVerticalSearch; 00835 search_size += kMinVerticalSearch) { 00836 int vector_count = FindTabVectors(search_size, TA_LEFT_ALIGNED, 00837 min_gutter_width, 00838 &dummy_vectors, 00839 &vertical_x, &vertical_y); 00840 vector_count += FindTabVectors(search_size, TA_RIGHT_ALIGNED, 00841 min_gutter_width, 00842 &dummy_vectors, 00843 &vertical_x, &vertical_y); 00844 if (vector_count > 0) 00845 break; 00846 } 00847 // Get rid of the test vectors and reset the types of the tabs. 00848 dummy_vectors.clear(); 00849 for (int i = 0; i < left_tab_boxes_.size(); ++i) { 00850 BLOBNBOX* bbox = left_tab_boxes_[i]; 00851 if (bbox->left_tab_type() == TT_CONFIRMED) 00852 bbox->set_left_tab_type(TT_MAYBE_ALIGNED); 00853 } 00854 for (int i = 0; i < right_tab_boxes_.size(); ++i) { 00855 BLOBNBOX* bbox = right_tab_boxes_[i]; 00856 if (bbox->right_tab_type() == TT_CONFIRMED) 00857 bbox->set_right_tab_type(TT_MAYBE_ALIGNED); 00858 } 00859 if (textord_debug_tabfind) { 00860 tprintf("Beginning real tab search with vertical = %d,%d...\n", 00861 vertical_x, vertical_y); 00862 } 00863 // Now do the real thing ,but keep the vectors in the dummy_vectors list 00864 // until they are all done, so we don't get the tab vectors confused with 00865 // the rule line vectors. 00866 FindTabVectors(kMaxVerticalSearch, TA_LEFT_ALIGNED, min_gutter_width, 00867 &dummy_vectors, &vertical_x, &vertical_y); 00868 FindTabVectors(kMaxVerticalSearch, TA_RIGHT_ALIGNED, min_gutter_width, 00869 &dummy_vectors, &vertical_x, &vertical_y); 00870 FindTabVectors(kMaxRaggedSearch, TA_LEFT_RAGGED, min_gutter_width, 00871 &dummy_vectors, &vertical_x, &vertical_y); 00872 FindTabVectors(kMaxRaggedSearch, TA_RIGHT_RAGGED, min_gutter_width, 00873 &dummy_vectors, &vertical_x, &vertical_y); 00874 // Now add the vectors to the vectors_ list. 00875 TabVector_IT v_it(&vectors_); 00876 v_it.add_list_after(&dummy_vectors); 00877 // Now use the summed (mean) vertical vector as the direction for everything. 00878 SetVerticalSkewAndParellelize(vertical_x, vertical_y); 00879 } 00880 00881 // Helper for FindAllTabVectors finds the vectors of a particular type. 00882 int TabFind::FindTabVectors(int search_size_multiple, TabAlignment alignment, 00883 int min_gutter_width, TabVector_LIST* vectors, 00884 int* vertical_x, int* vertical_y) { 00885 TabVector_IT vector_it(vectors); 00886 int vector_count = 0; 00887 // Search the right or left tab boxes, looking for tab vectors. 00888 bool right = alignment == TA_RIGHT_ALIGNED || alignment == TA_RIGHT_RAGGED; 00889 const GenericVector<BLOBNBOX*>& boxes = right ? right_tab_boxes_ 00890 : left_tab_boxes_; 00891 for (int i = 0; i < boxes.size(); ++i) { 00892 BLOBNBOX* bbox = boxes[i]; 00893 if ((!right && bbox->left_tab_type() == TT_MAYBE_ALIGNED) || 00894 (right && bbox->right_tab_type() == TT_MAYBE_ALIGNED)) { 00895 TabVector* vector = FindTabVector(search_size_multiple, min_gutter_width, 00896 alignment, 00897 bbox, vertical_x, vertical_y); 00898 if (vector != NULL) { 00899 ++vector_count; 00900 vector_it.add_to_end(vector); 00901 } 00902 } 00903 } 00904 return vector_count; 00905 } 00906 00907 // Finds a vector corresponding to a tabstop running through the 00908 // given box of the given alignment type. 00909 // search_size_multiple is a multiple of height used to control 00910 // the size of the search. 00911 // vertical_x and y are updated with an estimate of the real 00912 // vertical direction. (skew finding.) 00913 // Returns NULL if no decent tabstop can be found. 00914 TabVector* TabFind::FindTabVector(int search_size_multiple, 00915 int min_gutter_width, 00916 TabAlignment alignment, 00917 BLOBNBOX* bbox, 00918 int* vertical_x, int* vertical_y) { 00919 int height = MAX(bbox->bounding_box().height(), gridsize()); 00920 AlignedBlobParams align_params(*vertical_x, *vertical_y, 00921 height, 00922 search_size_multiple, min_gutter_width, 00923 resolution_, alignment); 00924 // FindVerticalAlignment is in the parent (AlignedBlob) class. 00925 return FindVerticalAlignment(align_params, bbox, vertical_x, vertical_y); 00926 } 00927 00928 // Set the vertical_skew_ member from the given vector and refit 00929 // all vectors parallel to the skew vector. 00930 void TabFind::SetVerticalSkewAndParellelize(int vertical_x, int vertical_y) { 00931 // Fit the vertical vector into an ICOORD, which is 16 bit. 00932 vertical_skew_.set_with_shrink(vertical_x, vertical_y); 00933 if (textord_debug_tabfind) 00934 tprintf("Vertical skew vector=(%d,%d)\n", 00935 vertical_skew_.x(), vertical_skew_.y()); 00936 v_it_.set_to_list(&vectors_); 00937 for (v_it_.mark_cycle_pt(); !v_it_.cycled_list(); v_it_.forward()) { 00938 TabVector* v = v_it_.data(); 00939 v->Fit(vertical_skew_, true); 00940 } 00941 // Now sort the vectors as their direction has potentially changed. 00942 SortVectors(); 00943 } 00944 00945 // Sort all the current vectors using the given vertical direction vector. 00946 void TabFind::SortVectors() { 00947 vectors_.sort(TabVector::SortVectorsByKey); 00948 v_it_.set_to_list(&vectors_); 00949 } 00950 00951 // Evaluate all the current tab vectors. 00952 void TabFind::EvaluateTabs() { 00953 TabVector_IT rule_it(&vectors_); 00954 for (rule_it.mark_cycle_pt(); !rule_it.cycled_list(); rule_it.forward()) { 00955 TabVector* tab = rule_it.data(); 00956 if (!tab->IsSeparator()) { 00957 tab->Evaluate(vertical_skew_, this); 00958 if (tab->BoxCount() < kMinEvaluatedTabs) { 00959 if (textord_debug_tabfind > 2) 00960 tab->Print("Too few boxes"); 00961 delete rule_it.extract(); 00962 v_it_.set_to_list(&vectors_); 00963 } else if (WithinTestRegion(3, tab->startpt().x(), tab->startpt().y())) { 00964 tab->Print("Evaluated tab"); 00965 } 00966 } 00967 } 00968 } 00969 00970 // Trace textlines from one side to the other of each tab vector, saving 00971 // the most frequent column widths found in a list so that a given width 00972 // can be tested for being a common width with a simple callback function. 00973 void TabFind::ComputeColumnWidths(ScrollView* tab_win, 00974 ColPartitionGrid* part_grid) { 00975 #ifndef GRAPHICS_DISABLED 00976 if (tab_win != NULL) 00977 tab_win->Pen(ScrollView::WHITE); 00978 #endif // GRAPHICS_DISABLED 00979 // Accumulate column sections into a STATS 00980 int col_widths_size = (tright_.x() - bleft_.x()) / kColumnWidthFactor; 00981 STATS col_widths(0, col_widths_size + 1); 00982 ApplyPartitionsToColumnWidths(part_grid, &col_widths); 00983 #ifndef GRAPHICS_DISABLED 00984 if (tab_win != NULL) { 00985 tab_win->Update(); 00986 } 00987 #endif // GRAPHICS_DISABLED 00988 if (textord_debug_tabfind > 1) 00989 col_widths.print(); 00990 // Now make a list of column widths. 00991 MakeColumnWidths(col_widths_size, &col_widths); 00992 } 00993 00994 // Find column width and pair-up tab vectors with existing ColPartitions. 00995 void TabFind::ApplyPartitionsToColumnWidths(ColPartitionGrid* part_grid, 00996 STATS* col_widths) { 00997 // For every ColPartition in the part_grid, add partners to the tabvectors 00998 // and accumulate the column widths. 00999 ColPartitionGridSearch gsearch(part_grid); 01000 gsearch.StartFullSearch(); 01001 ColPartition* part; 01002 while ((part = gsearch.NextFullSearch()) != NULL) { 01003 BLOBNBOX_C_IT blob_it(part->boxes()); 01004 if (blob_it.empty()) 01005 continue; 01006 BLOBNBOX* left_blob = blob_it.data(); 01007 blob_it.move_to_last(); 01008 BLOBNBOX* right_blob = blob_it.data(); 01009 TabVector* left_vector = LeftTabForBox(left_blob->bounding_box(), 01010 true, false); 01011 if (left_vector == NULL || left_vector->IsRightTab()) 01012 continue; 01013 TabVector* right_vector = RightTabForBox(right_blob->bounding_box(), 01014 true, false); 01015 if (right_vector == NULL || right_vector->IsLeftTab()) 01016 continue; 01017 01018 AddPartnerVector(left_blob, right_blob, left_vector, right_vector); 01019 int line_left = left_vector->XAtY(left_blob->bounding_box().bottom()); 01020 int line_right = right_vector->XAtY(right_blob->bounding_box().bottom()); 01021 // Add to STATS of measurements if the width is significant. 01022 int width = line_right - line_left; 01023 if (width >= kMinColumnWidth) 01024 col_widths->add(width / kColumnWidthFactor, 1); 01025 } 01026 } 01027 01028 // Helper makes the list of common column widths in column_widths_ from the 01029 // input col_widths. Destroys the content of col_widths by repeatedly 01030 // finding the mode and erasing the peak. 01031 void TabFind::MakeColumnWidths(int col_widths_size, STATS* col_widths) { 01032 ICOORDELT_IT w_it(&column_widths_); 01033 int total_col_count = col_widths->get_total(); 01034 while (col_widths->get_total() > 0) { 01035 int width = col_widths->mode(); 01036 int col_count = col_widths->pile_count(width); 01037 col_widths->add(width, -col_count); 01038 // Get the entire peak. 01039 for (int left = width - 1; left > 0 && 01040 col_widths->pile_count(left) > 0; 01041 --left) { 01042 int new_count = col_widths->pile_count(left); 01043 col_count += new_count; 01044 col_widths->add(left, -new_count); 01045 } 01046 for (int right = width + 1; right < col_widths_size && 01047 col_widths->pile_count(right) > 0; 01048 ++right) { 01049 int new_count = col_widths->pile_count(right); 01050 col_count += new_count; 01051 col_widths->add(right, -new_count); 01052 } 01053 if (col_count > kMinLinesInColumn && 01054 col_count > kMinFractionalLinesInColumn * total_col_count) { 01055 ICOORDELT* w = new ICOORDELT(width, col_count); 01056 w_it.add_after_then_move(w); 01057 if (textord_debug_tabfind) 01058 tprintf("Column of width %d has %d = %.2f%% lines\n", 01059 width * kColumnWidthFactor, col_count, 01060 100.0 * col_count / total_col_count); 01061 } 01062 } 01063 } 01064 01065 // Mark blobs as being in a vertical text line where that is the case. 01066 // Returns true if the majority of the image is vertical text lines. 01067 void TabFind::MarkVerticalText() { 01068 if (textord_debug_tabfind) 01069 tprintf("Checking for vertical lines\n"); 01070 BlobGridSearch gsearch(this); 01071 gsearch.StartFullSearch(); 01072 BLOBNBOX* blob = NULL; 01073 while ((blob = gsearch.NextFullSearch()) != NULL) { 01074 if (blob->region_type() < BRT_UNKNOWN) 01075 continue; 01076 if (blob->UniquelyVertical()) { 01077 blob->set_region_type(BRT_VERT_TEXT); 01078 } 01079 } 01080 } 01081 01082 int TabFind::FindMedianGutterWidth(TabVector_LIST *lines) { 01083 TabVector_IT it(lines); 01084 int prev_right = -1; 01085 int max_gap = static_cast<int>(kMaxGutterWidthAbsolute * resolution_); 01086 STATS gaps(0, max_gap); 01087 STATS heights(0, max_gap); 01088 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { 01089 TabVector* v = it.data(); 01090 TabVector* partner = v->GetSinglePartner(); 01091 if (!v->IsLeftTab() || v->IsSeparator() || !partner) continue; 01092 heights.add(partner->startpt().x() - v->startpt().x(), 1); 01093 if (prev_right > 0 && v->startpt().x() > prev_right) { 01094 gaps.add(v->startpt().x() - prev_right, 1); 01095 } 01096 prev_right = partner->startpt().x(); 01097 } 01098 if (textord_debug_tabfind) 01099 tprintf("TabGutter total %d median_gap %.2f median_hgt %.2f\n", 01100 gaps.get_total(), gaps.median(), heights.median()); 01101 if (gaps.get_total() < kMinLinesInColumn) return 0; 01102 return static_cast<int>(gaps.median()); 01103 } 01104 01105 // Find the next adjacent (looking to the left or right) blob on this text 01106 // line, with the constraint that it must vertically significantly overlap 01107 // the [top_y, bottom_y] range. 01108 // If ignore_images is true, then blobs with aligned_text() < 0 are treated 01109 // as if they do not exist. 01110 BLOBNBOX* TabFind::AdjacentBlob(const BLOBNBOX* bbox, 01111 bool look_left, bool ignore_images, 01112 double min_overlap_fraction, 01113 int gap_limit, int top_y, int bottom_y) { 01114 GridSearch<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT> sidesearch(this); 01115 const TBOX& box = bbox->bounding_box(); 01116 int left = box.left(); 01117 int right = box.right(); 01118 int mid_x = (left + right) / 2; 01119 sidesearch.StartSideSearch(mid_x, bottom_y, top_y); 01120 int best_gap = 0; 01121 bool debug = WithinTestRegion(3, left, bottom_y); 01122 BLOBNBOX* result = NULL; 01123 BLOBNBOX* neighbour = NULL; 01124 while ((neighbour = sidesearch.NextSideSearch(look_left)) != NULL) { 01125 if (debug) { 01126 tprintf("Adjacent blob: considering box:"); 01127 neighbour->bounding_box().print(); 01128 } 01129 if (neighbour == bbox || 01130 (ignore_images && neighbour->region_type() < BRT_UNKNOWN)) 01131 continue; 01132 const TBOX& nbox = neighbour->bounding_box(); 01133 int n_top_y = nbox.top(); 01134 int n_bottom_y = nbox.bottom(); 01135 int v_overlap = MIN(n_top_y, top_y) - MAX(n_bottom_y, bottom_y); 01136 int height = top_y - bottom_y; 01137 int n_height = n_top_y - n_bottom_y; 01138 if (v_overlap > min_overlap_fraction * MIN(height, n_height) && 01139 (min_overlap_fraction == 0.0 || !DifferentSizes(height, n_height))) { 01140 int n_left = nbox.left(); 01141 int n_right = nbox.right(); 01142 int h_gap = MAX(n_left, left) - MIN(n_right, right); 01143 int n_mid_x = (n_left + n_right) / 2; 01144 if (look_left == (n_mid_x < mid_x) && n_mid_x != mid_x) { 01145 if (h_gap > gap_limit) { 01146 // Hit a big gap before next tab so don't return anything. 01147 if (debug) 01148 tprintf("Giving up due to big gap = %d vs %d\n", 01149 h_gap, gap_limit); 01150 return result; 01151 } 01152 if (h_gap > 0 && (look_left ? neighbour->right_tab_type() 01153 : neighbour->left_tab_type()) >= TT_CONFIRMED) { 01154 // Hit a tab facing the wrong way. Stop in case we are crossing 01155 // the column boundary. 01156 if (debug) 01157 tprintf("Collision with like tab of type %d at %d,%d\n", 01158 look_left ? neighbour->right_tab_type() 01159 : neighbour->left_tab_type(), 01160 n_left, nbox.bottom()); 01161 return result; 01162 } 01163 // This is a good fit to the line. Continue with this 01164 // neighbour as the bbox if the best gap. 01165 if (result == NULL || h_gap < best_gap) { 01166 if (debug) 01167 tprintf("Good result\n"); 01168 result = neighbour; 01169 best_gap = h_gap; 01170 } else { 01171 // The new one is worse, so we probably already have the best result. 01172 return result; 01173 } 01174 } else if (debug) { 01175 tprintf("Wrong way\n"); 01176 } 01177 } else if (debug) { 01178 tprintf("Insufficient overlap\n"); 01179 } 01180 } 01181 if (WithinTestRegion(3, left, box.top())) 01182 tprintf("Giving up due to end of search\n"); 01183 return result; // Hit the edge and found nothing. 01184 } 01185 01186 // Add a bi-directional partner relationship between the left 01187 // and the right. If one (or both) of the vectors is a separator, 01188 // extend a nearby extendable vector or create a new one of the 01189 // correct type, using the given left or right blob as a guide. 01190 void TabFind::AddPartnerVector(BLOBNBOX* left_blob, BLOBNBOX* right_blob, 01191 TabVector* left, TabVector* right) { 01192 const TBOX& left_box = left_blob->bounding_box(); 01193 const TBOX& right_box = right_blob->bounding_box(); 01194 if (left->IsSeparator()) { 01195 // Try to find a nearby left edge to extend. 01196 TabVector* v = LeftTabForBox(left_box, true, true); 01197 if (v != NULL && v != left && v->IsLeftTab() && 01198 v->XAtY(left_box.top()) > left->XAtY(left_box.top())) { 01199 left = v; // Found a good replacement. 01200 left->ExtendToBox(left_blob); 01201 } else { 01202 // Fake a vector. 01203 left = new TabVector(*left, TA_LEFT_RAGGED, vertical_skew_, left_blob); 01204 vectors_.add_sorted(TabVector::SortVectorsByKey, left); 01205 v_it_.move_to_first(); 01206 } 01207 } 01208 if (right->IsSeparator()) { 01209 // Try to find a nearby left edge to extend. 01210 if (WithinTestRegion(3, right_box.right(), right_box.bottom())) { 01211 tprintf("Box edge (%d,%d-%d)", 01212 right_box.right(), right_box.bottom(), right_box.top()); 01213 right->Print(" looking for improvement for"); 01214 } 01215 TabVector* v = RightTabForBox(right_box, true, true); 01216 if (v != NULL && v != right && v->IsRightTab() && 01217 v->XAtY(right_box.top()) < right->XAtY(right_box.top())) { 01218 right = v; // Found a good replacement. 01219 right->ExtendToBox(right_blob); 01220 if (WithinTestRegion(3, right_box.right(), right_box.bottom())) { 01221 right->Print("Extended vector"); 01222 } 01223 } else { 01224 // Fake a vector. 01225 right = new TabVector(*right, TA_RIGHT_RAGGED, vertical_skew_, 01226 right_blob); 01227 vectors_.add_sorted(TabVector::SortVectorsByKey, right); 01228 v_it_.move_to_first(); 01229 if (WithinTestRegion(3, right_box.right(), right_box.bottom())) { 01230 right->Print("Created new vector"); 01231 } 01232 } 01233 } 01234 left->AddPartner(right); 01235 right->AddPartner(left); 01236 } 01237 01238 // Remove separators and unused tabs from the main vectors_ list 01239 // to the dead_vectors_ list. 01240 void TabFind::CleanupTabs() { 01241 // TODO(rays) Before getting rid of separators and unused vectors, it 01242 // would be useful to try moving ragged vectors outwards to see if this 01243 // allows useful extension. Could be combined with checking ends of partners. 01244 TabVector_IT it(&vectors_); 01245 TabVector_IT dead_it(&dead_vectors_); 01246 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { 01247 TabVector* v = it.data(); 01248 if (v->IsSeparator() || v->Partnerless()) { 01249 dead_it.add_after_then_move(it.extract()); 01250 v_it_.set_to_list(&vectors_); 01251 } else { 01252 v->FitAndEvaluateIfNeeded(vertical_skew_, this); 01253 } 01254 } 01255 } 01256 01257 // Apply the given rotation to the given list of blobs. 01258 void TabFind::RotateBlobList(const FCOORD& rotation, BLOBNBOX_LIST* blobs) { 01259 BLOBNBOX_IT it(blobs); 01260 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { 01261 it.data()->rotate_box(rotation); 01262 } 01263 } 01264 01265 // Recreate the grid with deskewed BLOBNBOXes. 01266 // Returns false if the detected skew angle is impossible. 01267 bool TabFind::Deskew(TabVector_LIST* hlines, BLOBNBOX_LIST* image_blobs, 01268 TO_BLOCK* block, FCOORD* deskew, FCOORD* reskew) { 01269 ComputeDeskewVectors(deskew, reskew); 01270 if (deskew->x() < kCosMaxSkewAngle) 01271 return false; 01272 RotateBlobList(*deskew, image_blobs); 01273 RotateBlobList(*deskew, &block->blobs); 01274 RotateBlobList(*deskew, &block->small_blobs); 01275 RotateBlobList(*deskew, &block->noise_blobs); 01276 if (textord_debug_images) { 01277 // Rotate the debug pix and arrange for it to be drawn at the correct 01278 // pixel offset. 01279 Pix* pix_grey = pixRead(AlignedBlob::textord_debug_pix().string()); 01280 int width = pixGetWidth(pix_grey); 01281 int height = pixGetHeight(pix_grey); 01282 float angle = atan2(deskew->y(), deskew->x()); 01283 // Positive angle is clockwise to pixRotate. 01284 Pix* pix_rot = pixRotate(pix_grey, -angle, L_ROTATE_AREA_MAP, 01285 L_BRING_IN_WHITE, width, height); 01286 // The image must be translated by the rotation of its center, since it 01287 // has just been rotated about its center. 01288 ICOORD center_offset(width / 2, height / 2); 01289 ICOORD new_center_offset(center_offset); 01290 new_center_offset.rotate(*deskew); 01291 image_origin_ += new_center_offset - center_offset; 01292 // The image grew as it was rotated, so offset the (top/left) origin 01293 // by half the change in size. y is opposite to x because it is drawn 01294 // at ist top/left, not bottom/left. 01295 ICOORD corner_offset((width - pixGetWidth(pix_rot)) / 2, 01296 (pixGetHeight(pix_rot) - height) / 2); 01297 image_origin_ += corner_offset; 01298 pixWrite(AlignedBlob::textord_debug_pix().string(), pix_rot, IFF_PNG); 01299 pixDestroy(&pix_grey); 01300 pixDestroy(&pix_rot); 01301 } 01302 01303 // Rotate the horizontal vectors. The vertical vectors don't need 01304 // rotating as they can just be refitted. 01305 TabVector_IT h_it(hlines); 01306 for (h_it.mark_cycle_pt(); !h_it.cycled_list(); h_it.forward()) { 01307 TabVector* h = h_it.data(); 01308 h->Rotate(*deskew); 01309 } 01310 TabVector_IT d_it(&dead_vectors_); 01311 for (d_it.mark_cycle_pt(); !d_it.cycled_list(); d_it.forward()) { 01312 TabVector* d = d_it.data(); 01313 d->Rotate(*deskew); 01314 } 01315 SetVerticalSkewAndParellelize(0, 1); 01316 // Rebuild the grid to the new size. 01317 TBOX grid_box(bleft_, tright_); 01318 grid_box.rotate_large(*deskew); 01319 Init(gridsize(), grid_box.botleft(), grid_box.topright()); 01320 InsertBlobsToGrid(false, false, image_blobs, this); 01321 InsertBlobsToGrid(true, false, &block->blobs, this); 01322 return true; 01323 } 01324 01325 // Flip the vertical and horizontal lines and rotate the grid ready 01326 // for working on the rotated image. 01327 // This also makes parameter adjustments for FindInitialTabVectors(). 01328 void TabFind::ResetForVerticalText(const FCOORD& rotate, const FCOORD& rerotate, 01329 TabVector_LIST* horizontal_lines, 01330 int* min_gutter_width) { 01331 // Rotate the horizontal and vertical vectors and swap them over. 01332 // Only the separators are kept and rotated; other tabs are used 01333 // to estimate the gutter width then thrown away. 01334 TabVector_LIST ex_verticals; 01335 TabVector_IT ex_v_it(&ex_verticals); 01336 TabVector_LIST vlines; 01337 TabVector_IT v_it(&vlines); 01338 while (!v_it_.empty()) { 01339 TabVector* v = v_it_.extract(); 01340 if (v->IsSeparator()) { 01341 v->Rotate(rotate); 01342 ex_v_it.add_after_then_move(v); 01343 } else { 01344 v_it.add_after_then_move(v); 01345 } 01346 v_it_.forward(); 01347 } 01348 01349 // Adjust the min gutter width for better tabbox selection 01350 // in 2nd call to FindInitialTabVectors(). 01351 int median_gutter = FindMedianGutterWidth(&vlines); 01352 if (median_gutter > *min_gutter_width) 01353 *min_gutter_width = median_gutter; 01354 01355 TabVector_IT h_it(horizontal_lines); 01356 for (h_it.mark_cycle_pt(); !h_it.cycled_list(); h_it.forward()) { 01357 TabVector* h = h_it.data(); 01358 h->Rotate(rotate); 01359 } 01360 v_it_.add_list_after(horizontal_lines); 01361 v_it_.move_to_first(); 01362 h_it.set_to_list(horizontal_lines); 01363 h_it.add_list_after(&ex_verticals); 01364 01365 // Rebuild the grid to the new size. 01366 TBOX grid_box(bleft(), tright()); 01367 grid_box.rotate_large(rotate); 01368 Init(gridsize(), grid_box.botleft(), grid_box.topright()); 01369 } 01370 01371 // Clear the grid and get rid of the tab vectors, but not separators, 01372 // ready to start again. 01373 void TabFind::Reset() { 01374 v_it_.move_to_first(); 01375 for (v_it_.mark_cycle_pt(); !v_it_.cycled_list(); v_it_.forward()) { 01376 if (!v_it_.data()->IsSeparator()) 01377 delete v_it_.extract(); 01378 } 01379 Clear(); 01380 } 01381 01382 // Reflect the separator tab vectors and the grids in the y-axis. 01383 // Can only be called after Reset! 01384 void TabFind::ReflectInYAxis() { 01385 TabVector_LIST temp_list; 01386 TabVector_IT temp_it(&temp_list); 01387 v_it_.move_to_first(); 01388 // The TabVector list only contains vertical lines, but they need to be 01389 // reflected and the list needs to be reversed, so they are still in 01390 // sort_key order. 01391 while (!v_it_.empty()) { 01392 TabVector* v = v_it_.extract(); 01393 v_it_.forward(); 01394 v->ReflectInYAxis(); 01395 temp_it.add_before_then_move(v); 01396 } 01397 v_it_.add_list_after(&temp_list); 01398 v_it_.move_to_first(); 01399 // Reset this grid with reflected bounding boxes. 01400 TBOX grid_box(bleft(), tright()); 01401 int tmp = grid_box.left(); 01402 grid_box.set_left(-grid_box.right()); 01403 grid_box.set_right(-tmp); 01404 Init(gridsize(), grid_box.botleft(), grid_box.topright()); 01405 } 01406 01407 // Compute the rotation required to deskew, and its inverse rotation. 01408 void TabFind::ComputeDeskewVectors(FCOORD* deskew, FCOORD* reskew) { 01409 double length = vertical_skew_ % vertical_skew_; 01410 length = sqrt(length); 01411 deskew->set_x(static_cast<float>(vertical_skew_.y() / length)); 01412 deskew->set_y(static_cast<float>(vertical_skew_.x() / length)); 01413 reskew->set_x(deskew->x()); 01414 reskew->set_y(-deskew->y()); 01415 } 01416 01417 // Compute and apply constraints to the end positions of TabVectors so 01418 // that where possible partners end at the same y coordinate. 01419 void TabFind::ApplyTabConstraints() { 01420 TabVector_IT it(&vectors_); 01421 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { 01422 TabVector* v = it.data(); 01423 v->SetupConstraints(); 01424 } 01425 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { 01426 TabVector* v = it.data(); 01427 // With the first and last partner, we want a common bottom and top, 01428 // respectively, and for each change of partner, we want a common 01429 // top of first with bottom of next. 01430 v->SetupPartnerConstraints(); 01431 } 01432 // TODO(rays) The back-to-back pairs should really be done like the 01433 // front-to-front pairs, but there is no convenient way of producing the 01434 // list of partners like there is with the front-to-front. 01435 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { 01436 TabVector* v = it.data(); 01437 if (!v->IsRightTab()) 01438 continue; 01439 // For each back-to-back pair of vectors, try for common top and bottom. 01440 TabVector_IT partner_it(it); 01441 for (partner_it.forward(); !partner_it.at_first(); partner_it.forward()) { 01442 TabVector* partner = partner_it.data(); 01443 if (!partner->IsLeftTab() || !v->VOverlap(*partner)) 01444 continue; 01445 v->SetupPartnerConstraints(partner); 01446 } 01447 } 01448 // Now actually apply the constraints to get common start/end points. 01449 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { 01450 TabVector* v = it.data(); 01451 if (!v->IsSeparator()) 01452 v->ApplyConstraints(); 01453 } 01454 // TODO(rays) Where constraint application fails, it would be good to try 01455 // checking the ends to see if they really should be moved. 01456 } 01457 01458 } // namespace tesseract.