Skip to content

Commit c23792b

Browse files
authored
Allow for text angle/gradient to be retrieved (#4070)
* Added GetGradient function
1 parent 6a31e36 commit c23792b

File tree

9 files changed

+31
-8
lines changed

9 files changed

+31
-8
lines changed

include/tesseract/baseapi.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ class TESS_API TessBaseAPI {
337337
*/
338338
Pix *GetThresholdedImage();
339339

340+
/**
341+
* Return average gradient of lines on page.
342+
*/
343+
float GetGradient();
344+
340345
/**
341346
* Get the result of page layout analysis as a leptonica-style
342347
* Boxa, Pixa pair, in reading order.

include/tesseract/capi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ TESS_API void TessBaseAPISetRectangle(TessBaseAPI *handle, int left, int top,
273273
int width, int height);
274274

275275
TESS_API struct Pix *TessBaseAPIGetThresholdedImage(TessBaseAPI *handle);
276+
TESS_API float TessBaseAPIGetGradient(TessBaseAPI *handle);
276277
TESS_API struct Boxa *TessBaseAPIGetRegions(TessBaseAPI *handle,
277278
struct Pixa **pixa);
278279
TESS_API struct Boxa *TessBaseAPIGetTextlines(TessBaseAPI *handle,

src/api/baseapi.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,13 @@ int TessBaseAPI::FindLines() {
21892189
return 0;
21902190
}
21912191

2192+
/**
2193+
* Return average gradient of lines on page.
2194+
*/
2195+
float TessBaseAPI::GetGradient() {
2196+
return tesseract_->gradient();
2197+
}
2198+
21922199
/** Delete the pageres and clear the block list ready for a new page. */
21932200
void TessBaseAPI::ClearResults() {
21942201
if (tesseract_ != nullptr) {

src/api/capi.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ struct Pix *TessBaseAPIGetThresholdedImage(TessBaseAPI *handle) {
327327
return handle->GetThresholdedImage();
328328
}
329329

330+
float TessBaseAPIGetGradient(TessBaseAPI *handle) {
331+
return handle->GetGradient();
332+
}
333+
330334
void TessBaseAPIClearPersistentCache(TessBaseAPI * /*handle*/) {
331335
TessBaseAPI::ClearPersistentCache();
332336
}

src/ccmain/pagesegmain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ int Tesseract::SegmentPage(const char *input_file, BLOCK_LIST *blocks, Tesseract
168168
bool cjk_mode = textord_use_cjk_fp_model;
169169

170170
textord_.TextordPage(pageseg_mode, reskew_, width, height, pix_binary_, pix_thresholds_,
171-
pix_grey_, splitting || cjk_mode, &diacritic_blobs, blocks, &to_blocks);
171+
pix_grey_, splitting || cjk_mode, &diacritic_blobs, blocks, &to_blocks, &gradient_);
172172
return auto_page_seg_ret_val;
173173
}
174174

src/ccmain/tesseractclass.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ Tesseract::Tesseract()
461461
, scaled_factor_(-1)
462462
, deskew_(1.0f, 0.0f)
463463
, reskew_(1.0f, 0.0f)
464+
, gradient_(0.0f)
464465
, most_recently_used_(this)
465466
, font_table_size_(0)
466467
#ifndef DISABLED_LEGACY_ENGINE
@@ -498,6 +499,7 @@ void Tesseract::Clear() {
498499
scaled_color_.destroy();
499500
deskew_ = FCOORD(1.0f, 0.0f);
500501
reskew_ = FCOORD(1.0f, 0.0f);
502+
gradient_ = 0.0f;
501503
splitter_.Clear();
502504
scaled_factor_ = -1;
503505
for (auto &sub_lang : sub_langs_) {

src/ccmain/tesseractclass.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ class TESS_API Tesseract : public Wordrec {
200200
const FCOORD &reskew() const {
201201
return reskew_;
202202
}
203+
float gradient() const {
204+
return gradient_;
205+
}
203206
// Destroy any existing pix and return a pointer to the pointer.
204207
Image *mutable_pix_binary() {
205208
pix_binary_.destroy();
@@ -1004,6 +1007,7 @@ class TESS_API Tesseract : public Wordrec {
10041007
int scaled_factor_;
10051008
FCOORD deskew_;
10061009
FCOORD reskew_;
1010+
float gradient_;
10071011
TesseractStats stats_;
10081012
// Sub-languages to be tried in addition to this.
10091013
std::vector<Tesseract *> sub_langs_;

src/textord/textord.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Textord::Textord(CCStruct *ccstruct)
177177
void Textord::TextordPage(PageSegMode pageseg_mode, const FCOORD &reskew, int width, int height,
178178
Image binary_pix, Image thresholds_pix, Image grey_pix, bool use_box_bottoms,
179179
BLOBNBOX_LIST *diacritic_blobs, BLOCK_LIST *blocks,
180-
TO_BLOCK_LIST *to_blocks) {
180+
TO_BLOCK_LIST *to_blocks, float *gradient) {
181181
page_tr_.set_x(width);
182182
page_tr_.set_y(height);
183183
if (to_blocks->empty()) {
@@ -219,15 +219,14 @@ void Textord::TextordPage(PageSegMode pageseg_mode, const FCOORD &reskew, int wi
219219
TO_BLOCK_IT to_block_it(to_blocks);
220220
TO_BLOCK *to_block = to_block_it.data();
221221
// Make the rows in the block.
222-
float gradient;
223222
// Do it the old fashioned way.
224223
if (PSM_LINE_FIND_ENABLED(pageseg_mode)) {
225-
gradient = make_rows(page_tr_, to_blocks);
224+
*gradient = make_rows(page_tr_, to_blocks);
226225
} else if (!PSM_SPARSE(pageseg_mode)) {
227226
// RAW_LINE, SINGLE_LINE, SINGLE_WORD and SINGLE_CHAR all need a single row.
228-
gradient = make_single_row(page_tr_, pageseg_mode != PSM_RAW_LINE, to_block, to_blocks);
227+
*gradient = make_single_row(page_tr_, pageseg_mode != PSM_RAW_LINE, to_block, to_blocks);
229228
} else {
230-
gradient = 0.0f;
229+
*gradient = 0.0f;
231230
}
232231
BaselineDetect baseline_detector(textord_baseline_debug, reskew, to_blocks);
233232
baseline_detector.ComputeStraightBaselines(use_box_bottoms);
@@ -236,7 +235,7 @@ void Textord::TextordPage(PageSegMode pageseg_mode, const FCOORD &reskew, int wi
236235
// Now make the words in the lines.
237236
if (PSM_WORD_FIND_ENABLED(pageseg_mode)) {
238237
// SINGLE_LINE uses the old word maker on the single line.
239-
make_words(this, page_tr_, gradient, blocks, to_blocks);
238+
make_words(this, page_tr_, *gradient, blocks, to_blocks);
240239
} else {
241240
// SINGLE_WORD and SINGLE_CHAR cram all the blobs into a
242241
// single word, and in SINGLE_CHAR mode, all the outlines

src/textord/textord.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ class Textord {
8989
// to the appropriate word(s) in case they are really diacritics.
9090
void TextordPage(PageSegMode pageseg_mode, const FCOORD &reskew, int width, int height,
9191
Image binary_pix, Image thresholds_pix, Image grey_pix, bool use_box_bottoms,
92-
BLOBNBOX_LIST *diacritic_blobs, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks);
92+
BLOBNBOX_LIST *diacritic_blobs, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks,
93+
float *gradient);
9394

9495
// If we were supposed to return only a single textline, and there is more
9596
// than one, clean up and leave only the best.

0 commit comments

Comments
 (0)