Skip to content

Commit 76e912d

Browse files
committed
UI for Gatos params test
1 parent a83daf7 commit 76e912d

10 files changed

+198
-21
lines changed

src/core/filters/output/BlackWhiteOptions.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ BlackWhiteOptions::BlackWhiteOptions():
3131
m_thresholdForegroundAdjustment(m_thresholdAdjustment),
3232
m_thresholdMethod(OTSU),
3333
m_thresholdWindowSize(200),
34-
m_thresholdCoef(0.3)
34+
m_thresholdCoef(0.3),
35+
m_q(0.6),
36+
m_p1(0.5),
37+
m_p2(0.8)
3538
{
3639
}
3740

@@ -40,7 +43,11 @@ BlackWhiteOptions::BlackWhiteOptions(QDomElement const& el)
4043
m_thresholdForegroundAdjustment(el.attribute("thresholdForegAdj").toInt()),
4144
m_thresholdMethod(parseThresholdMethod(el.attribute("thresholdMethod"))),
4245
m_thresholdWindowSize(el.attribute("thresholdWinSize").toInt()),
43-
m_thresholdCoef(el.attribute("thresholdCoef").toDouble())
46+
m_thresholdCoef(el.attribute("thresholdCoef").toDouble()),
47+
48+
m_q(el.attribute("q").toDouble()),
49+
m_p1(el.attribute("p1").toDouble()),
50+
m_p2(el.attribute("p2").toDouble())
4451
{
4552
if (m_thresholdWindowSize <= 0)
4653
{
@@ -50,6 +57,11 @@ BlackWhiteOptions::BlackWhiteOptions(QDomElement const& el)
5057
{
5158
m_thresholdCoef = 0.0;
5259
}
60+
61+
// move to init list
62+
m_q = qBound(0.0, m_q, 1.0);
63+
m_p1 = qBound(0.0, m_p1, 0.99);
64+
m_p2 = qBound(0.0, m_p2, 0.99);
5365
}
5466

5567
QDomElement
@@ -61,6 +73,9 @@ BlackWhiteOptions::toXml(QDomDocument& doc, QString const& name) const
6173
el.setAttribute("thresholdMethod", formatThresholdMethod(m_thresholdMethod));
6274
el.setAttribute("thresholdWinSize", m_thresholdWindowSize);
6375
el.setAttribute("thresholdCoef", m_thresholdCoef);
76+
el.setAttribute("q", m_q);
77+
el.setAttribute("p1", m_p1);
78+
el.setAttribute("p2", m_p2);
6479

6580
return el;
6681
}
@@ -86,6 +101,18 @@ BlackWhiteOptions::operator==(BlackWhiteOptions const& other) const
86101
{
87102
return false;
88103
}
104+
if (m_q != other.m_q)
105+
{
106+
return false;
107+
}
108+
if (m_p1 != other.m_p1)
109+
{
110+
return false;
111+
}
112+
if (m_p2 != other.m_p2)
113+
{
114+
return false;
115+
}
89116

90117
return true;
91118
}

src/core/filters/output/BlackWhiteOptions.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ class BlackWhiteOptions
8787
m_thresholdCoef = val;
8888
}
8989

90+
double q() const { return m_q; }
91+
double p1() const { return m_p1; }
92+
double p2() const { return m_p2; }
93+
94+
void setQCoef(double q) { m_q = q; }
95+
void setP1Coef(double p1) { m_p1 = p1; }
96+
void setP2Coef(double p2) { m_p2 = p2; }
97+
9098
bool operator==(BlackWhiteOptions const& other) const;
9199

92100
bool operator!=(BlackWhiteOptions const& other) const;
@@ -97,6 +105,10 @@ class BlackWhiteOptions
97105
int m_thresholdWindowSize;
98106
double m_thresholdCoef;
99107

108+
double m_q;
109+
double m_p1;
110+
double m_p2;
111+
100112
static ThresholdFilter parseThresholdMethod(QString const& str);
101113

102114
static QString formatThresholdMethod(ThresholdFilter type);

src/core/filters/output/OptionsWidget.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ OptionsWidget::OptionsWidget(
174174
this, SLOT(thresholdCoefChanged(double))
175175
);
176176

