From a82b82f0c92ef35cf10a29d3f37fc6ceb7df017c Mon Sep 17 00:00:00 2001 From: Balearica Date: Mon, 8 May 2023 17:43:52 -0700 Subject: [PATCH 1/6] Added GetGradient function --- include/tesseract/baseapi.h | 17 +++++++++++------ src/api/baseapi.cpp | 7 +++++++ src/ccmain/pagesegmain.cpp | 2 +- src/ccmain/tesseractclass.h | 4 ++++ src/textord/textord.cpp | 11 +++++------ src/textord/textord.h | 3 ++- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/include/tesseract/baseapi.h b/include/tesseract/baseapi.h index dd9fe4a299..7a209ca626 100644 --- a/include/tesseract/baseapi.h +++ b/include/tesseract/baseapi.h @@ -346,6 +346,11 @@ class TESS_API TessBaseAPI { */ Pix *GetThresholdedImage(); + /** + * Return average gradient of lines on page. + */ + float GetGradient(); + /** * Get the result of page layout analysis as a leptonica-style * Boxa, Pixa pair, in reading order. @@ -719,6 +724,12 @@ class TESS_API TessBaseAPI { void set_min_orientation_margin(double margin); /* @} */ + /** + * Find lines from the image making the BLOCK_LIST. + * @return 0 on success. + */ + int FindLines(); + protected: /** Common code for setting the image. Returns true if Init has been called. */ @@ -730,12 +741,6 @@ class TESS_API TessBaseAPI { */ virtual bool Threshold(Pix **pix); - /** - * Find lines from the image making the BLOCK_LIST. - * @return 0 on success. - */ - int FindLines(); - /** Delete the pageres and block list ready for a new page. */ void ClearResults(); diff --git a/src/api/baseapi.cpp b/src/api/baseapi.cpp index f78894ce74..faf2345b14 100644 --- a/src/api/baseapi.cpp +++ b/src/api/baseapi.cpp @@ -2201,6 +2201,13 @@ int TessBaseAPI::FindLines() { return 0; } +/** + * Return average gradient of lines on page. + */ +float TessBaseAPI::GetGradient() { + return tesseract_->gradient(); +} + /** Delete the pageres and clear the block list ready for a new page. */ void TessBaseAPI::ClearResults() { if (tesseract_ != nullptr) { diff --git a/src/ccmain/pagesegmain.cpp b/src/ccmain/pagesegmain.cpp index ea7af538c8..e0685bbeaa 100644 --- a/src/ccmain/pagesegmain.cpp +++ b/src/ccmain/pagesegmain.cpp @@ -168,7 +168,7 @@ int Tesseract::SegmentPage(const char *input_file, BLOCK_LIST *blocks, Tesseract bool cjk_mode = textord_use_cjk_fp_model; textord_.TextordPage(pageseg_mode, reskew_, width, height, pix_binary_, pix_thresholds_, - pix_grey_, splitting || cjk_mode, &diacritic_blobs, blocks, &to_blocks); + pix_grey_, splitting || cjk_mode, &diacritic_blobs, blocks, &to_blocks, &gradient_); return auto_page_seg_ret_val; } diff --git a/src/ccmain/tesseractclass.h b/src/ccmain/tesseractclass.h index 732bb9e62e..3cda7bf8d2 100644 --- a/src/ccmain/tesseractclass.h +++ b/src/ccmain/tesseractclass.h @@ -200,6 +200,9 @@ class TESS_API Tesseract : public Wordrec { const FCOORD &reskew() const { return reskew_; } + float gradient() const { + return gradient_; + } // Destroy any existing pix and return a pointer to the pointer. Image *mutable_pix_binary() { pix_binary_.destroy(); @@ -1001,6 +1004,7 @@ class TESS_API Tesseract : public Wordrec { int scaled_factor_; FCOORD deskew_; FCOORD reskew_; + float gradient_; TesseractStats stats_; // Sub-languages to be tried in addition to this. std::vector sub_langs_; diff --git a/src/textord/textord.cpp b/src/textord/textord.cpp index 3abbb7a816..9526fe1909 100644 --- a/src/textord/textord.cpp +++ b/src/textord/textord.cpp @@ -177,7 +177,7 @@ Textord::Textord(CCStruct *ccstruct) void Textord::TextordPage(PageSegMode pageseg_mode, const FCOORD &reskew, int width, int height, Image binary_pix, Image thresholds_pix, Image grey_pix, bool use_box_bottoms, BLOBNBOX_LIST *diacritic_blobs, BLOCK_LIST *blocks, - TO_BLOCK_LIST *to_blocks) { + TO_BLOCK_LIST *to_blocks, float *gradient) { page_tr_.set_x(width); page_tr_.set_y(height); if (to_blocks->empty()) { @@ -219,15 +219,14 @@ void Textord::TextordPage(PageSegMode pageseg_mode, const FCOORD &reskew, int wi TO_BLOCK_IT to_block_it(to_blocks); TO_BLOCK *to_block = to_block_it.data(); // Make the rows in the block. - float gradient; // Do it the old fashioned way. if (PSM_LINE_FIND_ENABLED(pageseg_mode)) { - gradient = make_rows(page_tr_, to_blocks); + *gradient = make_rows(page_tr_, to_blocks); } else if (!PSM_SPARSE(pageseg_mode)) { // RAW_LINE, SINGLE_LINE, SINGLE_WORD and SINGLE_CHAR all need a single row. - gradient = make_single_row(page_tr_, pageseg_mode != PSM_RAW_LINE, to_block, to_blocks); + *gradient = make_single_row(page_tr_, pageseg_mode != PSM_RAW_LINE, to_block, to_blocks); } else { - gradient = 0.0f; + *gradient = 0.0f; } BaselineDetect baseline_detector(textord_baseline_debug, reskew, to_blocks); baseline_detector.ComputeStraightBaselines(use_box_bottoms); @@ -236,7 +235,7 @@ void Textord::TextordPage(PageSegMode pageseg_mode, const FCOORD &reskew, int wi // Now make the words in the lines. if (PSM_WORD_FIND_ENABLED(pageseg_mode)) { // SINGLE_LINE uses the old word maker on the single line. - make_words(this, page_tr_, gradient, blocks, to_blocks); + make_words(this, page_tr_, *gradient, blocks, to_blocks); } else { // SINGLE_WORD and SINGLE_CHAR cram all the blobs into a // single word, and in SINGLE_CHAR mode, all the outlines diff --git a/src/textord/textord.h b/src/textord/textord.h index df6750a748..308096b4f0 100644 --- a/src/textord/textord.h +++ b/src/textord/textord.h @@ -89,7 +89,8 @@ class Textord { // to the appropriate word(s) in case they are really diacritics. void TextordPage(PageSegMode pageseg_mode, const FCOORD &reskew, int width, int height, Image binary_pix, Image thresholds_pix, Image grey_pix, bool use_box_bottoms, - BLOBNBOX_LIST *diacritic_blobs, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks); + BLOBNBOX_LIST *diacritic_blobs, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks, + float *gradient); // If we were supposed to return only a single textline, and there is more // than one, clean up and leave only the best. From df967ab331d842428c0525c2e1ec3e7c8dd27bc9 Mon Sep 17 00:00:00 2001 From: Balearica Date: Mon, 8 May 2023 20:15:45 -0700 Subject: [PATCH 2/6] Updated gradient_ --- src/ccmain/tesseractclass.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ccmain/tesseractclass.cpp b/src/ccmain/tesseractclass.cpp index fd58ac8746..13ac27025d 100644 --- a/src/ccmain/tesseractclass.cpp +++ b/src/ccmain/tesseractclass.cpp @@ -495,6 +495,7 @@ void Tesseract::Clear() { scaled_color_.destroy(); deskew_ = FCOORD(1.0f, 0.0f); reskew_ = FCOORD(1.0f, 0.0f); + gradient_ = 0.0f; splitter_.Clear(); scaled_factor_ = -1; for (auto &sub_lang : sub_langs_) { From 2a93932af6186a49f22901dcdc0f4247fe501e79 Mon Sep 17 00:00:00 2001 From: Balearica Date: Wed, 10 May 2023 11:34:00 -0700 Subject: [PATCH 3/6] Changed FindLines back to protected function --- include/tesseract/baseapi.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/tesseract/baseapi.h b/include/tesseract/baseapi.h index 7a209ca626..bf5d9480f3 100644 --- a/include/tesseract/baseapi.h +++ b/include/tesseract/baseapi.h @@ -724,12 +724,6 @@ class TESS_API TessBaseAPI { void set_min_orientation_margin(double margin); /* @} */ - /** - * Find lines from the image making the BLOCK_LIST. - * @return 0 on success. - */ - int FindLines(); - protected: /** Common code for setting the image. Returns true if Init has been called. */ @@ -741,6 +735,12 @@ class TESS_API TessBaseAPI { */ virtual bool Threshold(Pix **pix); + /** + * Find lines from the image making the BLOCK_LIST. + * @return 0 on success. + */ + int FindLines(); + /** Delete the pageres and block list ready for a new page. */ void ClearResults(); From e0e1644097a1671a0d3e198e22f9d1bd00dabe6b Mon Sep 17 00:00:00 2001 From: Balearica Date: Wed, 10 May 2023 14:02:32 -0700 Subject: [PATCH 4/6] Minor update --- src/ccmain/tesseractclass.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ccmain/tesseractclass.h b/src/ccmain/tesseractclass.h index 3cda7bf8d2..948ca81906 100644 --- a/src/ccmain/tesseractclass.h +++ b/src/ccmain/tesseractclass.h @@ -1004,7 +1004,7 @@ class TESS_API Tesseract : public Wordrec { int scaled_factor_; FCOORD deskew_; FCOORD reskew_; - float gradient_; + float gradient_ = 0.0f; TesseractStats stats_; // Sub-languages to be tried in addition to this. std::vector sub_langs_; From 9452c5ab464faabbff4f3d833dece975b06f7dbc Mon Sep 17 00:00:00 2001 From: Balearica Date: Wed, 10 May 2023 14:10:34 -0700 Subject: [PATCH 5/6] Minor update --- src/ccmain/tesseractclass.cpp | 1 + src/ccmain/tesseractclass.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ccmain/tesseractclass.cpp b/src/ccmain/tesseractclass.cpp index 13ac27025d..cc16a711e0 100644 --- a/src/ccmain/tesseractclass.cpp +++ b/src/ccmain/tesseractclass.cpp @@ -458,6 +458,7 @@ Tesseract::Tesseract() , scaled_factor_(-1) , deskew_(1.0f, 0.0f) , reskew_(1.0f, 0.0f) + , gradient_(0.0f) , most_recently_used_(this) , font_table_size_(0) #ifndef DISABLED_LEGACY_ENGINE diff --git a/src/ccmain/tesseractclass.h b/src/ccmain/tesseractclass.h index 948ca81906..3cda7bf8d2 100644 --- a/src/ccmain/tesseractclass.h +++ b/src/ccmain/tesseractclass.h @@ -1004,7 +1004,7 @@ class TESS_API Tesseract : public Wordrec { int scaled_factor_; FCOORD deskew_; FCOORD reskew_; - float gradient_ = 0.0f; + float gradient_; TesseractStats stats_; // Sub-languages to be tried in addition to this. std::vector sub_langs_; From b454c3360dd92718c5ea21f68929fd849d503d2d Mon Sep 17 00:00:00 2001 From: Balearica Date: Wed, 10 May 2023 14:42:18 -0700 Subject: [PATCH 6/6] Added GetGradient functions to C API --- include/tesseract/capi.h | 1 + src/api/capi.cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/tesseract/capi.h b/include/tesseract/capi.h index 40f4856a11..85160d75f6 100644 --- a/include/tesseract/capi.h +++ b/include/tesseract/capi.h @@ -274,6 +274,7 @@ TESS_API void TessBaseAPISetRectangle(TessBaseAPI *handle, int left, int top, int width, int height); TESS_API struct Pix *TessBaseAPIGetThresholdedImage(TessBaseAPI *handle); +TESS_API float TessBaseAPIGetGradient(TessBaseAPI *handle); TESS_API struct Boxa *TessBaseAPIGetRegions(TessBaseAPI *handle, struct Pixa **pixa); TESS_API struct Boxa *TessBaseAPIGetTextlines(TessBaseAPI *handle, diff --git a/src/api/capi.cpp b/src/api/capi.cpp index 65c0e6e524..336c2c3a0d 100644 --- a/src/api/capi.cpp +++ b/src/api/capi.cpp @@ -327,6 +327,10 @@ struct Pix *TessBaseAPIGetThresholdedImage(TessBaseAPI *handle) { return handle->GetThresholdedImage(); } +float TessBaseAPIGetGradient(TessBaseAPI *handle) { + return handle->GetGradient(); +} + void TessBaseAPIClearPersistentCache(TessBaseAPI * /*handle*/) { TessBaseAPI::ClearPersistentCache(); }