diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d9b45a797185fea994bb163810534927824c907..df196cb74b16419ca6e7a2f9f5596e4b2a733b0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,10 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") endif() option(PACKAGE_TXZ "Create .tar.xz package" OFF) +find_package(OpenCV 4 QUIET COMPONENTS bgsegm) +if(NOT OpenCV_FOUND) + find_package(OpenCV 3 REQUIRED COMPONENTS bgsegm) +endif() find_package(biotracker-utility REQUIRED) add_subdirectory(Src) diff --git a/Src/CMakeLists.txt b/Src/CMakeLists.txt index 8dc0aae19d643b26a11869fd1d106484f2f205ea..9b2e99aef13fef3747ac3b399c93683e4d675b90 100644 --- a/Src/CMakeLists.txt +++ b/Src/CMakeLists.txt @@ -47,3 +47,5 @@ add_behavior_plugin(${target} install(TARGETS ${target} OPTIONAL DESTINATION .) find_package(Qt5 REQUIRED COMPONENTS Xml Network) target_link_libraries (${target} Qt5::Xml Qt5::Network) + +target_link_libraries(${target} ${OpenCV_LIBS}) diff --git a/Src/Model/BioTrackerTrackingAlgorithm.cpp b/Src/Model/BioTrackerTrackingAlgorithm.cpp index 483c498584b1335ca6c4cd00abab38fe5ea999e7..52a3788c41d2afe10bc040a68a79f3a17bed358d 100644 --- a/Src/Model/BioTrackerTrackingAlgorithm.cpp +++ b/Src/Model/BioTrackerTrackingAlgorithm.cpp @@ -5,7 +5,7 @@ BioTrackerTrackingAlgorithm::BioTrackerTrackingAlgorithm(IController *parent, IModel* parameter, IModel* trajectory) : IModelTrackingAlgorithm(parent) -, _ipp((TrackerParameter*)parameter) +, _ipp((TrackerParameter*) parameter) { _cfg = static_cast<ControllerTrackingAlgorithm*>(parent)->getConfig(); _TrackingParameter = (TrackerParameter*)parameter; diff --git a/Src/Model/TrackerParameter.cpp b/Src/Model/TrackerParameter.cpp index b15604243f72f84eb932e5cf160b30122587a7e0..8363a86f349c72024abc3ace7309da7c097f4201 100644 --- a/Src/Model/TrackerParameter.cpp +++ b/Src/Model/TrackerParameter.cpp @@ -23,6 +23,8 @@ TrackerParameter::TrackerParameter(QObject *parent) : _sendImage = 0; //Send no image _resetBackground = false; + _algorithm = "Custom"; + Q_EMIT notifyView(); } diff --git a/Src/Model/TrackerParameter.h b/Src/Model/TrackerParameter.h index 02479e022ffb6f61589bd88342171b51872db293..db3384fbf01cbc01b583f84a5311f5ed669ee998 100644 --- a/Src/Model/TrackerParameter.h +++ b/Src/Model/TrackerParameter.h @@ -13,6 +13,11 @@ public: public slots: + QString getAlgorithm() { return _algorithm; } + void setAlgorithm(QString algorithm) { + _algorithm = algorithm; + } + void setBinarizationThreshold(int x); int getBinarizationThreshold(); @@ -110,6 +115,8 @@ public slots: private: + QString _algorithm; + int _BinarizationThreshold; int _SizeErode; int _SizeDilate; diff --git a/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.cpp b/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.cpp index 3a52f7a23996f5707fd6caff2a00f9974d24c4ba..621d935328b1c80e9576439420c573d6f7dfcfd4 100644 --- a/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.cpp +++ b/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.cpp @@ -4,14 +4,16 @@ #include <QMutex> +#include <opencv2/bgsegm.hpp> + #include "CustomBackgroundSubtractor.h" QMutex bgsMutex; -ImagePreProcessor::ImagePreProcessor(TrackerParameter* p_TrackingParameter) : - m_subtractor(new CustomBackgroundSubtractor()) +ImagePreProcessor::ImagePreProcessor(TrackerParameter* TrackingParameter) : + m_subtractor(nullptr), + m_TrackingParameter{TrackingParameter} { - _TrackingParameter = p_TrackingParameter; init(); } @@ -24,6 +26,13 @@ void ImagePreProcessor::init() { QMutexLocker locker(&bgsMutex); + auto algorithm = m_TrackingParameter->getAlgorithm(); + if (algorithm == QString("Custom")) { + m_subtractor = new CustomBackgroundSubtractor(); + } else { + qFatal("Unsupported background subtraction algorithm"); + } + m_backgroundImage = std::make_shared<cv::Mat>(); m_foregroundMask = std::make_shared<cv::Mat>(); @@ -72,8 +81,13 @@ cv::Mat ImagePreProcessor::dilate(cv::Mat& image) cv::Mat ImagePreProcessor::backgroundSubtraction(cv::Mat& image) { - if (auto subtractor = dynamic_cast<CustomBackgroundSubtractor*>(m_subtractor); subtractor) { + if (auto subtractor = m_subtractor.dynamicCast<CustomBackgroundSubtractor>(); subtractor) { + if (m_TrackingParameter->getAlgorithm() != QString("Custom")) { + init(); + } subtractor->setBinarizationThreshold(m_TrackingParameter->getBinarizationThreshold()); + } else { + qFatal("Unsupported background subtraction algorithm"); } cv::Mat fgmask; diff --git a/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.h b/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.h index 9df20d0499088a3d8cbe13f6cc27a3898940678d..bb03f38eddc924a97fbb7e6ade66480a1fb0cc48 100644 --- a/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.h +++ b/Src/Model/TrackingAlgorithm/imageProcessor/preprocessor/ImagePreProcessor.h @@ -15,12 +15,12 @@ public: /** * The standard constructor. */ - ImagePreProcessor(TrackerParameter* p_TrackingParameter); + ImagePreProcessor(TrackerParameter* TrackingParameter); /** * The standard destructor. */ - ~ImagePreProcessor(void); + ~ImagePreProcessor(); /** * Init function. Sets the property for the imge pre-processing. @@ -86,7 +86,5 @@ private: bool _gaussianBlurEnabled; bool _resetBackgroundImageEnabled; - TrackerParameter* _TrackingParameter; - - cv::BackgroundSubtractor* m_subtractor; + cv::Ptr<cv::BackgroundSubtractor> m_subtractor; }; diff --git a/Src/View/TrackerParameterView.cpp b/Src/View/TrackerParameterView.cpp index 984c2c9066c43f47e5b40e55138022fb281090c6..bf585931726cd12ee076a316becfa56d6de51710 100644 --- a/Src/View/TrackerParameterView.cpp +++ b/Src/View/TrackerParameterView.cpp @@ -4,15 +4,16 @@ #include <iostream> TrackerParameterView::TrackerParameterView(QWidget *parent, IController *controller, IModel *model) : IViewWidget(parent, controller, model), - _ui(new Ui::TrackerParameterView) + _ui(new Ui::TrackerParameterView), + _binThres(nullptr) { _ui->setupUi(this); getNotified(); auto parameter = qobject_cast<TrackerParameter *>(getModel()); - connect(_ui->lineEdit_2_binThresh, qOverload<int>(&QSpinBox::valueChanged), parameter, &TrackerParameter::setBinarizationThreshold); - connect(_ui->lineEdit_2_binThresh, qOverload<int>(&QSpinBox::valueChanged), this, &TrackerParameterView::parametersChanged); + connect(_ui->algorithmCB, &QComboBox::currentTextChanged, parameter, &TrackerParameter::setAlgorithm); + connect(_ui->algorithmCB, &QComboBox::currentTextChanged, this, &TrackerParameterView::initSubtractorSpecificUI); connect(_ui->lineEdit_3_SizeErode, qOverload<int>(&QSpinBox::valueChanged), parameter, &TrackerParameter::setSizeErode); connect(_ui->lineEdit_3_SizeErode, qOverload<int>(&QSpinBox::valueChanged), this, &TrackerParameterView::parametersChanged); @@ -28,6 +29,8 @@ TrackerParameterView::TrackerParameterView(QWidget *parent, IController *control connect(_ui->lineEdit_7_learningRate, qOverload<double>(&QDoubleSpinBox::valueChanged), parameter, &TrackerParameter::setLearningRate); connect(_ui->lineEdit_7_learningRate, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &TrackerParameterView::parametersChanged); + + initSubtractorSpecificUI(_ui->algorithmCB->currentText()); } TrackerParameterView::~TrackerParameterView() @@ -35,6 +38,34 @@ TrackerParameterView::~TrackerParameterView() delete _ui; } +void TrackerParameterView::initSubtractorSpecificUI(QString algorithm) +{ + auto parameter = qobject_cast<TrackerParameter *>(getModel()); + + while (_ui->algorithmSpecificParameterLayout->rowCount() > 0) { + _ui->algorithmSpecificParameterLayout->removeRow(0); + } + + if (_binThres) { + _binThres = nullptr; + } + + if (algorithm == QString("Custom")) { + _binThres = new QSpinBox(); + _binThres->setMinimum(1); + _binThres->setMaximum(255); + _binThres->setValue(parameter->getBinarizationThreshold()); + _ui->algorithmSpecificParameterLayout->addRow(tr("Binarization Threshold:"), _binThres); + + connect(_binThres, qOverload<int>(&QSpinBox::valueChanged), parameter, &TrackerParameter::setBinarizationThreshold); + connect(_binThres, qOverload<int>(&QSpinBox::valueChanged), this, &TrackerParameterView::parametersChanged); + } else { + qFatal("Unsupported background subtraction algorithm"); + } + + emit parametersChanged(); +} + void TrackerParameterView::on_pushButtonResetBackground_clicked() { TrackerParameter *parameter = qobject_cast<TrackerParameter *>(getModel()); @@ -52,21 +83,17 @@ void TrackerParameterView::getNotified() { TrackerParameter *parameter = qobject_cast<TrackerParameter *>(getModel()); - int val = parameter->getBinarizationThreshold(); - _ui->lineEdit_2_binThresh->setValue(val); + _ui->lineEdit_7_learningRate->setValue(parameter->getLearningRate()); - val = parameter->getSizeErode(); - _ui->lineEdit_3_SizeErode->setValue(val); + if (_binThres) { + _binThres->setValue(parameter->getBinarizationThreshold()); + } - val = parameter->getSizeDilate(); - _ui->lineEdit_4_SizeDilate->setValue(val); + _ui->lineEdit_3_SizeErode->setValue(parameter->getSizeErode()); - double dval = parameter->getLearningRate(); - _ui->lineEdit_7_learningRate->setValue(dval); + _ui->lineEdit_4_SizeDilate->setValue(parameter->getSizeDilate()); - val = parameter->getMinBlobSize(); - _ui->lineEdit_8_MinBlob->setValue(val); + _ui->lineEdit_8_MinBlob->setValue(parameter->getMinBlobSize()); - val = parameter->getMaxBlobSize(); - _ui->lineEdit_9MaxBlob->setValue(val); + _ui->lineEdit_9MaxBlob->setValue(parameter->getMaxBlobSize()); } diff --git a/Src/View/TrackerParameterView.h b/Src/View/TrackerParameterView.h index cc3b97cd301d90334d9170631ffa61cd1391b2a1..6412b0b835c93c7279b7a35433ed5c72f29c68ea 100644 --- a/Src/View/TrackerParameterView.h +++ b/Src/View/TrackerParameterView.h @@ -4,6 +4,8 @@ #include "Interfaces/IView/IViewWidget.h" #include "../Model/TrackerParameter.h" +#include <QSpinBox> + namespace Ui { class TrackerParameterView; } @@ -29,6 +31,11 @@ public: private: Ui::TrackerParameterView *_ui; + QSpinBox* _binThres; + +private slots: + void initSubtractorSpecificUI(QString algorithm); + // IViewWidget interface public slots: diff --git a/Src/View/TrackerParameterView.ui b/Src/View/TrackerParameterView.ui index 86973da5270450b7421e93989c9a5629d6f51659..59e548d9321530da1bdd5aa8d41135d6d07b263d 100644 --- a/Src/View/TrackerParameterView.ui +++ b/Src/View/TrackerParameterView.ui @@ -7,7 +7,7 @@ <x>0</x> <y>0</y> <width>583</width> - <height>782</height> + <height>916</height> </rect> </property> <property name="sizePolicy"> @@ -67,7 +67,7 @@ <x>0</x> <y>0</y> <width>583</width> - <height>773</height> + <height>907</height> </rect> </property> <layout class="QHBoxLayout" name="horizontalLayout_4"> @@ -88,7 +88,7 @@ <item> <layout class="QFormLayout" name="formLayout"> <property name="fieldGrowthPolicy"> - <enum>QFormLayout::ExpandingFieldsGrow</enum> + <enum>QFormLayout::AllNonFixedFieldsGrow</enum> </property> <property name="formAlignment"> <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set> @@ -103,6 +103,27 @@ </property> </widget> </item> + <item row="0" column="1"> + <widget class="QComboBox" name="algorithmCB"> + <property name="minimumSize"> + <size> + <width>230</width> + <height>0</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>132</width> + <height>16777215</height> + </size> + </property> + <item> + <property name="text"> + <string>Custom</string> + </property> + </item> + </widget> + </item> <item row="1" column="0"> <widget class="QLabel" name="label_7"> <property name="text"> @@ -114,8 +135,8 @@ <widget class="QDoubleSpinBox" name="lineEdit_7_learningRate"> <property name="minimumSize"> <size> - <width>132</width> - <height>30</height> + <width>230</width> + <height>60</height> </size> </property> <property name="toolTip"> @@ -135,47 +156,16 @@ </property> </widget> </item> - <item row="2" column="0"> - <widget class="QLabel" name="label_2"> - <property name="text"> - <string>Binarization Threshold:</string> - </property> - </widget> - </item> - <item row="2" column="1"> - <widget class="QSpinBox" name="lineEdit_2_binThresh"> - <property name="minimumSize"> - <size> - <width>132</width> - <height>30</height> - </size> - </property> - <property name="toolTip"> - <string>Set the binarization threshold</string> - </property> - </widget> - </item> - <item row="0" column="1"> - <widget class="QComboBox" name="algorithmCB"> - <property name="minimumSize"> - <size> - <width>132</width> - <height>0</height> - </size> - </property> - <property name="maximumSize"> - <size> - <width>132</width> - <height>16777215</height> - </size> - </property> - <item> - <property name="text"> - <string>Custom</string> - </property> - </item> - </widget> - </item> + </layout> + </item> + <item> + <layout class="QFormLayout" name="algorithmSpecificParameterLayout"> + <property name="fieldGrowthPolicy"> + <enum>QFormLayout::AllNonFixedFieldsGrow</enum> + </property> + <property name="formAlignment"> + <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set> + </property> </layout> </item> <item> @@ -250,7 +240,7 @@ <property name="minimumSize"> <size> <width>132</width> - <height>30</height> + <height>60</height> </size> </property> <property name="toolTip"> @@ -270,7 +260,7 @@ <property name="minimumSize"> <size> <width>132</width> - <height>30</height> + <height>60</height> </size> </property> <property name="toolTip"> @@ -309,7 +299,7 @@ <property name="minimumSize"> <size> <width>132</width> - <height>30</height> + <height>60</height> </size> </property> <property name="toolTip"> @@ -328,8 +318,8 @@ <widget class="QSpinBox" name="lineEdit_9MaxBlob"> <property name="minimumSize"> <size> - <width>132</width> - <height>30</height> + <width>171</width> + <height>60</height> </size> </property> <property name="toolTip">