177+
connect(
178+
qCoef, SIGNAL(valueChanged(double)),
179+
this, SLOT(qCoefChanged(double))
180+
);
181+
connect(
182+
p1Coef, SIGNAL(valueChanged(double)),
183+
this, SLOT(p1CoefChanged(double))
184+
);
185+
connect(
186+
p2Coef, SIGNAL(valueChanged(double)),
187+
this, SLOT(p2CoefChanged(double))
188+
);
189+
177190
addAction(actionactionDespeckleOff);
178191
addAction(actionactionDespeckleNormal);
179192
addAction(actionactionDespeckleCautious);
@@ -277,6 +290,39 @@ OptionsWidget::thresholdCoefChanged(double value) {
277290
emit reloadRequested();
278291
}
279292

293+
void
294+
OptionsWidget::qCoefChanged(double value)
295+
{
296+
BlackWhiteOptions blackWhiteOptions(m_colorParams.blackWhiteOptions());
297+
blackWhiteOptions.setQCoef(value);
298+
m_colorParams.setBlackWhiteOptions(blackWhiteOptions);
299+
m_ptrSettings->setColorParams(m_pageId, m_colorParams);
300+
if (blackWhiteOptions.thresholdMethod() != OTSU && blackWhiteOptions.thresholdMethod() != MEANDELTA)
301+
emit reloadRequested();
302+
}
303+
304+
void
305+
OptionsWidget::p1CoefChanged(double value)
306+
{
307+
BlackWhiteOptions blackWhiteOptions(m_colorParams.blackWhiteOptions());
308+
blackWhiteOptions.setP1Coef(value);
309+
m_colorParams.setBlackWhiteOptions(blackWhiteOptions);
310+
m_ptrSettings->setColorParams(m_pageId, m_colorParams);
311+
if (blackWhiteOptions.thresholdMethod() != OTSU && blackWhiteOptions.thresholdMethod() != MEANDELTA)
312+
emit reloadRequested();
313+
}
314+
315+
void
316+
OptionsWidget::p2CoefChanged(double value)
317+
{
318+
BlackWhiteOptions blackWhiteOptions(m_colorParams.blackWhiteOptions());
319+
blackWhiteOptions.setP2Coef(value);
320+
m_colorParams.setBlackWhiteOptions(blackWhiteOptions);
321+
m_ptrSettings->setColorParams(m_pageId, m_colorParams);
322+
if (blackWhiteOptions.thresholdMethod() != OTSU && blackWhiteOptions.thresholdMethod() != MEANDELTA)
323+
emit reloadRequested();
324+
}
325+
280326
void
281327
OptionsWidget::modeSelectorIndexChanged(int idx)
282328
{
@@ -534,6 +580,9 @@ OptionsWidget::updateColorsDisplay()
534580
thresholdSlider->setValue(blackWhiteOptions.thresholdAdjustment());
535581
thresholdWindowSize->setValue(blackWhiteOptions.thresholdWindowSize());
536582
thresholdCoef->setValue(blackWhiteOptions.thresholdCoef());
583+
qCoef->setValue(blackWhiteOptions.q());
584+
p1Coef->setValue(blackWhiteOptions.p1());
585+
p2Coef->setValue(blackWhiteOptions.p2());
537586
if (blackWhiteOptions.thresholdMethod() == OTSU || blackWhiteOptions.thresholdMethod() == MEANDELTA)
538587
{
539588
thresholdWindowSize->setEnabled( false );
@@ -544,6 +593,18 @@ OptionsWidget::updateColorsDisplay()
544593
thresholdWindowSize->setEnabled( true );
545594
thresholdCoef->setEnabled( true );
546595
}
596+
if (blackWhiteOptions.thresholdMethod() == GATOS)
597+
{
598+
qCoef->setEnabled(true);
599+
p1Coef->setEnabled(true);
600+
p2Coef->setEnabled(true);
601+
}
602+
else
603+
{
604+
qCoef->setDisabled(true);
605+
p1Coef->setDisabled(true);
606+
p2Coef->setDisabled(true);
607+
}
547608
}
548609

