28 #pragma warning(disable:4244) // Conversion warnings 36 #include "allheaders.h" 62 static Pix* RemoveEnclosingCircle(Pix* pixs) {
63 Pix* pixsi = pixInvert(NULL, pixs);
64 Pix* pixc = pixCreateTemplate(pixs);
65 pixSetOrClearBorder(pixc, 1, 1, 1, 1, PIX_SET);
66 pixSeedfillBinary(pixc, pixc, pixsi, 4);
67 pixInvert(pixc, pixc);
69 Pix* pixt = pixAnd(NULL, pixs, pixc);
71 pixCountConnComp(pixt, 8, &max_count);
77 pixErodeBrick(pixc, pixc, 3, 3);
78 pixt = pixAnd(NULL, pixs, pixc);
80 pixCountConnComp(pixt, 8, &
count);
81 if (i == 1 ||
count > max_count) {
84 }
else if (i > 1 &&
count < min_count) {
87 pixout = pixCopy(NULL, pixt);
88 }
else if (
count >= min_count) {
105 int width = pixGetWidth(pix_binary_);
106 int height = pixGetHeight(pix_binary_);
112 input_file != NULL && input_file->
length() > 0) {
113 STRING name = *input_file;
114 const char* lastdot = strrchr(name.
string(),
'.');
116 name[lastdot - name.
string()] =
'\0';
119 if (blocks->empty()) {
122 BLOCK_IT block_it(blocks);
125 block_it.add_to_end(block);
136 BLOBNBOX_LIST diacritic_blobs;
137 int auto_page_seg_ret_val = 0;
138 TO_BLOCK_LIST to_blocks;
142 pageseg_mode, blocks, &to_blocks,
145 return auto_page_seg_ret_val;
149 deskew_ =
FCOORD(1.0f, 0.0f);
150 reskew_ =
FCOORD(1.0f, 0.0f);
152 Pix* pixcleaned = RemoveEnclosingCircle(pix_binary_);
153 if (pixcleaned != NULL) {
154 pixDestroy(&pix_binary_);
155 pix_binary_ = pixcleaned;
160 if (auto_page_seg_ret_val < 0) {
164 if (blocks->empty()) {
173 textord_.
TextordPage(pageseg_mode, reskew_, width, height, pix_binary_,
174 pix_thresholds_, pix_grey_, splitting || cjk_mode,
175 &diacritic_blobs, blocks, &to_blocks);
176 return auto_page_seg_ret_val;
184 static void WriteDebugBackgroundImage(
bool printable, Pix* pix_binary) {
185 Pix* grey_pix = pixCreate(pixGetWidth(pix_binary),
186 pixGetHeight(pix_binary), 8);
191 pixSetMasked(grey_pix, pix_binary, 192);
193 pixSetAllArbitrary(grey_pix, 64);
194 pixSetMasked(grey_pix, pix_binary, 0);
198 pixDestroy(&grey_pix);
226 TO_BLOCK_LIST* to_blocks,
227 BLOBNBOX_LIST* diacritic_blobs,
Tesseract* osd_tess,
232 Pix* photomask_pix = NULL;
233 Pix* musicmask_pix = NULL;
235 BLOCK_LIST found_blocks;
236 TO_BLOCK_LIST temp_blocks;
239 pageseg_mode, blocks, osd_tess, osr, &temp_blocks, &photomask_pix,
242 if (finder != NULL) {
243 TO_BLOCK_IT to_block_it(&temp_blocks);
244 TO_BLOCK* to_block = to_block_it.data();
245 if (musicmask_pix != NULL) {
248 pixOr(photomask_pix, photomask_pix, musicmask_pix);
254 pageseg_mode, scaled_color_, scaled_factor_, to_block, photomask_pix,
255 pix_thresholds_, pix_grey_, &found_blocks, diacritic_blobs, to_blocks);
260 pixDestroy(&photomask_pix);
261 pixDestroy(&musicmask_pix);
262 if (result < 0)
return result;
265 BLOCK_IT block_it(blocks);
267 block_it.add_list_after(&found_blocks);
278 static void AddAllScriptsConverted(
const UNICHARSET& sid_set,
304 OSResults* osr, TO_BLOCK_LIST* to_blocks, Pix** photo_mask_pix,
305 Pix** music_mask_pix) {
308 TabVector_LIST v_lines;
309 TabVector_LIST h_lines;
314 pixWrite(
"tessinput.png", pix_binary_, IFF_PNG);
319 &vertical_x, &vertical_y, music_mask_pix,
322 pixWrite(
"tessnolines.png", pix_binary_, IFF_PNG);
326 pixWrite(
"tessnoimages.png", pix_binary_, IFF_PNG);
332 TO_BLOCK_IT to_block_it(to_blocks);
336 TO_BLOCK* to_block = to_block_it.data();
337 TBOX blkbox = to_block->block->bounding_box();
340 if (to_block->line_size >= 2) {
341 finder =
new ColumnFinder(static_cast<int>(to_block->line_size),
345 &v_lines, &h_lines, vertical_x, vertical_y);
353 BLOBNBOX_CLIST osd_blobs;
358 int osd_orientation = 0;
365 to_block, &osd_blobs);
367 if (
PSM_OSD_ENABLED(pageseg_mode) && osd_tess != NULL && osr != NULL) {
369 if (osd_tess !=
this) {
373 for (
int s = 0; s < sub_langs_.size(); ++s) {
374 AddAllScriptsConverted(sub_langs_[s]->
unicharset,
386 for (
int i = 0; i < 4; ++i) {
387 if (i != osd_orientation &&
393 const char* best_script_str =
398 strcmp(
"Japanese", best_script_str) == 0 ||
399 strcmp(
"Korean", best_script_str) == 0 ||
400 strcmp(
"Hangul", best_script_str) == 0;
406 if (!cjk && !vertical_text && osd_orientation == 2) {
408 tprintf(
"OSD: Weak margin (%.2f), horiz textlines, not CJK: " 409 "Don't rotate.\n", osd_margin);
413 "OSD: Weak margin (%.2f) for %d blob text block, " 414 "but using orientation anyway: %d\n",
415 osd_margin, osd_blobs.length(), osd_orientation);
419 osd_blobs.shallow_clear();
void set_cjk_script(bool is_cjk)
int SegmentPage(const STRING *input_file, BLOCK_LIST *blocks, Tesseract *osd_tess, OSResults *osr)
bool PSM_COL_FIND_ENABLED(int pageseg_mode)
bool PSM_OSD_ENABLED(int pageseg_mode)
void CorrectOrientation(TO_BLOCK *block, bool vertical_text_lines, int recognition_rotation)
int AutoPageSeg(PageSegMode pageseg_mode, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks, BLOBNBOX_LIST *diacritic_blobs, Tesseract *osd_tess, OSResults *osr)
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 right_to_left() const
void SetEquationDetect(EquationDetectBase *detect)
void SetupAndFilterNoise(PageSegMode pageseg_mode, Pix *photo_mask_pix, TO_BLOCK *input_block)
int get_script_id_from_name(const char *script_name) const
int tessedit_pageseg_mode
int os_detect_blobs(const GenericVector< int > *allowed_scripts, BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
const ICOORD & topright() const
const int kMaxCircleErosions
bool textord_debug_images
bool tessedit_dump_pageseg_images
static Pix * FindImages(Pix *pix)
int get_script_table_size() const
Orientation and script detection only.
const char * get_script_from_script_id(int id) const
double textord_tabfind_aligned_gap_fraction
bool PSM_SPARSE(int pageseg_mode)
bool IsVerticallyAlignedText(double find_vertical_text_ratio, TO_BLOCK *block, BLOBNBOX_CLIST *osd_blobs)
bool PSM_BLOCK_FIND_ENABLED(int pageseg_mode)
void find_components(Pix *pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
static void IncrementDebugPix()
bool enable_noise_removal
double min_orientation_margin
void set_right_to_left(bool value)
bool read_unlv_file(STRING name, inT32 xsize, inT32 ysize, BLOCK_LIST *blocks)
bool PSM_ORIENTATION_ENABLED(int pageseg_mode)
void GetDeskewVectors(FCOORD *deskew, FCOORD *reskew)
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)
static const STRING & textord_debug_pix()
const char * string() const
int LabelSpecialText(TO_BLOCK *to_block)
bool textord_debug_printable
int textord_debug_tabfind
double textord_tabfind_vertical_text_ratio
Assume a single uniform block of text. (Default.)
int pageseg_devanagari_split_strategy
bool textord_tabfind_vertical_text
bool textord_tabfind_force_vertical_text
Treat the image as a single word in a circle.
bool textord_tabfind_show_vlines
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)
bool textord_use_cjk_fp_model
const ICOORD & botleft() const
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)