2009-07-11 10:39:56 +08:00
|
|
|
/**********************************************************************
|
|
|
|
* File: pagesegmain.cpp
|
|
|
|
* Description: Top-level page segmenter for Tesseract.
|
|
|
|
* Author: Ray Smith
|
|
|
|
* Created: Thu Sep 25 17:12:01 PDT 2008
|
|
|
|
*
|
|
|
|
* (C) Copyright 2008, Google Inc.
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
2012-03-03 01:31:24 +08:00
|
|
|
#ifdef _WIN32
|
2014-01-22 16:10:15 +08:00
|
|
|
#ifndef unlink
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2009-07-11 10:39:56 +08:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2014-01-29 10:22:14 +08:00
|
|
|
#endif // _WIN32
|
2009-07-11 10:39:56 +08:00
|
|
|
|
|
|
|
// Include automatically generated configuration file if running autoconf.
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config_auto.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "allheaders.h"
|
|
|
|
#include "blobbox.h"
|
|
|
|
#include "blread.h"
|
2012-02-02 10:59:49 +08:00
|
|
|
#include "colfind.h"
|
2017-01-26 08:05:59 +08:00
|
|
|
#include "debugpixa.h"
|
2012-02-02 10:59:49 +08:00
|
|
|
#include "equationdetect.h"
|
|
|
|
#include "imagefind.h"
|
|
|
|
#include "linefind.h"
|
2009-07-11 10:39:56 +08:00
|
|
|
#include "makerow.h"
|
2010-11-24 02:34:14 +08:00
|
|
|
#include "osdetect.h"
|
2012-02-02 10:59:49 +08:00
|
|
|
#include "tabvector.h"
|
|
|
|
#include "tesseractclass.h"
|
|
|
|
#include "tessvars.h"
|
2010-11-24 02:34:14 +08:00
|
|
|
#include "textord.h"
|
2009-07-11 10:39:56 +08:00
|
|
|
#include "tordmain.h"
|
2012-02-02 10:59:49 +08:00
|
|
|
#include "wordseg.h"
|
2009-07-11 10:39:56 +08:00
|
|
|
|
|
|
|
namespace tesseract {
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
// Max erosions to perform in removing an enclosing circle.
|
|
|
|
const int kMaxCircleErosions = 8;
|
|
|
|
|
|
|
|
// Helper to remove an enclosing circle from an image.
|
|
|
|
// If there isn't one, then the image will most likely get badly mangled.
|
2016-12-13 00:19:35 +08:00
|
|
|
// The returned pix must be pixDestroyed after use. nullptr may be returned
|
2010-11-24 02:34:14 +08:00
|
|
|
// if the image doesn't meet the trivial conditions that it uses to determine
|
|
|
|
// success.
|
|
|
|
static Pix* RemoveEnclosingCircle(Pix* pixs) {
|
2016-12-13 00:19:35 +08:00
|
|
|
Pix* pixsi = pixInvert(nullptr, pixs);
|
2010-11-24 02:34:14 +08:00
|
|
|
Pix* pixc = pixCreateTemplate(pixs);
|
|
|
|
pixSetOrClearBorder(pixc, 1, 1, 1, 1, PIX_SET);
|
|
|
|
pixSeedfillBinary(pixc, pixc, pixsi, 4);
|
|
|
|
pixInvert(pixc, pixc);
|
|
|
|
pixDestroy(&pixsi);
|
2016-12-13 00:19:35 +08:00
|
|
|
Pix* pixt = pixAnd(nullptr, pixs, pixc);
|
2010-11-24 02:34:14 +08:00
|
|
|
l_int32 max_count;
|
|
|
|
pixCountConnComp(pixt, 8, &max_count);
|
|
|
|
// The count has to go up before we start looking for the minimum.
|
Use POSIX data types and macros (#878)
* api: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* cutil: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* training: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract data types by POSIX data types
Now all Tesseract data types which are no longer needed can be removed
from ccutil/host.h.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* lstm: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Remove the macros which are now unused from ccutil/host.h.
Remove also the obsolete history comments.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* Fix build error caused by ambiguous ClipToRange
Error message vom Appveyor CI:
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2672: 'ClipToRange': no matching overloaded function found [C:\projects\tesseract\build\libtesseract.vcxproj]
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2782: 'T ClipToRange(const T &,const T &,const T &)': template parameter 'T' is ambiguous [C:\projects\tesseract\build\libtesseract.vcxproj]
c:\projects\tesseract\ccutil\helpers.h(122): note: see declaration of 'ClipToRange'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: could be 'char'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: or 'int'
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* unittest: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* arch: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2018-03-14 04:36:30 +08:00
|
|
|
l_int32 min_count = INT32_MAX;
|
2016-12-13 00:19:35 +08:00
|
|
|
Pix* pixout = nullptr;
|
2010-11-24 02:34:14 +08:00
|
|
|
for (int i = 1; i < kMaxCircleErosions; i++) {
|
|
|
|
pixDestroy(&pixt);
|
|
|
|
pixErodeBrick(pixc, pixc, 3, 3);
|
2016-12-13 00:19:35 +08:00
|
|
|
pixt = pixAnd(nullptr, pixs, pixc);
|
2010-11-24 02:34:14 +08:00
|
|
|
l_int32 count;
|
|
|
|
pixCountConnComp(pixt, 8, &count);
|
|
|
|
if (i == 1 || count > max_count) {
|
|
|
|
max_count = count;
|
|
|
|
min_count = count;
|
|
|
|
} else if (i > 1 && count < min_count) {
|
|
|
|
min_count = count;
|
|
|
|
pixDestroy(&pixout);
|
2016-12-13 00:19:35 +08:00
|
|
|
pixout = pixCopy(nullptr, pixt); // Save the best.
|
2010-11-24 02:34:14 +08:00
|
|
|
} else if (count >= min_count) {
|
|
|
|
break; // We have passed by the best.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pixDestroy(&pixt);
|
|
|
|
pixDestroy(&pixc);
|
|
|
|
return pixout;
|
|
|
|
}
|
2009-07-11 10:39:56 +08:00
|
|
|
|
2010-07-27 22:58:33 +08:00
|
|
|
/**
|
|
|
|
* Segment the page according to the current value of tessedit_pageseg_mode.
|
2016-12-13 00:19:35 +08:00
|
|
|
* pix_binary_ is used as the source image and should not be nullptr.
|
2010-07-27 22:58:33 +08:00
|
|
|
* On return the blocks list owns all the constructed page layout.
|
|
|
|
*/
|
2010-11-24 02:34:14 +08:00
|
|
|
int Tesseract::SegmentPage(const STRING* input_file, BLOCK_LIST* blocks,
|
|
|
|
Tesseract* osd_tess, OSResults* osr) {
|
2016-12-13 00:19:35 +08:00
|
|
|
ASSERT_HOST(pix_binary_ != nullptr);
|
2010-11-24 02:34:14 +08:00
|
|
|
int width = pixGetWidth(pix_binary_);
|
|
|
|
int height = pixGetHeight(pix_binary_);
|
2009-07-11 10:39:56 +08:00
|
|
|
// Get page segmentation mode.
|
|
|
|
PageSegMode pageseg_mode = static_cast<PageSegMode>(
|
|
|
|
static_cast<int>(tessedit_pageseg_mode));
|
|
|
|
// If a UNLV zone file can be found, use that instead of segmentation.
|
2010-11-24 02:34:14 +08:00
|
|
|
if (!PSM_COL_FIND_ENABLED(pageseg_mode) &&
|
2016-12-13 00:19:35 +08:00
|
|
|
input_file != nullptr && input_file->length() > 0) {
|
2009-07-11 10:39:56 +08:00
|
|
|
STRING name = *input_file;
|
|
|
|
const char* lastdot = strrchr(name.string(), '.');
|
2016-12-13 00:19:35 +08:00
|
|
|
if (lastdot != nullptr)
|
2009-07-11 10:39:56 +08:00
|
|
|
name[lastdot - name.string()] = '\0';
|
|
|
|
read_unlv_file(name, width, height, blocks);
|
|
|
|
}
|
|
|
|
if (blocks->empty()) {
|
|
|
|
// No UNLV file present. Work according to the PageSegMode.
|
|
|
|
// First make a single block covering the whole image.
|
|
|
|
BLOCK_IT block_it(blocks);
|
|
|
|
BLOCK* block = new BLOCK("", TRUE, 0, 0, 0, 0, width, height);
|
2010-11-24 02:34:14 +08:00
|
|
|
block->set_right_to_left(right_to_left());
|
2009-07-11 10:39:56 +08:00
|
|
|
block_it.add_to_end(block);
|
|
|
|
} else {
|
2011-03-19 05:52:08 +08:00
|
|
|
// UNLV file present. Use PSM_SINGLE_BLOCK.
|
|
|
|
pageseg_mode = PSM_SINGLE_BLOCK;
|
2009-07-11 10:39:56 +08:00
|
|
|
}
|
2015-05-13 07:47:02 +08:00
|
|
|
// The diacritic_blobs holds noise blobs that may be diacritics. They
|
|
|
|
// are separated out on areas of the image that seem noisy and short-circuit
|
|
|
|
// the layout process, going straight from the initial partition creation
|
|
|
|
// right through to after word segmentation, where they are added to the
|
|
|
|
// rej_cblobs list of the most appropriate word. From there classification
|
|
|
|
// will determine whether they are used.
|
|
|
|
BLOBNBOX_LIST diacritic_blobs;
|
2010-11-24 02:34:14 +08:00
|
|
|
int auto_page_seg_ret_val = 0;
|
|
|
|
TO_BLOCK_LIST to_blocks;
|
2013-01-04 03:06:41 +08:00
|
|
|
if (PSM_OSD_ENABLED(pageseg_mode) || PSM_BLOCK_FIND_ENABLED(pageseg_mode) ||
|
|
|
|
PSM_SPARSE(pageseg_mode)) {
|
2015-05-13 07:47:02 +08:00
|
|
|
auto_page_seg_ret_val = AutoPageSeg(
|
|
|
|
pageseg_mode, blocks, &to_blocks,
|
2016-12-13 00:19:35 +08:00
|
|
|
enable_noise_removal ? &diacritic_blobs : nullptr, osd_tess, osr);
|
2013-01-04 03:06:41 +08:00
|
|
|
if (pageseg_mode == PSM_OSD_ONLY)
|
2010-11-24 02:34:14 +08:00
|
|
|
return auto_page_seg_ret_val;
|
2009-07-11 10:39:56 +08:00
|
|
|
// To create blobs from the image region bounds uncomment this line:
|
2010-11-24 02:34:14 +08:00
|
|
|
// to_blocks.clear(); // Uncomment to go back to the old mode.
|
2009-07-11 10:39:56 +08:00
|
|
|
} else {
|
|
|
|
deskew_ = FCOORD(1.0f, 0.0f);
|
|
|
|
reskew_ = FCOORD(1.0f, 0.0f);
|
2010-11-24 02:34:14 +08:00
|
|
|
if (pageseg_mode == PSM_CIRCLE_WORD) {
|
|
|
|
Pix* pixcleaned = RemoveEnclosingCircle(pix_binary_);
|
2016-12-13 00:19:35 +08:00
|
|
|
if (pixcleaned != nullptr) {
|
2010-11-24 02:34:14 +08:00
|
|
|
pixDestroy(&pix_binary_);
|
|
|
|
pix_binary_ = pixcleaned;
|
|
|
|
}
|
|
|
|
}
|
2009-07-11 10:39:56 +08:00
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
|
|
|
|
if (auto_page_seg_ret_val < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-07-11 10:39:56 +08:00
|
|
|
if (blocks->empty()) {
|
2011-03-22 05:44:05 +08:00
|
|
|
if (textord_debug_tabfind)
|
|
|
|
tprintf("Empty page\n");
|
2009-07-11 10:39:56 +08:00
|
|
|
return 0; // AutoPageSeg found an empty page.
|
|
|
|
}
|
2013-09-23 23:19:50 +08:00
|
|
|
bool splitting =
|
|
|
|
pageseg_devanagari_split_strategy != ShiroRekhaSplitter::NO_SPLIT;
|
|
|
|
bool cjk_mode = textord_use_cjk_fp_model;
|
2009-07-11 10:39:56 +08:00
|
|
|
|
2013-09-23 23:19:50 +08:00
|
|
|
textord_.TextordPage(pageseg_mode, reskew_, width, height, pix_binary_,
|
|
|
|
pix_thresholds_, pix_grey_, splitting || cjk_mode,
|
2015-05-13 07:47:02 +08:00
|
|
|
&diacritic_blobs, blocks, &to_blocks);
|
2010-11-24 02:34:14 +08:00
|
|
|
return auto_page_seg_ret_val;
|
|
|
|
}
|
2009-07-11 10:39:56 +08:00
|
|
|
|
2010-07-27 22:58:33 +08:00
|
|
|
/**
|
|
|
|
* Auto page segmentation. Divide the page image into blocks of uniform
|
|
|
|
* text linespacing and images.
|
|
|
|
*
|
2010-11-24 02:34:14 +08:00
|
|
|
* Resolution (in ppi) is derived from the input image.
|
2010-07-27 22:58:33 +08:00
|
|
|
*
|
|
|
|
* The output goes in the blocks list with corresponding TO_BLOCKs in the
|
|
|
|
* to_blocks list.
|
|
|
|
*
|
2015-05-13 07:47:02 +08:00
|
|
|
* If !PSM_COL_FIND_ENABLED(pageseg_mode), then no attempt is made to divide
|
|
|
|
* the image into columns, but multiple blocks are still made if the text is
|
|
|
|
* of non-uniform linespacing.
|
|
|
|
*
|
|
|
|
* If diacritic_blobs is non-null, then diacritics/noise blobs, that would
|
2018-08-14 22:15:24 +08:00
|
|
|
* confuse layout analysis by causing textline overlap, are placed there,
|
2015-05-13 07:47:02 +08:00
|
|
|
* with the expectation that they will be reassigned to words later and
|
|
|
|
* noise/diacriticness determined via classification.
|
2010-11-24 02:34:14 +08:00
|
|
|
*
|
2012-02-02 10:59:49 +08:00
|
|
|
* If osd (orientation and script detection) is true then that is performed
|
|
|
|
* as well. If only_osd is true, then only orientation and script detection is
|
|
|
|
* performed. If osd is desired, (osd or only_osd) then osr_tess must be
|
|
|
|
* another Tesseract that was initialized especially for osd, and the results
|
|
|
|
* will be output into osr (orientation and script result).
|
2010-07-27 22:58:33 +08:00
|
|
|
*/
|
2015-05-13 07:47:02 +08:00
|
|
|
int Tesseract::AutoPageSeg(PageSegMode pageseg_mode, BLOCK_LIST* blocks,
|
|
|
|
TO_BLOCK_LIST* to_blocks,
|
|
|
|
BLOBNBOX_LIST* diacritic_blobs, Tesseract* osd_tess,
|
|
|
|
OSResults* osr) {
|
2016-12-13 00:19:35 +08:00
|
|
|
Pix* photomask_pix = nullptr;
|
|
|
|
Pix* musicmask_pix = nullptr;
|
2009-07-11 10:39:56 +08:00
|
|
|
// The blocks made by the ColumnFinder. Moved to blocks before return.
|
|
|
|
BLOCK_LIST found_blocks;
|
2012-02-02 10:59:49 +08:00
|
|
|
TO_BLOCK_LIST temp_blocks;
|
2009-07-11 10:39:56 +08:00
|
|
|
|
2012-02-02 10:59:49 +08:00
|
|
|
ColumnFinder* finder = SetupPageSegAndDetectOrientation(
|
2015-06-13 02:05:00 +08:00
|
|
|
pageseg_mode, blocks, osd_tess, osr, &temp_blocks, &photomask_pix,
|
|
|
|
&musicmask_pix);
|
2013-01-04 03:06:41 +08:00
|
|
|
int result = 0;
|
2016-12-13 00:19:35 +08:00
|
|
|
if (finder != nullptr) {
|
2012-02-02 10:59:49 +08:00
|
|
|
TO_BLOCK_IT to_block_it(&temp_blocks);
|
|
|
|
TO_BLOCK* to_block = to_block_it.data();
|
2016-12-13 00:19:35 +08:00
|
|
|
if (musicmask_pix != nullptr) {
|
2012-02-02 10:59:49 +08:00
|
|
|
// TODO(rays) pass the musicmask_pix into FindBlocks and mark music
|
|
|
|
// blocks separately. For now combine with photomask_pix.
|
|
|
|
pixOr(photomask_pix, photomask_pix, musicmask_pix);
|
2009-07-11 10:39:56 +08:00
|
|
|
}
|
2012-02-02 10:59:49 +08:00
|
|
|
if (equ_detect_) {
|
|
|
|
finder->SetEquationDetect(equ_detect_);
|
2010-11-24 02:34:14 +08:00
|
|
|
}
|
2017-01-26 08:05:59 +08:00
|
|
|
result = finder->FindBlocks(pageseg_mode, scaled_color_, scaled_factor_,
|
|
|
|
to_block, photomask_pix, pix_thresholds_,
|
|
|
|
pix_grey_, &pixa_debug_, &found_blocks,
|
|
|
|
diacritic_blobs, to_blocks);
|
2013-01-04 03:06:41 +08:00
|
|
|
if (result >= 0)
|
|
|
|
finder->GetDeskewVectors(&deskew_, &reskew_);
|
2012-02-02 10:59:49 +08:00
|
|
|
delete finder;
|
2009-07-11 10:39:56 +08:00
|
|
|
}
|
2012-02-02 10:59:49 +08:00
|
|
|
pixDestroy(&photomask_pix);
|
|
|
|
pixDestroy(&musicmask_pix);
|
2013-01-04 03:06:41 +08:00
|
|
|
if (result < 0) return result;
|
|
|
|
|
2009-07-11 10:39:56 +08:00
|
|
|
blocks->clear();
|
|
|
|
BLOCK_IT block_it(blocks);
|
|
|
|
// Move the found blocks to the input/output blocks.
|
|
|
|
block_it.add_list_after(&found_blocks);
|
2013-01-04 03:06:41 +08:00
|
|
|
return result;
|
2009-07-11 10:39:56 +08:00
|
|
|
}
|
|
|
|
|
2014-02-04 03:16:42 +08:00
|
|
|
// Helper adds all the scripts from sid_set converted to ids from osd_set to
|
|
|
|
// allowed_ids.
|
|
|
|
static void AddAllScriptsConverted(const UNICHARSET& sid_set,
|
|
|
|
const UNICHARSET& osd_set,
|
|
|
|
GenericVector<int>* allowed_ids) {
|
|
|
|
for (int i = 0; i < sid_set.get_script_table_size(); ++i) {
|
|
|
|
if (i != sid_set.null_sid()) {
|
|
|
|
const char* script = sid_set.get_script_from_script_id(i);
|
|
|
|
allowed_ids->push_back(osd_set.get_script_id_from_name(script));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 10:59:49 +08:00
|
|
|
/**
|
|
|
|
* Sets up auto page segmentation, determines the orientation, and corrects it.
|
|
|
|
* Somewhat arbitrary chunk of functionality, factored out of AutoPageSeg to
|
|
|
|
* facilitate testing.
|
2016-12-13 00:19:35 +08:00
|
|
|
* photo_mask_pix is a pointer to a nullptr pointer that will be filled on return
|
2012-02-02 10:59:49 +08:00
|
|
|
* with the leptonica photo mask, which must be pixDestroyed by the caller.
|
|
|
|
* to_blocks is an empty list that will be filled with (usually a single)
|
|
|
|
* block that is used during layout analysis. This ugly API is required
|
|
|
|
* because of the possibility of a unlv zone file.
|
|
|
|
* TODO(rays) clean this up.
|
|
|
|
* See AutoPageSeg for other arguments.
|
|
|
|
* The returned ColumnFinder must be deleted after use.
|
|
|
|
*/
|
|
|
|
ColumnFinder* Tesseract::SetupPageSegAndDetectOrientation(
|
2015-06-13 02:05:00 +08:00
|
|
|
PageSegMode pageseg_mode, BLOCK_LIST* blocks, Tesseract* osd_tess,
|
|
|
|
OSResults* osr, TO_BLOCK_LIST* to_blocks, Pix** photo_mask_pix,
|
|
|
|
Pix** music_mask_pix) {
|
2012-02-02 10:59:49 +08:00
|
|
|
int vertical_x = 0;
|
|
|
|
int vertical_y = 1;
|
|
|
|
TabVector_LIST v_lines;
|
|
|
|
TabVector_LIST h_lines;
|
|
|
|
ICOORD bleft(0, 0);
|
|
|
|
|
2016-12-13 00:19:35 +08:00
|
|
|
ASSERT_HOST(pix_binary_ != nullptr);
|
2012-02-02 10:59:49 +08:00
|
|
|
if (tessedit_dump_pageseg_images) {
|
2017-01-26 08:05:59 +08:00
|
|
|
pixa_debug_.AddPix(pix_binary_, "PageSegInput");
|
2012-02-02 10:59:49 +08:00
|
|
|
}
|
|
|
|
// Leptonica is used to find the rule/separator lines in the input.
|
|
|
|
LineFinder::FindAndRemoveLines(source_resolution_,
|
|
|
|
textord_tabfind_show_vlines, pix_binary_,
|
|
|
|
&vertical_x, &vertical_y, music_mask_pix,
|
|
|
|
&v_lines, &h_lines);
|
2017-01-26 08:05:59 +08:00
|
|
|
if (tessedit_dump_pageseg_images) {
|
|
|
|
pixa_debug_.AddPix(pix_binary_, "NoLines");
|
|
|
|
}
|
2012-02-02 10:59:49 +08:00
|
|
|
// Leptonica is used to find a mask of the photo regions in the input.
|
2017-01-26 08:05:59 +08:00
|
|
|
*photo_mask_pix = ImageFind::FindImages(pix_binary_, &pixa_debug_);
|
|
|
|
if (tessedit_dump_pageseg_images) {
|
|
|
|
pixa_debug_.AddPix(pix_binary_, "NoImages");
|
|
|
|
}
|
2015-06-13 02:05:00 +08:00
|
|
|
if (!PSM_COL_FIND_ENABLED(pageseg_mode)) v_lines.clear();
|
2012-02-02 10:59:49 +08:00
|
|
|
|
|
|
|
// The rest of the algorithm uses the usual connected components.
|
|
|
|
textord_.find_components(pix_binary_, blocks, to_blocks);
|
|
|
|
|
|
|
|
TO_BLOCK_IT to_block_it(to_blocks);
|
|
|
|
// There must be exactly one input block.
|
|
|
|
// TODO(rays) handle new textline finding with a UNLV zone file.
|
|
|
|
ASSERT_HOST(to_blocks->singleton());
|
|
|
|
TO_BLOCK* to_block = to_block_it.data();
|
2018-04-19 19:55:39 +08:00
|
|
|
TBOX blkbox = to_block->block->pdblk.bounding_box();
|
2016-12-13 00:19:35 +08:00
|
|
|
ColumnFinder* finder = nullptr;
|
2017-09-08 16:37:03 +08:00
|
|
|
int estimated_resolution = source_resolution_;
|
|
|
|
if (source_resolution_ == kMinCredibleResolution) {
|
|
|
|
// Try to estimate resolution from typical body text size.
|
|
|
|
int res = IntCastRounded(to_block->line_size * kResolutionEstimationFactor);
|
|
|
|
if (res > estimated_resolution && res < kMaxCredibleResolution) {
|
|
|
|
estimated_resolution = res;
|
|
|
|
tprintf("Estimating resolution as %d\n", estimated_resolution);
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 10:59:49 +08:00
|
|
|
|
|
|
|
if (to_block->line_size >= 2) {
|
|
|
|
finder = new ColumnFinder(static_cast<int>(to_block->line_size),
|
|
|
|
blkbox.botleft(), blkbox.topright(),
|
2017-09-08 16:37:03 +08:00
|
|
|
estimated_resolution, textord_use_cjk_fp_model,
|
|
|
|
textord_tabfind_aligned_gap_fraction, &v_lines,
|
|
|
|
&h_lines, vertical_x, vertical_y);
|
2012-02-02 10:59:49 +08:00
|
|
|
|
2015-06-13 02:05:00 +08:00
|
|
|
finder->SetupAndFilterNoise(pageseg_mode, *photo_mask_pix, to_block);
|
2012-02-02 10:59:49 +08:00
|
|
|
|
2018-07-03 23:49:42 +08:00
|
|
|
#ifndef DISABLED_LEGACY_ENGINE
|
|
|
|
|
2012-02-02 10:59:49 +08:00
|
|
|
if (equ_detect_) {
|
|
|
|
equ_detect_->LabelSpecialText(to_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
BLOBNBOX_CLIST osd_blobs;
|
|
|
|
// osd_orientation is the number of 90 degree rotations to make the
|
|
|
|
// characters upright. (See osdetect.h for precise definition.)
|
|
|
|
// We want the text lines horizontal, (vertical text indicates vertical
|
|
|
|
// textlines) which may conflict (eg vertically written CJK).
|
|
|
|
int osd_orientation = 0;
|
2015-06-13 02:05:00 +08:00
|
|
|
bool vertical_text = textord_tabfind_force_vertical_text ||
|
|
|
|
pageseg_mode == PSM_SINGLE_BLOCK_VERT_TEXT;
|
|
|
|
if (!vertical_text && textord_tabfind_vertical_text &&
|
|
|
|
PSM_ORIENTATION_ENABLED(pageseg_mode)) {
|
2014-10-08 00:31:00 +08:00
|
|
|
vertical_text =
|
|
|
|
finder->IsVerticallyAlignedText(textord_tabfind_vertical_text_ratio,
|
|
|
|
to_block, &osd_blobs);
|
|
|
|
}
|
2016-12-13 00:19:35 +08:00
|
|
|
if (PSM_OSD_ENABLED(pageseg_mode) && osd_tess != nullptr && osr != nullptr) {
|
2014-02-04 03:16:42 +08:00
|
|
|
GenericVector<int> osd_scripts;
|
|
|
|
if (osd_tess != this) {
|
|
|
|
// We are running osd as part of layout analysis, so constrain the
|
|
|
|
// scripts to those allowed by *this.
|
|
|
|
AddAllScriptsConverted(unicharset, osd_tess->unicharset, &osd_scripts);
|
|
|
|
for (int s = 0; s < sub_langs_.size(); ++s) {
|
|
|
|
AddAllScriptsConverted(sub_langs_[s]->unicharset,
|
|
|
|
osd_tess->unicharset, &osd_scripts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os_detect_blobs(&osd_scripts, &osd_blobs, osr, osd_tess);
|
2015-06-13 02:05:00 +08:00
|
|
|
if (pageseg_mode == PSM_OSD_ONLY) {
|
2012-02-02 10:59:49 +08:00
|
|
|
delete finder;
|
2016-12-13 00:19:35 +08:00
|
|
|
return nullptr;
|
2012-02-02 10:59:49 +08:00
|
|
|
}
|
|
|
|
osd_orientation = osr->best_result.orientation_id;
|
|
|
|
double osd_score = osr->orientations[osd_orientation];
|
|
|
|
double osd_margin = min_orientation_margin * 2;
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (i != osd_orientation &&
|
|
|
|
osd_score - osr->orientations[i] < osd_margin) {
|
|
|
|
osd_margin = osd_score - osr->orientations[i];
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 10:20:59 +08:00
|
|
|
int best_script_id = osr->best_result.script_id;
|
|
|
|
const char* best_script_str =
|
|
|
|
osd_tess->unicharset.get_script_from_script_id(best_script_id);
|
|
|
|
bool cjk = best_script_id == osd_tess->unicharset.han_sid() ||
|
|
|
|
best_script_id == osd_tess->unicharset.hiragana_sid() ||
|
|
|
|
best_script_id == osd_tess->unicharset.katakana_sid() ||
|
|
|
|
strcmp("Japanese", best_script_str) == 0 ||
|
|
|
|
strcmp("Korean", best_script_str) == 0 ||
|
|
|
|
strcmp("Hangul", best_script_str) == 0;
|
|
|
|
if (cjk) {
|
|
|
|
finder->set_cjk_script(true);
|
|
|
|
}
|
2012-02-02 10:59:49 +08:00
|
|
|
if (osd_margin < min_orientation_margin) {
|
|
|
|
// The margin is weak.
|
|
|
|
if (!cjk && !vertical_text && osd_orientation == 2) {
|
|
|
|
// upside down latin text is improbable with such a weak margin.
|
|
|
|
tprintf("OSD: Weak margin (%.2f), horiz textlines, not CJK: "
|
|
|
|
"Don't rotate.\n", osd_margin);
|
|
|
|
osd_orientation = 0;
|
|
|
|
} else {
|
2016-11-08 02:46:33 +08:00
|
|
|
tprintf(
|
|
|
|
"OSD: Weak margin (%.2f) for %d blob text block, "
|
|
|
|
"but using orientation anyway: %d\n",
|
|
|
|
osd_margin, osd_blobs.length(), osd_orientation);
|
2012-02-02 10:59:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
osd_blobs.shallow_clear();
|
|
|
|
finder->CorrectOrientation(to_block, vertical_text, osd_orientation);
|
2018-07-03 23:49:42 +08:00
|
|
|
|
|
|
|
#endif // ndef DISABLED_LEGACY_ENGINE
|
2012-02-02 10:59:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return finder;
|
|
|
|
}
|
|
|
|
|
2009-07-11 10:39:56 +08:00
|
|
|
} // namespace tesseract.
|