From d5bb7657039c45dee7be64f59989f14e3fde5695 Mon Sep 17 00:00:00 2001 From: Moritz Maxeiner <mm@ucw.sh> Date: Fri, 12 Nov 2021 18:35:29 +0100 Subject: [PATCH] misc --- CMakeLists.txt | 4 + Src/CMakeLists.txt | 2 + Src/Model/BioTrackerTrackingAlgorithm.cpp | 2 +- Src/Model/TrackerParameter.cpp | 2 + Src/Model/TrackerParameter.h | 7 ++ .../preprocessor/ImagePreProcessor.cpp | 22 ++++- .../preprocessor/ImagePreProcessor.h | 8 +- Src/View/TrackerParameterView.cpp | 57 +++++++++--- Src/View/TrackerParameterView.h | 7 ++ Src/View/TrackerParameterView.ui | 92 +++++++++---------- 10 files changed, 127 insertions(+), 76 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d9b45a..df196cb 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 8dc0aae..9b2e99a 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 483c498..52a3788 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 b156042..8363a86 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 02479e0..db3384f 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 3a52f7a..621d935 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 9df20d0..bb03f38 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 984c2c9..bf58593 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 cc3b97c..6412b0b 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 86973da..59e548d 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"> -- GitLab