diff --git a/Src/Config.cpp b/Src/Config.cpp index 1d102787e7b29b9d9c2d1b0809d74990f57638c6..aaed4dda68ad82a5a9420257ffb185249b9f3169 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 690b9b2ffbf9365622de9f1f8959e712e84bbb45..185581c4d14cf7a959889785c219994d9784fd59 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 1bdbab5cb34753163f3417f3babc885845b8299e..3641989f735984720b0a08725c293f0950b79592 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 2df14f01d5cd95311891818863ecd051f315a339..15dd7db2f93a319acf6822b4941d437c0bdec561 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 fab85807a338c7ca8626302b827886c23f526027..35398a504c7832453ccb616a640168c9656c2b24 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 9d611ca9665e7c74673b31a4c3a8222aba755ae7..e979826f243a0ad64eab05c0038e047a41d49ab9 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 92c2ab0c2712276d6875d05658e3eebbee0a11a2..5b1f9ba93a8e1c150d4888a4005aa020c3547b15 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 2cdb497189b54991b0e0709f5384dd19bb832ff9..254400a70b83777bd22842f4df65aaee92a51339 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 ad1c99f000b71f4546a08154a18c2a14fc4c0922..da49ce896d96c134c572ac370b38ab459daf872b 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);