From f1b319203bc583b3a4d3bd6dc4cbf491333d652b Mon Sep 17 00:00:00 2001
From: Moritz Maxeiner <mm@ucw.sh>
Date: Fri, 29 Jul 2022 19:34:15 +0200
Subject: [PATCH] Add maximum image value filter

---
 Src/Config.cpp                                |  5 ++++
 Src/Config.h                                  |  1 +
 Src/Model/TrackerParameter.cpp                | 12 +++++++++
 Src/Model/TrackerParameter.h                  |  4 +++
 .../CustomBackgroundSubtractor.cpp            | 26 ++++++++++++++-----
 .../preprocessor/CustomBackgroundSubtractor.h |  2 ++
 .../preprocessor/ImagePreProcessor.cpp        |  2 ++
 Src/View/TrackerParameterView.cpp             | 23 ++++++++++++++++
 Src/View/TrackerParameterView.h               |  1 +
 9 files changed, 69 insertions(+), 7 deletions(-)

diff --git a/Src/Config.cpp b/Src/Config.cpp
index 1d10278..aaed4dd 100644
--- a/Src/Config.cpp
+++ b/Src/Config.cpp
@@ -62,6 +62,9 @@ void Config::load(QString dir, QString file)
     config->BinarizationThreshold = tree.get<int>(
         globalPrefix + "BinarizationThreshold",
         config->BinarizationThreshold);
+    config->MaximumImageValue = tree.get<int>(
+        globalPrefix + "MaximumImageValue",
+        config->MaximumImageValue);
 
     config->OpeningErosionSize  = tree.get<int>(globalPrefix +
                                                    "OpeningErosionSize",
@@ -111,6 +114,8 @@ void Config::save(QString dir, QString file)
              config->UseAbsoluteDifference);
     tree.put(globalPrefix + "BinarizationThreshold",
              config->BinarizationThreshold);
+    tree.put(globalPrefix + "MaximumImageValue",
+             config->MaximumImageValue);
     tree.put(globalPrefix + "OpeningErosionSize", config->OpeningErosionSize);
     tree.put(globalPrefix + "OpeningDilationSize",
              config->OpeningDilationSize);
diff --git a/Src/Config.h b/Src/Config.h
index 690b9b2..185581c 100644
--- a/Src/Config.h
+++ b/Src/Config.h
@@ -9,6 +9,7 @@ class Config : public IConfig
 public:
     bool UseAbsoluteDifference = true;
     int  BinarizationThreshold = 40;
+    int  MaximumImageValue = 255;
 
     int OpeningErosionSize  = 8;
     int OpeningDilationSize = 8;
diff --git a/Src/Model/TrackerParameter.cpp b/Src/Model/TrackerParameter.cpp
index 1bdbab5..3641989 100644
--- a/Src/Model/TrackerParameter.cpp
+++ b/Src/Model/TrackerParameter.cpp
@@ -42,6 +42,18 @@ int TrackerParameter::getBinarizationThreshold()
     return _BinarizationThreshold;
 }
 
