Skip to content

Commit

Permalink
0.2024.07.18: threshold EdgeDiv: add Adapt mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Jul 18, 2024
1 parent cd19bb4 commit 249d4e7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 8 deletions.
42 changes: 38 additions & 4 deletions src/imageproc/Binarize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ GrayImage binarizeEdgeDivPrefilter(
{
if (window_size.isEmpty())
{
throw std::invalid_argument("binarizeBlurDivPrefilter: invalid window_size");
throw std::invalid_argument("binarizeeEdgeDivPrefilter: invalid window_size");
}

if (src.isNull())
Expand Down Expand Up @@ -1316,22 +1316,56 @@ GrayImage binarizeEdgeDivPrefilter(
uint8_t* gmean_line = gmean.data();
int const gmean_stride = gmean.stride();

double kepw = kep;
// Adapt mode
if (kep < 0.0)
{
kepw = 0.0;
double kepl = 0.0, kd = 0.0, kdl = 0.0, gdelta;
for (int y = 0; y < h; y++)
{
kdl = 0.0;
kepl = 0.0;
for (int x = 0; x < w; x++)
{
double const mean = gmean_line[x];
double const origin = gray_line[x];
gdelta = (origin < mean) ? (mean - origin) : (origin - mean);
kdl += gdelta;
kepl += (gdelta * gdelta);
}
kd += kdl;
kepw += kepl;
gray_line += gray_stride;
gmean_line += gmean_stride;
}
kepw = (kd > 0.0) ? (kepw / kd) : 0.0;
kepw /= 64.0;
kepw = 1.0 - kepw;
kepw = (kepw < 0.0) ? 0.0 : kepw;
kepw *= kbd;

gray_line = gray.data();
gmean_line = gmean.data();
}
// Adapt mode end

for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
float const mean = gmean_line[x];
float const origin = gray_line[x];
float retval = origin;
if (kep > 0.0)
if (kepw > 0.0)
{
// EdgePlus
// edge = I / blur (shift = -0.5) {0.0 .. >1.0}, mean value = 0.5
float const edge = (retval + 1) / (mean + 1) - 0.5;
// edgeplus = I * edge, mean value = 0.5 * mean(I)
float const edgeplus = origin * edge;
// return k * edgeplus + (1 - k) * I
retval = kep * edgeplus + (1.0 - kep) * origin;
retval = kepw * edgeplus + (1.0 - kepw) * origin;
}
if (kbd > 0.0)
{
Expand Down Expand Up @@ -1360,7 +1394,7 @@ BinaryImage binarizeEdgeDiv(
{
if (window_size.isEmpty())
{
throw std::invalid_argument("binarizeBlurDiv: invalid window_size");
throw std::invalid_argument("binarizeeEdgeDiv: invalid window_size");
}

if (src.isNull())
Expand Down
7 changes: 7 additions & 0 deletions src/stages/output/BlackWhiteOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ BlackWhiteOptions::parseThresholdMethod(QString const& str)
{
return T_EDGEDIV;
}
else if (str == "edgeadapt")
{
return T_EDGEADAPT;
}
else if (str == "robust")
{
return T_ROBUST;
Expand Down Expand Up @@ -253,6 +257,9 @@ BlackWhiteOptions::formatThresholdMethod(ThresholdFilter type)
case T_EDGEDIV:
str = "edgediv";
break;
case T_EDGEADAPT:
str = "edgeadapt";
break;
case T_ROBUST:
str = "robust";
break;
Expand Down
2 changes: 1 addition & 1 deletion src/stages/output/BlackWhiteOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class QDomElement;
namespace output
{

enum ThresholdFilter { T_OTSU, T_MEANDELTA, T_DOTS8, T_NIBLACK, T_GATOS, T_SAUVOLA, T_WOLF, T_BRADLEY, T_GRAD, T_SINGH, T_WAN, T_EDGEPLUS, T_BLURDIV, T_EDGEDIV, T_ROBUST, T_MSCALE };
enum ThresholdFilter { T_OTSU, T_MEANDELTA, T_DOTS8, T_NIBLACK, T_GATOS, T_SAUVOLA, T_WOLF, T_BRADLEY, T_GRAD, T_SINGH, T_WAN, T_EDGEPLUS, T_BLURDIV, T_EDGEDIV, T_EDGEADAPT, T_ROBUST, T_MSCALE };

class BlackWhiteOptions
{
Expand Down
1 change: 1 addition & 0 deletions src/stages/output/OptionsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ OptionsWidget::OptionsWidget(
thresholdMethodSelector->addItem(tr("EdgePlus"), T_EDGEPLUS);
thresholdMethodSelector->addItem(tr("BlurDiv"), T_BLURDIV);
thresholdMethodSelector->addItem(tr("EdgeDiv"), T_EDGEDIV);
thresholdMethodSelector->addItem(tr("EdgeAdapt"), T_EDGEADAPT);
thresholdMethodSelector->addItem(tr("Robust"), T_ROBUST);
thresholdMethodSelector->addItem(tr("MultiScale"), T_MSCALE);

Expand Down
2 changes: 1 addition & 1 deletion src/stages/output/OptionsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private slots:
void sqrCoefChanged(double value);

void colorFilterGet();

void colorFilterChanged(int idx);

void colorFilterSizeChanged(int value);
Expand Down
5 changes: 5 additions & 0 deletions src/stages/output/OutputGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,11 @@ OutputGenerator::binarize(QImage const& image, BinaryImage const& mask) const
binarized = binarizeEdgeDiv(gray, window_size, threshold_coef, threshold_coef, threshold_delta);
break;
}
case T_EDGEADAPT:
{
binarized = binarizeEdgeDiv(gray, window_size, -1.0, threshold_coef, threshold_delta);
break;
}
case T_ROBUST:
{
binarized = binarizeRobust(gray, window_size, threshold_coef, threshold_delta);
Expand Down
2 changes: 1 addition & 1 deletion src/stages/select_content/ContentBoxFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ ContentBoxFinder::findContentBox(TaskStatus const& status,

//BinaryImage bw150(binarizeWolf(GrayImage(gray150), QSize(51, 51), 50));
//BinaryImage bw150(binarizeEdgeDiv(GrayImage(gray150), QSize(51, 51), 0.5, 1.0, 0));
BinaryImage bw150(binarizeGatos(GrayImage(gray150), QSize(51, 51)));
BinaryImage bw150(binarizeEdgeDiv(GrayImage(gray150), QSize(51, 51), -1.0, 0.75, 0));
if (dbg)
{
dbg->add(bw150, "bw150");
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
#define SCANTAILOR_VERSION_H_

#define STFAMILY "experimental"
#define VERSION "0.2024.07.16" // Must be "x.x.x.x" or an empty string.
#define VERSION "0.2024.07.18" // Must be "x.x.x.x" or an empty string.

#endif

0 comments on commit 249d4e7

Please sign in to comment.