31 #pragma warning(disable:4244) // Conversion warnings
36 #include "config_auto.h"
39 #include "allheaders.h"
69 static Pix* RemoveEnclosingCircle(Pix* pixs) {
70 Pix* pixsi = pixInvert(
NULL, pixs);
71 Pix* pixc = pixCreateTemplate(pixs);
72 pixSetOrClearBorder(pixc, 1, 1, 1, 1, PIX_SET);
73 pixSeedfillBinary(pixc, pixc, pixsi, 4);
74 pixInvert(pixc, pixc);
76 Pix* pixt = pixAnd(
NULL, pixs, pixc);
78 pixCountConnComp(pixt, 8, &max_count);
84 pixErodeBrick(pixc, pixc, 3, 3);
85 pixt = pixAnd(
NULL, pixs, pixc);
87 pixCountConnComp(pixt, 8, &count);
88 if (i == 1 || count > max_count) {
91 }
else if (i > 1 && count < min_count) {
94 pixout = pixCopy(
NULL, pixt);
95 }
else if (count >= min_count) {
112 int width = pixGetWidth(pix_binary_);
113 int height = pixGetHeight(pix_binary_);
119 input_file !=
NULL && input_file->
length() > 0) {
121 const char* lastdot = strrchr(name.
string(),
'.');
123 name[lastdot - name.
string()] =
'\0';
126 if (blocks->empty()) {
129 BLOCK_IT block_it(blocks);
132 block_it.add_to_end(block);
143 BLOBNBOX_LIST diacritic_blobs;
144 int auto_page_seg_ret_val = 0;
145 TO_BLOCK_LIST to_blocks;
149 pageseg_mode, blocks, &to_blocks,
152 return auto_page_seg_ret_val;
156 deskew_ =
FCOORD(1.0f, 0.0f);
157 reskew_ =
FCOORD(1.0f, 0.0f);
159 Pix* pixcleaned = RemoveEnclosingCircle(pix_binary_);
160 if (pixcleaned !=
NULL) {
161 pixDestroy(&pix_binary_);
162 pix_binary_ = pixcleaned;
167 if (auto_page_seg_ret_val < 0) {
171 if (blocks->empty()) {
180 textord_.
TextordPage(pageseg_mode, reskew_, width, height, pix_binary_,
181 pix_thresholds_, pix_grey_, splitting || cjk_mode,
182 &diacritic_blobs, blocks, &to_blocks);
183 return auto_page_seg_ret_val;
191 static void WriteDebugBackgroundImage(
bool printable, Pix* pix_binary) {
192 Pix* grey_pix = pixCreate(pixGetWidth(pix_binary),
193 pixGetHeight(pix_binary), 8);
198 pixSetMasked(grey_pix, pix_binary, 192);
200 pixSetAllArbitrary(grey_pix, 64);
201 pixSetMasked(grey_pix, pix_binary, 0);
205 pixDestroy(&grey_pix);
233 TO_BLOCK_LIST* to_blocks,
234 BLOBNBOX_LIST* diacritic_blobs,
Tesseract* osd_tess,
239 Pix* photomask_pix =
NULL;
240 Pix* musicmask_pix =
NULL;
242 BLOCK_LIST found_blocks;
243 TO_BLOCK_LIST temp_blocks;
246 pageseg_mode, blocks, osd_tess, osr, &temp_blocks, &photomask_pix,
249 if (finder !=
NULL) {
250 TO_BLOCK_IT to_block_it(&temp_blocks);
251 TO_BLOCK* to_block = to_block_it.data();
252 if (musicmask_pix !=
NULL) {
255 pixOr(photomask_pix, photomask_pix, musicmask_pix);
261 pageseg_mode, scaled_color_, scaled_factor_, to_block, photomask_pix,
262 pix_thresholds_, pix_grey_, &found_blocks, diacritic_blobs, to_blocks);
267 pixDestroy(&photomask_pix);
268 pixDestroy(&musicmask_pix);
269 if (result < 0)
return result;
272 BLOCK_IT block_it(blocks);
274 block_it.add_list_after(&found_blocks);
285 static void AddAllScriptsConverted(
const UNICHARSET& sid_set,
311 OSResults* osr, TO_BLOCK_LIST* to_blocks, Pix** photo_mask_pix,
312 Pix** music_mask_pix) {
315 TabVector_LIST v_lines;
316 TabVector_LIST h_lines;
321 pixWrite(
"tessinput.png", pix_binary_, IFF_PNG);
326 &vertical_x, &vertical_y, music_mask_pix,
329 pixWrite(
"tessnolines.png", pix_binary_, IFF_PNG);
333 pixWrite(
"tessnoimages.png", pix_binary_, IFF_PNG);
339 TO_BLOCK_IT to_block_it(to_blocks);
343 TO_BLOCK* to_block = to_block_it.data();
344 TBOX blkbox = to_block->block->bounding_box();
347 if (to_block->line_size >= 2) {
348 finder =
new ColumnFinder(static_cast<int>(to_block->line_size),
352 &v_lines, &h_lines, vertical_x, vertical_y);
360 BLOBNBOX_CLIST osd_blobs;
365 int osd_orientation = 0;
372 to_block, &osd_blobs);
376 if (osd_tess !=
this) {
380 for (
int s = 0; s < sub_langs_.size(); ++s) {
381 AddAllScriptsConverted(sub_langs_[s]->
unicharset,
393 for (
int i = 0; i < 4; ++i) {
394 if (i != osd_orientation &&
400 const char* best_script_str =
405 strcmp(
"Japanese", best_script_str) == 0 ||
406 strcmp(
"Korean", best_script_str) == 0 ||
407 strcmp(
"Hangul", best_script_str) == 0;
413 if (!cjk && !vertical_text && osd_orientation == 2) {
415 tprintf(
"OSD: Weak margin (%.2f), horiz textlines, not CJK: "
416 "Don't rotate.\n", osd_margin);
419 tprintf(
"OSD: Weak margin (%.2f) for %d blob text block, "
420 "but using orientation anyway: %d\n",
421 osd_blobs.length(), osd_margin, osd_orientation);
425 osd_blobs.shallow_clear();
bool tessedit_dump_pageseg_images
Treat the image as a single word in a circle.
bool right_to_left() const
const ICOORD & botleft() const
static Pix * FindImages(Pix *pix)
bool PSM_SPARSE(int pageseg_mode)
bool textord_debug_images
static void IncrementDebugPix()
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)
static const STRING & textord_debug_pix()
int get_script_table_size() const
int SegmentPage(const STRING *input_file, BLOCK_LIST *blocks, Tesseract *osd_tess, OSResults *osr)
int tessedit_pageseg_mode
void SetupAndFilterNoise(PageSegMode pageseg_mode, Pix *photo_mask_pix, TO_BLOCK *input_block)
int FindBlocks(PageSegMode pageseg_mode, Pix *scaled_color, int scaled_factor, TO_BLOCK *block, Pix *photo_mask_pix, Pix *thresholds_pix, Pix *grey_pix, BLOCK_LIST *blocks, BLOBNBOX_LIST *diacritic_blobs, TO_BLOCK_LIST *to_blocks)
const int kDefaultResolution
Default resolution used if input in not believable.
int get_script_id_from_name(const char *script_name) const
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)
double textord_tabfind_vertical_text_ratio
bool IsVerticallyAlignedText(double find_vertical_text_ratio, TO_BLOCK *block, BLOBNBOX_CLIST *osd_blobs)
int textord_debug_tabfind
int pageseg_devanagari_split_strategy
const int kMinCredibleResolution
Minimum believable resolution.
void SetEquationDetect(EquationDetectBase *detect)
bool PSM_ORIENTATION_ENABLED(int pageseg_mode)
Orientation and script detection only.
bool textord_debug_printable
void set_cjk_script(bool is_cjk)
void CorrectOrientation(TO_BLOCK *block, bool vertical_text_lines, int recognition_rotation)
int LabelSpecialText(TO_BLOCK *to_block)
Assume a single uniform block of text. (Default.)
bool enable_noise_removal
const char * get_script_from_script_id(int id) const
int AutoPageSeg(PageSegMode pageseg_mode, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks, BLOBNBOX_LIST *diacritic_blobs, Tesseract *osd_tess, OSResults *osr)
const int kMaxCircleErosions
double min_orientation_margin
bool textord_tabfind_show_vlines
int os_detect_blobs(const GenericVector< int > *allowed_scripts, BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
void find_components(Pix *pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
bool PSM_COL_FIND_ENABLED(int pageseg_mode)
bool textord_tabfind_vertical_text
bool PSM_BLOCK_FIND_ENABLED(int pageseg_mode)
const ICOORD & topright() const
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 PSM_OSD_ENABLED(int pageseg_mode)
const char * string() const
bool read_unlv_file(STRING name, inT32 xsize, inT32 ysize, BLOCK_LIST *blocks)
bool textord_tabfind_force_vertical_text
bool textord_use_cjk_fp_model
double textord_tabfind_aligned_gap_fraction
void set_right_to_left(bool value)
void GetDeskewVectors(FCOORD *deskew, FCOORD *reskew)