Skip to content

Commit

Permalink
Add configurable variables to control thresholding (#3577)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitdo authored Sep 29, 2021
1 parent c4ad9b7 commit 0cb9c40
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/api/baseapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2072,7 +2072,7 @@ bool TessBaseAPI::Threshold(Pix **pix) {
tesseract_->set_pix_grey(nullptr);
}
} else {
auto [ok, pix_grey, pix_binary, pix_thresholds] = thresholder_->Threshold(thresholding_method);
auto [ok, pix_grey, pix_binary, pix_thresholds] = thresholder_->Threshold(this, thresholding_method);

if (!ok) {
return false;
Expand Down
30 changes: 28 additions & 2 deletions src/ccmain/tesseractclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,34 @@ Tesseract::Tesseract()
this->params())
, INT_MEMBER(thresholding_method,
static_cast<int>(tesseract::ThresholdMethod::Otsu),
"Thresholding "
"method: 0 = Otsu, 1 = Adaptive Otsu, 2 = Sauvola",
"Thresholding method: 0 = Otsu, 1 = Adaptive Otsu, 2 = "
"Sauvola",
this->params())
, INT_MEMBER(thresholding_window_size, 51,
"Window size for measuring local statistics. "
"This parameter is used by the Sauvola thresolding method",
this->params())
, double_MEMBER(thresholding_kfactor, 0.34,
"Factor for reducing threshold due to variance. "
"This parameter is used by the Sauvola thresolding method. "
"Must be >= 0",
this->params())
, INT_MEMBER(thresholding_tile_size, 300,
"Desired tile size. Actual size may vary. Must be >= 16. "
"This parameter is used by the Adaptive Otsu thresolding "
"method",
this->params())
, INT_MEMBER(thresholding_smooth_size, 0,
"Size of convolution kernel applied to threshold array. "
"This parameter is used by the Adaptive Otsu thresolding "
"method. "
"Use 0 for no smoothing",
this->params())
, double_MEMBER(thresholding_score_fraction, 0.1,
"Fraction of the max Otsu score. "
"This parameter is used by the Adaptive Otsu thresolding "
"method. "
"Typically 0.1. Use 0.0 for standard Otsu",
this->params())
, INT_INIT_MEMBER(tessedit_ocr_engine_mode, tesseract::OEM_DEFAULT,
"Which OCR engine(s) to run (Tesseract, LSTM, both)."
Expand Down
5 changes: 5 additions & 0 deletions src/ccmain/tesseractclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,11 @@ class TESS_API Tesseract : public Wordrec {
BOOL_VAR_H(tessedit_do_invert);
INT_VAR_H(tessedit_pageseg_mode);
INT_VAR_H(thresholding_method);
INT_VAR_H(thresholding_window_size);
double_VAR_H(thresholding_kfactor);
INT_VAR_H(thresholding_tile_size);
INT_VAR_H(thresholding_smooth_size);
double_VAR_H(thresholding_score_fraction);
INT_VAR_H(tessedit_ocr_engine_mode);
STRING_VAR_H(tessedit_char_blacklist);
STRING_VAR_H(tessedit_char_whitelist);
Expand Down
38 changes: 27 additions & 11 deletions src/ccmain/thresholder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#endif

#include <allheaders.h>
#include <tesseract/baseapi.h> // for api->GetIntVariable()

#include <cstdint> // for uint32_t
#include <cstring>
Expand Down Expand Up @@ -186,7 +187,8 @@ void ImageThresholder::SetImage(const Image pix) {
}

std::tuple<bool, Image, Image, Image> ImageThresholder::Threshold(
ThresholdMethod method) {
TessBaseAPI *api,
ThresholdMethod method) {
Image pix_binary = nullptr;
Image pix_thresholds = nullptr;

Expand All @@ -203,9 +205,10 @@ std::tuple<bool, Image, Image, Image> ImageThresholder::Threshold(

int r;
if (method == ThresholdMethod::Sauvola) {
// TODO: Convert this constant to config var
// window half-width for measuring local statistics
constexpr l_int32 whsize = 25;
bool b;
int window_size;
b = api->GetIntVariable("thresholding_window_size", &window_size);
int half_window_size = window_size / 2;
// factor for image division into tiles; >= 1
l_int32 nx, ny;
// // tiles size will be approx. 250 x 250 pixels
Expand All @@ -215,19 +218,32 @@ std::tuple<bool, Image, Image, Image> ImageThresholder::Threshold(
ny = std::max(1, (pix_h + 125) / 250);
auto xrat = pix_w / nx;
auto yrat = pix_h / ny;
if (xrat < whsize + 2) {
nx = pix_w / (whsize + 2);
if (xrat < half_window_size + 2) {
nx = pix_w / (half_window_size + 2);
}
if (yrat < whsize + 2) {
ny = pix_h / (whsize + 2);
if (yrat < half_window_size + 2) {
ny = pix_h / (half_window_size + 2);
}

r = pixSauvolaBinarizeTiled(pix_grey, whsize, 0.40, nx, ny,
double kfactor;
b = api->GetDoubleVariable("thresholding_kfactor", &kfactor);
r = pixSauvolaBinarizeTiled(pix_grey, half_window_size, kfactor, nx, ny,
(PIX**)pix_thresholds,
(PIX**)pix_binary);
} else { // if (method == ThresholdMethod::AdaptiveOtsu)
r = pixOtsuAdaptiveThreshold(pix_grey, 300, 300, 0, 0, 0.1,
(PIX**)pix_thresholds, (PIX**)pix_binary);
bool b;
int tile_size;
b = api->GetIntVariable("thresholding_tile_size", &tile_size);
int smooth_size;
b = api->GetIntVariable("thresholding_smooth_size", &smooth_size);
int half_smooth_size = smooth_size / 2;
double score_fraction;
b = api->GetDoubleVariable("thresholding_score_fraction", &score_fraction);
r = pixOtsuAdaptiveThreshold(pix_grey, tile_size, tile_size,
half_smooth_size, half_smooth_size,
score_fraction,
(PIX**)pix_thresholds,
(PIX**)pix_binary);
}

bool ok = (r == 0);
Expand Down
4 changes: 3 additions & 1 deletion src/ccmain/thresholder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct Pix;

namespace tesseract {

class TessBaseAPI;

/// Base class for all tesseract image thresholding classes.
/// Specific classes can add new thresholding methods by
/// overriding ThresholdToPix.
Expand Down Expand Up @@ -121,7 +123,7 @@ class TESS_API ImageThresholder {
/// Returns false on error.
virtual bool ThresholdToPix(Image *pix);

virtual std::tuple<bool, Image, Image, Image> Threshold(
virtual std::tuple<bool, Image, Image, Image> Threshold(TessBaseAPI *api,
ThresholdMethod method);

// Gets a pix that contains an 8 bit threshold value at each pixel. The
Expand Down

0 comments on commit 0cb9c40

Please sign in to comment.