diff --git a/Plugins/org.mitk.gui.qt.bonesegmentationrework/resources/trained_bone_seg_unet.pth.tar.REMOVED.git-id b/Plugins/org.mitk.gui.qt.bonesegmentationrework/resources/trained_bone_seg_unet.pth.tar.REMOVED.git-id new file mode 100644 index 0000000000..6986b0cc66 --- /dev/null +++ b/Plugins/org.mitk.gui.qt.bonesegmentationrework/resources/trained_bone_seg_unet.pth.tar.REMOVED.git-id @@ -0,0 +1 @@ +d14f304ddc4f7a454749dcdd1022af31e21bb630 \ No newline at end of file diff --git a/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationRework.cpp b/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationRework.cpp index 1e991cb46c..4de7cfea9c 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationRework.cpp +++ b/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationRework.cpp @@ -1,244 +1,246 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ // Blueberry #include #include // Qmitk #include "BoneSegmentationRework.h" // Qt #include #include #include // mitk image #include #include #include "internal/org_mitk_gui_qt_bonesegmentationrework_Activator.h" #include #include #include #include #include #include #include #include "ImageTransferPython.h" const std::string BoneSegmentationRework::VIEW_ID = "org.mitk.views.bonesegmentationrework"; BoneSegmentationRework::~BoneSegmentationRework() { if (m_ProcessBoneSegmentationPython.isRunning()) { m_ProcessBoneSegmentationPython.quit(); } if (Py_IsInitialized()) { if (PyGILState_Check() == 1) { PyGILState_Release(m_GState); } try { - //PyErr_Clear(); + // PyErr_Clear(); Py_FinalizeEx(); } catch (...) { MITK_ERROR << "Something went wrong in finalization"; } } } void BoneSegmentationRework::SetFocus() { m_Controls.buttonPerformImageProcessing->setFocus(); } void BoneSegmentationRework::CreateQtPartControl(QWidget *parent) { qRegisterMetaType(); m_Controls.setupUi(parent); m_Controls.buttonPerformImageProcessing->setText("Start Bone Segmentation"); m_Controls.buttonPerformImageProcessing->setEnabled(false); m_Controls.m_ImageSelector->SetDataStorage(GetDataStorage()); m_Controls.m_ImageSelector->SetPredicate(GetImagePredicate()); m_Controls.labelLoadTrainedNet->setText("Please load a trained network!"); - m_Controls.m_CancelButton->setEnabled(false); // Segmentation Thread m_Worker = new BoneSegmentationWorker; m_Worker->moveToThread(&m_ProcessBoneSegmentationPython); connect(&m_ProcessBoneSegmentationPython, &QThread::finished, m_Worker, &QObject::deleteLater); connect(this, &BoneSegmentationRework::Operate, m_Worker, &BoneSegmentationWorker::doWork); connect( m_Worker, &BoneSegmentationWorker::resultReady, this, &BoneSegmentationRework::DoBoneSegmentationProcessFinished); connect(m_Worker, &BoneSegmentationWorker::segmentationProcessCancelled, this, &BoneSegmentationRework::DoBoneSegmentationProcessFailed); // Connects connect( m_Controls.buttonPerformImageProcessing, &QPushButton::clicked, this, &BoneSegmentationRework::DoImageProcessing); connect(m_Controls.m_ButtonLoadTrainedNet, &QPushButton::clicked, this, &BoneSegmentationRework::DoLoadTrainedNet); + connect(m_Controls.m_ButtonLoadDefaultNet, &QPushButton::clicked, this, &BoneSegmentationRework::DoLoadDefaultNet); connect(this->m_Controls.m_ImageSelector, static_cast(&QComboBox::currentIndexChanged), this, &BoneSegmentationRework::OnImageSelectorChanged); - connect(m_Controls.m_CancelButton, &QPushButton::clicked, this, &BoneSegmentationRework::OnCancel); } void BoneSegmentationRework::OnImageSelectorChanged() { auto selectedImageNode = m_Controls.m_ImageSelector->GetSelectedNode(); if (selectedImageNode != m_selectedImageNode) { m_selectedImageNode = selectedImageNode; if (m_selectedImageNode.IsNotNull()) { m_Controls.labelWarning->setVisible(false); if (m_TrainedNet != "") { m_Controls.buttonPerformImageProcessing->setEnabled(true); } return; } m_Controls.labelWarning->setText("Please select an image!"); m_Controls.labelWarning->setVisible(true); m_Controls.buttonPerformImageProcessing->setEnabled(false); } } void BoneSegmentationRework::DoLoadTrainedNet() { QString tempPath = QString::fromStdString(mitk::IOUtil::GetTempPathA()); QString pretrainedNetResourcesPath = QFileDialog::getOpenFileName(nullptr, tr("Open File"), tempPath, tr("Images (*.pth.tar)")); if (pretrainedNetResourcesPath != "") { m_TrainedNet = pretrainedNetResourcesPath.toStdString(); } if (m_TrainedNet != "") { m_Controls.labelLoadTrainedNet->setText("Network loaded!"); if (m_selectedImageNode.IsNotNull()) { m_Controls.buttonPerformImageProcessing->setEnabled(true); } } } +void BoneSegmentationRework::DoLoadDefaultNet() +{ + std::string fileName = mitk::StandardFileLocations::GetInstance()->FindFile( + m_DefaultNetFileName.c_str(), "Plugins/org.mitk.gui.qt.bonesegmentationrework/resources"); + std::replace(fileName.begin(), fileName.end(), '\\', '/'); + if (fileName != "") + { + m_TrainedNet = fileName; + m_Controls.labelLoadTrainedNet->setText("Network loaded!"); + if (m_selectedImageNode.IsNotNull()) + { + m_Controls.buttonPerformImageProcessing->setEnabled(true); + } + } + MITK_INFO << m_TrainedNet; +} + void BoneSegmentationRework::DoImageProcessing() { - m_Controls.m_CancelButton->setEnabled(true); m_Controls.labelWarning->setVisible(true); m_Controls.labelWarning->setText("This might take a while.\nDepending ob your machine up to 20 minutes or more."); m_Controls.buttonPerformImageProcessing->setEnabled(false); m_Controls.m_ButtonLoadTrainedNet->setEnabled(false); + m_Controls.m_ButtonLoadDefaultNet->setEnabled(false); m_Controls.m_ImageSelector->setEnabled(false); this->DoStartBoneSegmentationProcess(); } void BoneSegmentationRework::DoStartBoneSegmentationProcess() { MITK_INFO << "[Start] DoStartBoneSegmentationProcess()"; m_ProcessBoneSegmentationPython.start(); emit Operate(QString::fromStdString(m_PythonFileName), QString::fromStdString(m_TrainedNet), dynamic_cast(m_selectedImageNode->GetData())); MITK_INFO << "[END] DoStartBoneSegmentationProcess()"; } void BoneSegmentationRework::DoLoadOutputImage(mitk::Image::Pointer outputImage) { try { mitk::DataNode::Pointer outputNode = mitk::DataNode::New(); outputNode->SetName("Bone_seg_" + m_selectedImageNode->GetName()); outputNode->SetData(outputImage); GetDataStorage().GetPointer()->Add(outputNode, m_selectedImageNode); } catch (...) { MITK_WARN << "Error loading output file. Maybe it does not exist."; } } void BoneSegmentationRework::ResetUI() { - m_Controls.m_CancelButton->setEnabled(false); m_Controls.buttonPerformImageProcessing->setText("Start Bone Segmentation"); m_Controls.buttonPerformImageProcessing->setEnabled(true); m_Controls.m_ButtonLoadTrainedNet->setEnabled(true); + m_Controls.m_ButtonLoadDefaultNet->setEnabled(true); m_Controls.labelWarning->setVisible(false); m_Controls.m_ImageSelector->setEnabled(true); } void BoneSegmentationRework::DoBoneSegmentationProcessFinished(mitk::Image::Pointer outputImage) { mitk::Image::Pointer image = outputImage; MITK_INFO << "Ended calculation thread."; this->DoLoadOutputImage(image); this->ResetUI(); } void BoneSegmentationRework::DoBoneSegmentationProcessFailed() { MITK_INFO << "Failed during calculation thread."; QMessageBox::warning(nullptr, "Error in segmentation", "There was an error in the segmentation process. No resulting segmentation can be loaded."); this->ResetUI(); } -void BoneSegmentationRework::OnCancel() -{ - if (m_ProcessBoneSegmentationPython.isRunning()) - { - m_ProcessBoneSegmentationPython.terminate(); - m_Controls.m_CancelButton->setEnabled(false); - m_Controls.buttonPerformImageProcessing->setText("Start Bone Segmentation"); - m_Controls.buttonPerformImageProcessing->setEnabled(true); - m_Controls.m_ButtonLoadTrainedNet->setEnabled(true); - m_Controls.labelWarning->setVisible(false); - m_Controls.m_ImageSelector->setEnabled(true); - } -} - mitk::NodePredicateBase::Pointer BoneSegmentationRework::GetImagePredicate() { auto isImage = mitk::NodePredicateDataType::New("Image"); auto hasBinaryProperty = mitk::NodePredicateProperty::New("binary", mitk::BoolProperty::New(true)); auto isNotBinary = mitk::NodePredicateNot::New(hasBinaryProperty); auto isNotBinaryImage = mitk::NodePredicateAnd::New(isImage, isNotBinary); auto hasHelperObjectProperty = mitk::NodePredicateProperty::New("helper object", nullptr); auto isNoHelperObject = mitk::NodePredicateNot::New(hasHelperObjectProperty); auto isNoHelperObjectPredicate = isNoHelperObject.GetPointer(); auto isImageForImageStatistics = mitk::NodePredicateAnd::New(isNotBinaryImage, isNoHelperObjectPredicate); return isImageForImageStatistics.GetPointer(); } diff --git a/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationRework.h b/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationRework.h index ab618f1993..7141426aba 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationRework.h +++ b/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationRework.h @@ -1,87 +1,88 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef BoneSegmentationRework_h #define BoneSegmentationRework_h #include #include #include "BoneSegmentationWorker.h" #include "ui_BoneSegmentationReworkControls.h" #include #include #include #include #include #include #include #include #include #include #include /** \brief BoneSegmentationRework \warning The BoneSegmentationRework plugin provides an automatic bone segmentation for CT images based on deep learning. At this moment, only CT images can be segmented. The segmentation is done on 2D axial slices. The network was trained using data from the multiple myeloma project. It was trained on a very limited amount of training data and needs to be tested in "real world scenarios". \sa QmitkAbstractView \ingroup ${plugin_target}_internal */ class BoneSegmentationRework : public QmitkAbstractView { // this is needed for all Qt objects that should have a Qt meta-object // (everything that derives from QObject and wants to have signal/slots) Q_OBJECT public: static const std::string VIEW_ID; void ResetUI(); ~BoneSegmentationRework(); signals: void Operate(QString pythonFileName, QString trainedNet, mitk::Image::Pointer inputImage); protected slots: /// \brief Called when the user clicks the GUI button void OnImageSelectorChanged(); void DoImageProcessing(); void DoStartBoneSegmentationProcess(); void DoBoneSegmentationProcessFinished(mitk::Image::Pointer outputImage); void DoBoneSegmentationProcessFailed(); void DoLoadTrainedNet(); + void DoLoadDefaultNet(); void DoLoadOutputImage(mitk::Image::Pointer outputImage); - void OnCancel(); protected: virtual void CreateQtPartControl(QWidget *parent) override; virtual void SetFocus() override; // Predicate helper mitk::NodePredicateBase::Pointer GetImagePredicate(); Ui::BoneSegmentationReworkControls m_Controls; mitk::DataNode::Pointer m_selectedImageNode = nullptr; std::string m_TrainedNet; const std::string m_PythonFileName="segment.py"; + const std::string m_DefaultNetFileName = "trained_bone_seg_unet.pth.tar"; QThread m_ProcessBoneSegmentationPython; PyGILState_STATE m_GState; BoneSegmentationWorker* m_Worker; }; #endif // BoneSegmentationRework_h diff --git a/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationReworkControls.ui b/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationReworkControls.ui index f0562b201d..75910ead18 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationReworkControls.ui +++ b/Plugins/org.mitk.gui.qt.bonesegmentationrework/src/internal/BoneSegmentationReworkControls.ui @@ -1,95 +1,96 @@ BoneSegmentationReworkControls 0 0 - 222 - 186 + 285 + 205 0 0 QmitkTemplate - Please load a trained network! + <html><head/><body><p><span style=" color:#ff0000;">Please load a trained network!</span></p></body></html> - - - Load trained network - - + + + + + Load trained network + + + + + + + Load default trained network + + + + QLabel { color: rgb(255, 0, 0) } Please select an image! Do image processing Do Something - - - Cancel - - - - - + Qt::Vertical - - QSizePolicy::Expanding - 20 - 220 + 40 QmitkDataStorageComboBoxWithSelectNone QComboBox
QmitkDataStorageComboBoxWithSelectNone.h