Add configurable variables to control thresholding (#3577)

This commit is contained in:
Amit D 2021-09-29 23:17:22 +03:00 committed by GitHub
parent c4ad9b7bbf
commit 0cb9c40528
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 15 deletions

View File

@ -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;

View File

@ -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)."

View File

@ -757,6 +757,11 @@ public:
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);

View File

@ -25,6 +25,7 @@
#endif
#include <allheaders.h>
#include <tesseract/baseapi.h> // for api->GetIntVariable()
#include <cstdint> // for uint32_t
#include <cstring>
@ -186,6 +187,7 @@ void ImageThresholder::SetImage(const Image pix) {
}
std::tuple<bool, Image, Image, Image> ImageThresholder::Threshold(
TessBaseAPI *api,
ThresholdMethod method) {
Image pix_binary = nullptr;
Image pix_thresholds = nullptr;
@ -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
@ -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);

View File

@ -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.
@ -121,7 +123,7 @@ public:
/// 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