549610
if (despeckle_controls_enbled) {

src/core/filters/output/OptionsWidget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ private slots:
104104

105105
void thresholdCoefChanged(double value);
106106

107+
void qCoefChanged(double value);
108+
void p1CoefChanged(double value);
109+
void p2CoefChanged(double value);
110+
107111
void dpiValueClicked();
108112

109113
void applyDpiButtonClicked();

src/core/filters/output/OutputGenerator.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,9 @@ OutputGenerator::binarize(QImage const& image, BinaryImage const& mask, const in
12301230
int const threshold_delta = black_white_options.thresholdAdjustment();
12311231
QSize const window_size = QSize(black_white_options.thresholdWindowSize(), black_white_options.thresholdWindowSize());
12321232
double const threshold_coef = black_white_options.thresholdCoef();
1233+
double const q = black_white_options.q();
1234+
double const p1 = black_white_options.p1();
1235+
double const p2 = black_white_options.p2();
12331236

12341237
BinaryImage binarized;
12351238
if ((image.format() == QImage::Format_Mono) || (image.format() == QImage::Format_MonoLSB))
@@ -1259,7 +1262,7 @@ OutputGenerator::binarize(QImage const& image, BinaryImage const& mask, const in
12591262
}
12601263
case GATOS:
12611264
{
1262-
binarized = binarizeGatos(image, window_size, 3.0, threshold_coef, threshold_delta);
1265+
binarized = binarizeGatos(image, window_size, q, p1, p2, 3.0, threshold_coef, threshold_delta);
12631266
break;
12641267
}
12651268
case SAUVOLA:

src/core/filters/output/ui/OutputOptionsWidget.ui

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>161</width>
10-
<height>687</height>
10+
<height>765</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -461,10 +461,19 @@
461461
</item>
462462
<item>
463463
<layout class="QGridLayout" name="gridLayout">
464-
<item row="0" column="0">
465-
<widget class="QLabel" name="thresholdWindowSizeLabel">
466-
<property name="text">
467-
<string>Window size:</string>
464+
<item row="3" column="1">
465+
<widget class="QDoubleSpinBox" name="p1Coef">
466+
<property name="toolTip">
467+
<string>Default value is 0.34.</string>
468+
</property>
469+
<property name="minimum">
470+
<double>0.000000000000000</double>
471+
</property>
472+
<property name="maximum">
473+
<double>0.990000000000000</double>
474+
</property>
475+
<property name="singleStep">
476+
<double>0.010000000000000</double>
468477
</property>
469478
</widget>
470479
</item>
@@ -481,6 +490,27 @@
481490
</property>
482491
</widget>
483492
</item>
493+
<item row="1" column="0">
494+
<widget class="QLabel" name="thresholdCoefLabel">
495+
<property name="text">
496+
<string>Coef:</string>
497+
</property>
498+
</widget>
499+
</item>
500+
<item row="0" column="0">
501+
<widget class="QLabel" name="thresholdWindowSizeLabel">
502+
<property name="text">
503+
<string>Window size:</string>
504+
</property>
505+
</widget>
506+
</item>
507+
<item row="3" column="0">
508+
<widget class="QLabel" name="p1CoefLabel">
509+
<property name="text">
510+
<string>p1:</string>
511+
</property>
512+
</widget>
513+
</item>
484514
<item row="1" column="1">
485515
<widget class="QDoubleSpinBox" name="thresholdCoef">
486516
<property name="toolTip">
@@ -497,10 +527,49 @@
497527
</property>
498528
</widget>
499529
</item>
500-
<item row="1" column="0">
501-
<widget class="QLabel" name="thresholdCoefLabel">
530+
<item row="2" column="0">
531+
<widget class="QLabel" name="qCoefLabel">
502532
<property name="text">
503-
<string>Coef:</string>
533+
<string>q:</string>
534+
</property>
535+
</widget>
536+
</item>
537+
<item row="2" column="1">
538+
<widget class="QDoubleSpinBox" name="qCoef">
539+
<property name="toolTip">
540+
<string>Default value is 0.34.</string>
541+
</property>
542+
<property name="minimum">
543+
<double>0.000000000000000</double>
544+
</property>
545+
<property name="maximum">
546+
<double>1.000000000000000</double>
547+
</property>
548+
<property name="singleStep">
549+
<double>0.010000000000000</double>
550+
</property>
551+
</widget>
552+
</item>
553+
<item row="4" column="0">
554+
<widget class="QLabel" name="p2CoefLabel">
555+
<property name="text">
556+
<string>p2:</string>
557+
</property>
558+
</widget>
559+
</item>
560+
<item row="4" column="1">
561+
<widget class="QDoubleSpinBox" name="p2Coef">
562+
<property name="toolTip">
563+
<string>Default value is 0.34.</string>
564+
</property>
565+
<property name="minimum">
566+
<double>0.000000000000000</double>
567+
</property>
568+
<property name="maximum">
569+
<double>0.990000000000000</double>
570+
</property>
571+
<property name="singleStep">
572+
<double>0.010000000000000</double>
504573
</property>
505574
</widget>
506575
</item>

src/dewarping/TextLineSegmenter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ double
410410
TextLineSegmenter::findSkewAngleRad(
411411
GrayImage const& image, TaskStatus const& status, DebugImages* dbg)
412412
{
413-
BinaryImage const binarized(binarizeGatos(image, QSize(9, 9), 3.0));
413+
BinaryImage const binarized(binarizeGatos(image, QSize(9, 9), 3.0, 0.6, 0.5, 0.8));
414414

415415
status.throwIfCancelled();
416416

@@ -1096,7 +1096,7 @@ std::list<std::vector<QPointF>>
10961096
dbg->add(seed, "vert_comps_removed");
10971097
}
10981098

1099-
BinaryImage binarized(binarizeGatos(seed, QSize(9, 9), 3.0));
1099+
BinaryImage binarized(binarizeGatos(seed, QSize(9, 9), 3.0, 0.6, 0.5, 0.8));
11001100
seed = GrayImage();
11011101

11021102
status.throwIfCancelled();

src/dewarping/TextLineTracer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ TextLineTracer::trace(
124124

125125
status.throwIfCancelled();
126126

127-
downscaled_image = GrayImage(binarizeGatos(downscaled_image, QSize(21, 21), 3.0).toQImage());
127+
downscaled_image = GrayImage(binarizeGatos(downscaled_image, QSize(21, 21), 3.0, 0.6, 0.5, 0.8).toQImage());
128128

129129
status.throwIfCancelled();
130130

src/imageproc/Binarize.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ BinaryImage binarizeNiblack(
348348

349349
BinaryImage binarizeGatosCleaner(
350350
GrayImage& wiener, BinaryImage const& niblack,
351-
QSize const window_size)
351+
QSize const window_size,
352+
double q, double p1, double p2)
352353
{
353354
if (window_size.isEmpty())
354355
{
@@ -458,10 +459,6 @@ BinaryImage binarizeGatosCleaner(
458459
double const delta = double(sum_diff) / (w*h - niblack_bg_ii.sum(image_rect));
459460
double const b = double(sum_bg) / niblack_bg_ii.sum(image_rect);
460461

461-
double const q = 0.6;
462-
double const p1 = 0.5;
463-
double const p2 = 0.8;
464-
465462
double const exp_scale = -4.0 / (b * (1.0 - p1));
466463
double const exp_bias = 2.0 * (1.0 + p1) / (1.0 - p1);
467464
double const threshold_scale = q * delta * (1.0 - p2);
@@ -488,6 +485,7 @@ BinaryImage binarizeGatosCleaner(
488485

489486
BinaryImage binarizeGatos(
490487
QImage const& src, QSize const window_size,
488+
double q, double p1, double p2,
491489
double const noise_sigma, double const k, int const delta)
492490
{
493491
if (window_size.isEmpty())
@@ -508,7 +506,7 @@ BinaryImage binarizeGatos(
508506

509507
GrayImage wiener(wienerFilter(gray, QSize(5, 5), noise_sigma));
510508
BinaryImage niblack(binarizeNiblack(wiener, window_size, k, delta));
511-
BinaryImage bw_img(binarizeGatosCleaner(wiener, niblack, window_size));
509+
BinaryImage bw_img(binarizeGatosCleaner(wiener, niblack, window_size, q, p1, p2));
512510

513511
return bw_img;
514512
}

0 commit comments

Comments
 (0)