Skip to content

Commit

Permalink
1.0.20: threshold: new method Grad: lower/upper bound
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Aug 12, 2024
1 parent 4e06c82 commit c9683d2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/app/DefaultParamsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,10 @@ std::unique_ptr<DefaultParams> DefaultParamsDialog::buildParams() const {
blackWhiteOptions.setBinarizationMethod(binarizationMethod);
blackWhiteOptions.setThresholdAdjustment(thresholdSlider->value());
blackWhiteOptions.setSauvolaCoef(sauvolaCoef->value());
if (binarizationMethod == T_SAUVOLA || binarizationMethod == T_BRADLEY || binarizationMethod == T_GRAD
|| binarizationMethod == T_EDGEPLUS || binarizationMethod == T_BLURDIV || binarizationMethod == T_EDGEDIV) {
if (binarizationMethod == T_SAUVOLA || binarizationMethod == T_BRADLEY || binarizationMethod == T_EDGEPLUS
|| binarizationMethod == T_BLURDIV || binarizationMethod == T_EDGEDIV) {
blackWhiteOptions.setWindowSize(sauvolaWindowSize->value());
} else if (binarizationMethod == T_WOLF) {
} else if (binarizationMethod == T_WOLF || binarizationMethod == T_GRAD) {
blackWhiteOptions.setWindowSize(wolfWindowSize->value());
}
blackWhiteOptions.setWolfCoef(wolfCoef->value());
Expand Down
2 changes: 1 addition & 1 deletion src/core/filters/output/OptionsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ OptionsWidget::OptionsWidget(std::shared_ptr<Settings> settings, const PageSelec
QPointer<BinarizationOptionsWidget> wolfBinarizationOptionsWidget = new WolfBinarizationOptionsWidget(m_settings);
QPointer<BinarizationOptionsWidget> bradleyBinarizationOptionsWidget
= new SauvolaBinarizationOptionsWidget(m_settings);
QPointer<BinarizationOptionsWidget> gradBinarizationOptionsWidget = new SauvolaBinarizationOptionsWidget(m_settings);
QPointer<BinarizationOptionsWidget> gradBinarizationOptionsWidget = new WolfBinarizationOptionsWidget(m_settings);
QPointer<BinarizationOptionsWidget> edgeplusBinarizationOptionsWidget
= new SauvolaBinarizationOptionsWidget(m_settings);
QPointer<BinarizationOptionsWidget> blurdivBinarizationOptionsWidget
Expand Down
6 changes: 4 additions & 2 deletions src/core/filters/output/OutputGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2330,9 +2330,11 @@ BinaryImage OutputGenerator::Processor::binarize(const QImage& image) const {
case T_GRAD: {
const double thresholdDelta = blackWhiteOptions.thresholdAdjustment();
const QSize windowsSize = QSize(blackWhiteOptions.getWindowSize(), blackWhiteOptions.getWindowSize());
const double thresholdCoef = blackWhiteOptions.getSauvolaCoef();
const auto lowerBound = (unsigned char) blackWhiteOptions.getWolfLowerBound();
const auto upperBound = (unsigned char) blackWhiteOptions.getWolfUpperBound();
const double thresholdCoef = blackWhiteOptions.getWolfCoef();

binarized = binarizeGrad(image, windowsSize, thresholdCoef, thresholdDelta);
binarized = binarizeGrad(image, windowsSize, lowerBound, upperBound, thresholdCoef, thresholdDelta);
break;
}
case T_EDGEPLUS: {
Expand Down
9 changes: 7 additions & 2 deletions src/imageproc/Binarize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,12 @@ BinaryImage binarizeBradley(const QImage& src, const QSize windowSize, const dou
return bwImg;
} // binarizeBradley

BinaryImage binarizeGrad(const QImage& src, const QSize windowSize, const double k, const double delta) {
BinaryImage binarizeGrad(const QImage& src,
const QSize windowSize,
const unsigned char lowerBound,
const unsigned char upperBound,
const double k,
const double delta) {
if (windowSize.isEmpty()) {
throw std::invalid_argument("binarizeGrad: invalid windowSize");
}
Expand Down Expand Up @@ -376,7 +381,7 @@ BinaryImage binarizeGrad(const QImage& src, const QSize windowSize, const double
const double threshold = meanGrad + mean * k;
const uint32_t msb = uint32_t(1) << 31;
const uint32_t mask = msb >> (x & 31);
if (origin < (threshold + delta)) {
if ((grayLine[x] < lowerBound) || ((grayLine[x] <= upperBound) && (origin < (threshold + delta)))) {
// black
bwLine[x >> 5] |= mask;
} else {
Expand Down
7 changes: 6 additions & 1 deletion src/imageproc/Binarize.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ BinaryImage binarizeBradley(const QImage& src, QSize windowSize, double k = 0.34
*
* Grad (aka "Gradient snip"), zvezdochiot 2024. "Adaptive/global document image binarization".
*/
BinaryImage binarizeGrad(const QImage& src, QSize windowSize, double k = 0.34, double delta = 0.0);
BinaryImage binarizeGrad(const QImage& src,
QSize windowSize,
unsigned char lowerBound = 1,
unsigned char upperBound = 254,
double k = 0.3,
double delta = 0.0);

/**
* \brief Image binarization using EdgeDiv (EdgePlus & BlurDiv) local/global thresholding method.
Expand Down

0 comments on commit c9683d2

Please sign in to comment.