+void TrackerParameter::setMaximumImageValue(int x)
+{
+    _MaximumImageValue      = x;
+    _cfg->MaximumImageValue = x;
+    Q_EMIT notifyView();
+}
+
+int TrackerParameter::getMaximumImageValue()
+{
+    return _MaximumImageValue;
+}
+
 void TrackerParameter::setUseAbsoluteDifference(bool value)
 {
     _UseAbsoluteDifference      = value;
diff --git a/Src/Model/TrackerParameter.h b/Src/Model/TrackerParameter.h
index 2df14f0..15dd7db 100644
--- a/Src/Model/TrackerParameter.h
+++ b/Src/Model/TrackerParameter.h
@@ -27,6 +27,9 @@ public slots:
     void setBinarizationThreshold(int x);
     int  getBinarizationThreshold();
 
+    void setMaximumImageValue(int x);
+    int  getMaximumImageValue();
+
     int getOpeningErosionSize()
     {
         return _OpeningErosionSize;
@@ -171,6 +174,7 @@ private:
 
     bool _UseAbsoluteDifference;
     int  _BinarizationThreshold;
+    int  _MaximumImageValue;
 
     int _OpeningErosionSize;
     int _OpeningDilationSize;
diff --git a/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/CustomBackgroundSubtractor.cpp b/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/CustomBackgroundSubtractor.cpp
index fab8580..35398a5 100644
--- a/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/CustomBackgroundSubtractor.cpp
+++ b/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/CustomBackgroundSubtractor.cpp
@@ -8,6 +8,7 @@
 CustomBackgroundSubtractor::CustomBackgroundSubtractor()
 : m_useAbsoluteDifference{true}
 , m_binarizationThreshold{8}
+, m_maximumImageValue{255}
 {
 }
 
@@ -21,6 +22,11 @@ void CustomBackgroundSubtractor::setBinarizationThreshold(int value)
     m_binarizationThreshold = value;
 }
 
+void CustomBackgroundSubtractor::setMaximumImageValue(int value)
+{
+    m_maximumImageValue = value;
+}
+
 void CustomBackgroundSubtractor::getBackgroundImage(
     cv::OutputArray backgroundImage) const
 {
@@ -43,9 +49,7 @@ void CustomBackgroundSubtractor::apply(cv::InputArray  image,
     const int regionWidth   = imageWidth / totalRegionsX;
     const int regionHeight  = imageHeight / totalRegionsY;
 
-    fgmask.create(imageHeight, imageWidth, image.type());
-
-    auto fgmaskmat = fgmask.getMat();
+    auto diffimg = cv::Mat(imageHeight, imageWidth, image.type());
 
     const auto useAbsoluteDifference = m_useAbsoluteDifference;
 
@@ -56,7 +60,7 @@ void CustomBackgroundSubtractor::apply(cv::InputArray  image,
             cv::Rect(startingX, startingY, regionWidth, regionHeight);
         cv::Mat subBackground = m_background(subArea);
         cv::Mat subImage      = image.getMat()(subArea);
-        cv::Mat subResults    = fgmaskmat(subArea);
+        cv::Mat subResults    = diffimg(subArea);
 
         if (useAbsoluteDifference) {
             cv::absdiff(subBackground, subImage, subResults);
@@ -81,11 +85,19 @@ void CustomBackgroundSubtractor::apply(cv::InputArray  image,
         asyncResult.wait();
     }
 
-    if (fgmaskmat.data) {
-        cv::threshold(fgmaskmat,
-                      fgmaskmat,
+    if (diffimg.data) {
+        cv::Mat diffmask;
+
+        cv::Mat maxabsmask;
+
+        cv::threshold(diffimg,
+                      diffmask,
                       m_binarizationThreshold,
                       255,
                       cv::THRESH_BINARY);
+
+        cv::threshold(image, maxabsmask, m_maximumImageValue, 255, cv::THRESH_BINARY_INV);
+
+        cv::bitwise_and(diffmask, maxabsmask, fgmask);
     }
 }
diff --git a/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/CustomBackgroundSubtractor.h b/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/CustomBackgroundSubtractor.h
index 9d611ca..e979826 100644
--- a/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/CustomBackgroundSubtractor.h
+++ b/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/CustomBackgroundSubtractor.h
@@ -15,6 +15,7 @@ public:
 
     void setUseAbsoluteDifference(bool value);
     void setBinarizationThreshold(int value);
+    void setMaximumImageValue(int value);
 
 private:
     cv::Mat m_background;
@@ -22,4 +23,5 @@ private:
     bool m_useAbsoluteDifference;
 
     int m_binarizationThreshold;
+    int m_maximumImageValue;
 };
diff --git a/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.cpp b/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.cpp
index 92c2ab0..5b1f9ba 100644
--- a/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.cpp
+++ b/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.cpp
@@ -88,6 +88,8 @@ cv::Mat ImagePreProcessor::backgroundSubtraction(cv::Mat& image)
             m_TrackingParameter->getUseAbsoluteDifference());
         subtractor->setBinarizationThreshold(
             m_TrackingParameter->getBinarizationThreshold());
+        subtractor->setMaximumImageValue(
+            m_TrackingParameter->getMaximumImageValue());
     } else {
         qFatal("Unsupported background subtraction algorithm");
     }
diff --git a/Src/View/TrackerParameterView.cpp b/Src/View/TrackerParameterView.cpp
index 2cdb497..254400a 100644
--- a/Src/View/TrackerParameterView.cpp
+++ b/Src/View/TrackerParameterView.cpp
@@ -10,6 +10,7 @@ TrackerParameterView::TrackerParameterView(QWidget*     parent,
 , _ui(new Ui::TrackerParameterView)
 , _useAbsDiff(nullptr)
 , _binThres(nullptr)
+, _maxImg(nullptr)
 {
     _ui->setupUi(this);
     getNotified();
@@ -108,6 +109,7 @@ void TrackerParameterView::initSubtractorSpecificUI(QString algorithm)
 
     _useAbsDiff = nullptr;
     _binThres   = nullptr;
+    _maxImg   = nullptr;
 
     if (algorithm == QString("Custom")) {
         _useAbsDiff = new QCheckBox();
@@ -142,6 +144,23 @@ void TrackerParameterView::initSubtractorSpecificUI(QString algorithm)
                 qOverload<int>(&QSpinBox::valueChanged),
                 this,
                 &TrackerParameterView::parametersChanged);
+ 
+        _maxImg = new QSpinBox();
+        _maxImg->setMinimum(1);
+        _maxImg->setMaximum(255);
+        _maxImg->setValue(parameter->getMaximumImageValue());
+        _ui->algorithmSpecificParameterLayout->addRow(
+            tr("Maximum Image Value:"),
+            _maxImg);
+
+        connect(_maxImg,
+                qOverload<int>(&QSpinBox::valueChanged),
+                parameter,
+                &TrackerParameter::setMaximumImageValue);
+        connect(_maxImg,
+                qOverload<int>(&QSpinBox::valueChanged),
+                this,
+                &TrackerParameterView::parametersChanged);
     } else {
         qFatal("Unsupported background subtraction algorithm");
     }
@@ -177,6 +196,10 @@ void TrackerParameterView::getNotified()
         _binThres->setValue(parameter->getBinarizationThreshold());
     }
 
+    if (_maxImg) {
+        _maxImg->setValue(parameter->getMaximumImageValue());
+    }
+
     _ui->lineEdit_3_OpeningErosionSize->setValue(
         parameter->getOpeningErosionSize());
 
diff --git a/Src/View/TrackerParameterView.h b/Src/View/TrackerParameterView.h
index ad1c99f..da49ce8 100644
--- a/Src/View/TrackerParameterView.h
+++ b/Src/View/TrackerParameterView.h
@@ -37,6 +37,7 @@ private:
     QCheckBox* _useAbsDiff;
 
     QSpinBox* _binThres;
+    QSpinBox* _maxImg;
 
 private slots:
     void initSubtractorSpecificUI(QString algorithm);
-- 
GitLab