29 #include "config_auto.h" 32 #include "allheaders.h" 59 static Pix* RemoveEnclosingCircle(Pix* pixs) {
60 Pix* pixsi = pixInvert(
nullptr, pixs);
61 Pix* pixc = pixCreateTemplate(pixs);
62 pixSetOrClearBorder(pixc, 1, 1, 1, 1, PIX_SET);
63 pixSeedfillBinary(pixc, pixc, pixsi, 4);
64 pixInvert(pixc, pixc);
66 Pix* pixt = pixAnd(
nullptr, pixs, pixc);
68 pixCountConnComp(pixt, 8, &max_count);
70 l_int32 min_count = INT32_MAX;
71 Pix* pixout =
nullptr;
74 pixErodeBrick(pixc, pixc, 3, 3);
75 pixt = pixAnd(
nullptr, pixs, pixc);
77 pixCountConnComp(pixt, 8, &
count);
78 if (i == 1 ||
count > max_count) {
81 }
else if (i > 1 &&
count < min_count) {
84 pixout = pixCopy(
nullptr, pixt);
85 }
else if (
count >= min_count) {
102 int width = pixGetWidth(pix_binary_);
103 int height = pixGetHeight(pix_binary_);
109 input_file !=
nullptr && input_file->
length() > 0) {
110 STRING name = *input_file;
111 const char* lastdot = strrchr(name.
string(),
'.');
112 if (lastdot !=
nullptr)
113 name[lastdot - name.
string()] =
'\0';
116 if (blocks->empty()) {
119 BLOCK_IT block_it(blocks);
120 auto* block =
new BLOCK(
"",
true, 0, 0, 0, 0, width, height);
122 block_it.add_to_end(block);
133 BLOBNBOX_LIST diacritic_blobs;
134 int auto_page_seg_ret_val = 0;
135 TO_BLOCK_LIST to_blocks;
139 pageseg_mode, blocks, &to_blocks,
142 return auto_page_seg_ret_val;
146 deskew_ =
FCOORD(1.0f, 0.0f);
147 reskew_ =
FCOORD(1.0f, 0.0f);
149 Pix* pixcleaned = RemoveEnclosingCircle(pix_binary_);
150 if (pixcleaned !=
nullptr) {
151 pixDestroy(&pix_binary_);
152 pix_binary_ = pixcleaned;
157 if (auto_page_seg_ret_val < 0) {
161 if (blocks->empty()) {
170 textord_.
TextordPage(pageseg_mode, reskew_, width, height, pix_binary_,
171 pix_thresholds_, pix_grey_, splitting || cjk_mode,
172 &diacritic_blobs, blocks, &to_blocks);
173 return auto_page_seg_ret_val;
201 TO_BLOCK_LIST* to_blocks,
202 BLOBNBOX_LIST* diacritic_blobs,
Tesseract* osd_tess,
204 Pix* photomask_pix =
nullptr;
205 Pix* musicmask_pix =
nullptr;
207 BLOCK_LIST found_blocks;
208 TO_BLOCK_LIST temp_blocks;
211 pageseg_mode, blocks, osd_tess, osr, &temp_blocks, &photomask_pix,
214 if (finder !=
nullptr) {
215 TO_BLOCK_IT to_block_it(&temp_blocks);
216 TO_BLOCK* to_block = to_block_it.data();
217 if (musicmask_pix !=
nullptr) {
220 pixOr(photomask_pix, photomask_pix, musicmask_pix);
225 result = finder->
FindBlocks(pageseg_mode, scaled_color_, scaled_factor_,
226 to_block, photomask_pix, pix_thresholds_,
227 pix_grey_, &pixa_debug_, &found_blocks,
228 diacritic_blobs, to_blocks);
233 pixDestroy(&photomask_pix);
234 pixDestroy(&musicmask_pix);
235 if (result < 0)
return result;
238 BLOCK_IT block_it(blocks);
240 block_it.add_list_after(&found_blocks);
246 static void AddAllScriptsConverted(
const UNICHARSET& sid_set,
272 OSResults* osr, TO_BLOCK_LIST* to_blocks, Pix** photo_mask_pix,
273 Pix** music_mask_pix) {
276 TabVector_LIST v_lines;
277 TabVector_LIST h_lines;
282 pixa_debug_.
AddPix(pix_binary_,
"PageSegInput");
287 &vertical_x, &vertical_y, music_mask_pix,
290 pixa_debug_.
AddPix(pix_binary_,
"NoLines");
295 pixa_debug_.
AddPix(pix_binary_,
"NoImages");
302 TO_BLOCK_IT to_block_it(to_blocks);
306 TO_BLOCK* to_block = to_block_it.data();
307 TBOX blkbox = to_block->block->pdblk.bounding_box();
309 int estimated_resolution = source_resolution_;
314 estimated_resolution = res;
315 tprintf(
"Estimating resolution as %d\n", estimated_resolution);
319 if (to_block->line_size >= 2) {
320 finder =
new ColumnFinder(static_cast<int>(to_block->line_size),
324 &h_lines, vertical_x, vertical_y);
328 #ifndef DISABLED_LEGACY_ENGINE 334 BLOBNBOX_CLIST osd_blobs;
339 int osd_orientation = 0;
346 to_block, &osd_blobs);
348 if (
PSM_OSD_ENABLED(pageseg_mode) && osd_tess !=
nullptr && osr !=
nullptr) {
350 if (osd_tess !=
this) {
354 for (
int s = 0; s < sub_langs_.size(); ++s) {
355 AddAllScriptsConverted(sub_langs_[s]->
unicharset,
367 for (
int i = 0; i < 4; ++i) {
368 if (i != osd_orientation &&
374 const char* best_script_str =
379 strcmp(
"Japanese", best_script_str) == 0 ||
380 strcmp(
"Korean", best_script_str) == 0 ||
381 strcmp(
"Hangul", best_script_str) == 0;
387 if (!cjk && !vertical_text && osd_orientation == 2) {
389 tprintf(
"OSD: Weak margin (%.2f), horiz textlines, not CJK: " 390 "Don't rotate.\n", osd_margin);
394 "OSD: Weak margin (%.2f) for %d blob text block, " 395 "but using orientation anyway: %d\n",
396 osd_margin, osd_blobs.length(), osd_orientation);
400 osd_blobs.shallow_clear();
403 #endif // ndef DISABLED_LEGACY_ENGINE
const char * get_script_from_script_id(int id) const
int textord_debug_tabfind
int get_script_id_from_name(const char *script_name) const
bool PSM_SPARSE(int pageseg_mode)
DLLSYM void tprintf(const char *format,...)
static Pix * FindImages(Pix *pix, DebugPixa *pixa_debug)
int pageseg_devanagari_split_strategy
void SetEquationDetect(EquationDetectBase *detect)
void SetupAndFilterNoise(PageSegMode pageseg_mode, Pix *photo_mask_pix, TO_BLOCK *input_block)
constexpr int kMinCredibleResolution
int os_detect_blobs(const GenericVector< int > *allowed_scripts, BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
double textord_tabfind_aligned_gap_fraction
int AutoPageSeg(PageSegMode pageseg_mode, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks, BLOBNBOX_LIST *diacritic_blobs, Tesseract *osd_tess, OSResults *osr)
Orientation and script detection only.
static void FindAndRemoveLines(int resolution, bool debug, Pix *pix, int *vertical_x, int *vertical_y, Pix **pix_music_mask, TabVector_LIST *v_lines, TabVector_LIST *h_lines)
void AddPix(const Pix *pix, const char *caption)
bool textord_tabfind_vertical_text
bool PSM_COL_FIND_ENABLED(int pageseg_mode)
bool read_unlv_file(STRING name, int32_t xsize, int32_t ysize, BLOCK_LIST *blocks)
int FindBlocks(PageSegMode pageseg_mode, Pix *scaled_color, int scaled_factor, TO_BLOCK *block, Pix *photo_mask_pix, Pix *thresholds_pix, Pix *grey_pix, DebugPixa *pixa_debug, BLOCK_LIST *blocks, BLOBNBOX_LIST *diacritic_blobs, TO_BLOCK_LIST *to_blocks)
const int kMaxCircleErosions
const char * string() const
bool PSM_ORIENTATION_ENABLED(int pageseg_mode)
double min_orientation_margin
bool PSM_BLOCK_FIND_ENABLED(int pageseg_mode)
void find_components(Pix *pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
int LabelSpecialText(TO_BLOCK *to_block) override
bool textord_tabfind_show_vlines
int SegmentPage(const STRING *input_file, BLOCK_LIST *blocks, Tesseract *osd_tess, OSResults *osr)
bool IsVerticallyAlignedText(double find_vertical_text_ratio, TO_BLOCK *block, BLOBNBOX_CLIST *osd_blobs)
void TextordPage(PageSegMode pageseg_mode, const FCOORD &reskew, int width, int height, Pix *binary_pix, Pix *thresholds_pix, Pix *grey_pix, bool use_box_bottoms, BLOBNBOX_LIST *diacritic_blobs, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
bool tessedit_dump_pageseg_images
const ICOORD & botleft() const
bool pageseg_apply_music_mask
void GetDeskewVectors(FCOORD *deskew, FCOORD *reskew)
void set_cjk_script(bool is_cjk)
ColumnFinder * SetupPageSegAndDetectOrientation(PageSegMode pageseg_mode, BLOCK_LIST *blocks, Tesseract *osd_tess, OSResults *osr, TO_BLOCK_LIST *to_blocks, Pix **photo_mask_pix, Pix **music_mask_pix)
bool textord_use_cjk_fp_model
Assume a single uniform block of text. (Default.)
int get_script_table_size() const
const ICOORD & topright() const
bool textord_tabfind_force_vertical_text
constexpr int kResolutionEstimationFactor
void CorrectOrientation(TO_BLOCK *block, bool vertical_text_lines, int recognition_rotation)
double textord_tabfind_vertical_text_ratio
Treat the image as a single word in a circle.
bool enable_noise_removal
constexpr int kMaxCredibleResolution
int IntCastRounded(double x)
bool right_to_left() const
bool PSM_OSD_ENABLED(int pageseg_mode)
int tessedit_pageseg_mode