diff --git a/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.cpp b/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.cpp index b64461fe8a..24c29fba73 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.cpp +++ b/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.cpp @@ -1,233 +1,234 @@ /*=================================================================== 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 "BoneSegmentation.h" // Qt #include #include #include // mitk image #include #include const std::string BoneSegmentation::VIEW_ID = "org.mitk.views.bonesegmentation"; void BoneSegmentation::SetFocus() { m_Controls.buttonPerformImageProcessing->setFocus(); } void BoneSegmentation::CreateQtPartControl(QWidget *parent) { m_Controls.setupUi(parent); m_ProcessBoneSegmentationPython = new QProcess(); 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.imageSelector->SetDataStorage(GetDataStorage()); + m_Controls.imageSelector->SetPredicate(GetImagePredicate()); // Connects connect(m_Controls.buttonPerformImageProcessing, &QPushButton::clicked, this, &BoneSegmentation::DoImageProcessing); - connect(m_Controls.m_ButtonLoadTrainedNet, &QPushButton::clicked, this, &BoneSegmentation::DoLoadTrainedNet); - connect(m_ProcessBoneSegmentationPython, SIGNAL(finished(int, QProcess::ExitStatus )),this, SLOT(DoBoneSegmentationProcessFinished())); - connect(&m_ThreadSaveTempNrrdImage, SIGNAL(started()), this, SLOT(DoSaveTempNrrd())); - connect(&m_ThreadSaveTempNrrdImage, SIGNAL(finished()), this, SLOT(DoStartBoneSegmentationProcess())); - connect(this->m_Controls.m_ImageSelector, - static_cast(&QComboBox::currentIndexChanged), - this, - &BoneSegmentation::OnImageSelectorChanged); - - if(QFile::exists(QString::fromStdString(mitk::IOUtil::GetTempPath() + "trained_bone_seg_unet.pth.tar"))) + connect(m_Controls.buttonLoadTrainedNet, &QPushButton::clicked, this, &BoneSegmentation::DoLoadTrainedNet); + connect(m_ProcessBoneSegmentationPython, static_cast(&QProcess::finished), + this, &BoneSegmentation::DoBoneSegmentationProcessFinished); + connect(&m_ThreadSaveTempNrrdImage, &QThread::started, this, &BoneSegmentation::DoSaveTempNrrd); + connect(&m_ThreadSaveTempNrrdImage, &QThread::finished, this, &BoneSegmentation::DoStartBoneSegmentationProcess); + connect(m_Controls.imageSelector, static_cast(&QComboBox::currentIndexChanged), + this, &BoneSegmentation::OnImageSelectorChanged); + + if (QFile::exists(QString::fromStdString(mitk::IOUtil::GetTempPath() + "trained_bone_seg_unet.pth.tar"))) { - m_Controls.labelLoadTrainedNet->setText("Network loaded!"); + m_Controls.labelLoadTrainedNet->setText("Network loaded."); } else { - m_Controls.labelLoadTrainedNet->setText("Please load a trained network!"); + m_Controls.labelLoadTrainedNet->setText("Please load a trained network."); } + + OnImageSelectorChanged(); } void BoneSegmentation::OnImageSelectorChanged() { - auto selectedImageNode = m_Controls.m_ImageSelector->GetSelectedNode(); - if (selectedImageNode != m_selectedImageNode) + auto selectedImageNode = m_Controls.imageSelector->GetSelectedNode(); + if (selectedImageNode != m_SelectedImageNode) { - m_selectedImageNode = selectedImageNode; - if (m_selectedImageNode.IsNotNull()) + m_SelectedImageNode = selectedImageNode; + if (m_SelectedImageNode.IsNotNull()) { - m_Controls.labelWarning->setVisible(false); - m_Controls.buttonPerformImageProcessing->setEnabled(true); - return; + m_Controls.labelWarning->setVisible(false); + m_Controls.buttonPerformImageProcessing->setEnabled(true); + return; } - m_Controls.labelWarning->setText("Please select an image!"); + m_Controls.labelWarning->setText("Please select an image."); m_Controls.labelWarning->setVisible(true); m_Controls.buttonPerformImageProcessing->setEnabled(false); } } void BoneSegmentation::DoLoadTrainedNet() { - QString pretrainedNetResourcesPath = QFileDialog::getOpenFileName(nullptr, tr("Open File"), - mitk::IOUtil::GetTempPath(), - tr("Images (*.pth.tar)")); + QString pretrainedNetResourcesPath = QFileDialog::getOpenFileName(nullptr, tr("Open File"), + QString::fromStdString(mitk::IOUtil::GetTempPath()), + tr("Images (*.pth.tar)")); - if(QFile::exists(QString::fromStdString(mitk::IOUtil::GetTempPath() + "trained_bone_seg_unet.pth.tar"))) - { - QFile::remove(QString::fromStdString(mitk::IOUtil::GetTempPath() + "trained_bone_seg_unet.pth.tar")); - } - QString pretrainedNetTmpPath = QString::fromStdString(mitk::IOUtil::GetTempPath() + "trained_bone_seg_unet.pth.tar"); - QFile::copy(pretrainedNetResourcesPath, pretrainedNetTmpPath); - m_Controls.labelLoadTrainedNet->setText("Network loaded!"); + if (QFile::exists(QString::fromStdString(mitk::IOUtil::GetTempPath() + "trained_bone_seg_unet.pth.tar"))) + { + QFile::remove(QString::fromStdString(mitk::IOUtil::GetTempPath() + "trained_bone_seg_unet.pth.tar")); + } + QString pretrainedNetTmpPath = QString::fromStdString(mitk::IOUtil::GetTempPath() + "trained_bone_seg_unet.pth.tar"); + QFile::copy(pretrainedNetResourcesPath, pretrainedNetTmpPath); + m_Controls.labelLoadTrainedNet->setText("Network loaded."); } void BoneSegmentation::DoSaveTempNrrd() { - if (!m_selectedImageNode) + if (!m_SelectedImageNode) { // Nothing selected. Inform the user and return QMessageBox::information(nullptr, "Template", "Please load and select an image before starting image processing."); return; } - mitk::BaseData *data = m_selectedImageNode->GetData(); - if (data) + mitk::BaseData *data = m_SelectedImageNode->GetData(); + if (nullptr != data) { // test if this data item is an image or not (could also be a surface or something totally different) - mitk::Image *mitk_image = dynamic_cast(data); - if (mitk_image) + mitk::Image *image = dynamic_cast(data); + if (nullptr != image) { std::stringstream message; std::string name; message << "Performing image processing for image "; - if (m_selectedImageNode->GetName(name)) + if (m_SelectedImageNode->GetName(name)) { // a property called "name" was found for this DataNode message << "'" << name << "'"; } message << "."; MITK_INFO << message.str(); MITK_INFO << "[START] StartPythonProcess()"; - std::string file_name = mitk::IOUtil::GetTempPath() + m_TempNrrdFile; - if(QFile::exists(QString::fromStdString(file_name))) + std::string filename = mitk::IOUtil::GetTempPath() + m_TempNrrdFile; + if (QFile::exists(QString::fromStdString(filename))) { - QFile::remove(QString::fromStdString(file_name)); + QFile::remove(QString::fromStdString(filename)); } - std::string file_name_temp_seg = mitk::IOUtil::GetTempPath() + m_TempBoneSegFile + ".nrrd"; - if(QFile::exists(QString::fromStdString(file_name_temp_seg))) + std::string filenameTempSeg = mitk::IOUtil::GetTempPath() + m_TempBoneSegFile + ".nrrd"; + if (QFile::exists(QString::fromStdString(filenameTempSeg))) { - QFile::remove(QString::fromStdString(file_name_temp_seg)); + QFile::remove(QString::fromStdString(filenameTempSeg)); } - mitk::IOUtil::Save(mitk_image, file_name); - MITK_INFO << "Temporary NRRD image saved!"; + mitk::IOUtil::Save(image, filename); + MITK_INFO << "Temporary NRRD image saved."; } } m_ThreadSaveTempNrrdImage.quit(); } void BoneSegmentation::DoImageProcessing() { 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.labelWarning->setText("This might take a while."); m_Controls.buttonPerformImageProcessing->setEnabled(false); - m_Controls.m_ButtonLoadTrainedNet->setEnabled(false); - m_Controls.m_ImageSelector->setEnabled(false); + m_Controls.buttonLoadTrainedNet->setEnabled(false); + m_Controls.imageSelector->setEnabled(false); m_ThreadSaveTempNrrdImage.start(); } void BoneSegmentation::DoStartBoneSegmentationProcess() { MITK_INFO << "[Start] DoStartBoneSegmentationProcess()"; // Copy python file and pretrained net to /tmp/ folder QString fileName(":/" + m_PythonFileName); QString pythonFileName = QString::fromStdString(mitk::IOUtil::GetTempPath()) + m_PythonFileName; QFile::copy(fileName, pythonFileName); QString programCall("python"); - QStringList arguments = QStringList() << pythonFileName; + QStringList arguments = QStringList() << pythonFileName; MITK_INFO << programCall; m_ProcessBoneSegmentationPython->start(programCall, arguments); MITK_INFO << "[END] DoStartBoneSegmentationProcess()"; } void BoneSegmentation::DoBoneSegmentationProcessFinished() { MITK_INFO << "Ended calculation thread."; try { mitk::IOUtil::Load(mitk::IOUtil::GetTempPath() + m_TempBoneSegFile + ".nrrd", *GetDataStorage()); - GetDataStorage().GetPointer()->GetNamedNode(m_TempBoneSegFile)->SetName("Bone_seg_" + m_selectedImageNode->GetName()); + GetDataStorage().GetPointer()->GetNamedNode(m_TempBoneSegFile)->SetName("Bone_seg_" + m_SelectedImageNode->GetName()); } catch(...) { MITK_WARN << "Error loading output file. Maybe it does not exist." ; } - std::string temp_CT = mitk::IOUtil::GetTempPath() + m_TempNrrdFile + ".nrrd"; + std::string temp_CT = mitk::IOUtil::GetTempPath() + m_TempNrrdFile; if(remove(temp_CT.c_str()) != 0) { MITK_WARN << "Error deleting file: " + m_TempNrrdFile; } else { MITK_INFO << "File successfully deleted: " + m_TempNrrdFile; } - std::string temp_output = mitk::IOUtil::GetTempPath() + m_TempBoneSegFile; + std::string temp_output = mitk::IOUtil::GetTempPath() + m_TempBoneSegFile + ".nrrd"; if(remove(temp_output.c_str()) != 0) { - MITK_WARN << "Error deleting file: " + m_TempBoneSegFile; + MITK_WARN << "Error deleting file: " + m_TempBoneSegFile + ".nrrd"; } else { - MITK_INFO << "File successfully deleted: " + m_TempBoneSegFile; + MITK_INFO << "File successfully deleted: " + m_TempBoneSegFile + ".nrrd"; } m_Controls.buttonPerformImageProcessing->setText("Start Bone Segmentation"); m_Controls.buttonPerformImageProcessing->setEnabled(true); - m_Controls.m_ButtonLoadTrainedNet->setEnabled(true); + m_Controls.buttonLoadTrainedNet->setEnabled(true); m_Controls.labelWarning->setVisible(false); - m_Controls.m_ImageSelector->setEnabled(true); + m_Controls.imageSelector->setEnabled(true); } mitk::NodePredicateBase::Pointer BoneSegmentation::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.bonesegmentation/src/internal/BoneSegmentation.h b/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.h index 78742850bb..fe71d20437 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.h +++ b/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.h @@ -1,85 +1,83 @@ /*=================================================================== 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. ===================================================================*/ - #ifndef BoneSegmentation_h #define BoneSegmentation_h #include #include #include "ui_BoneSegmentationControls.h" #include #include #include #include #include #include #include /** \brief BoneSegmentation \warning The BoneSegmentation 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 BoneSegmentation : 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; signals: void FinishedSaveTempNrrd(); protected slots: /// \brief Called when the user clicks the GUI button void OnImageSelectorChanged(); void DoSaveTempNrrd(); void DoImageProcessing(); void DoStartBoneSegmentationProcess(); void DoBoneSegmentationProcessFinished(); - void DoLoadTrainedNet(); protected: virtual void CreateQtPartControl(QWidget *parent) override; virtual void SetFocus() override; // Predicate helper mitk::NodePredicateBase::Pointer GetImagePredicate(); Ui::BoneSegmentationControls m_Controls; - mitk::DataNode::ConstPointer m_selectedImageNode = nullptr; + mitk::DataNode::Pointer m_SelectedImageNode = nullptr; QProcess* m_ProcessBoneSegmentationPython; QThread m_ThreadSaveTempNrrdImage; - const QString m_PythonFileName="segment.py"; - const std::string m_TempNrrdFile="temp_CT.nrrd"; - const std::string m_TempBoneSegFile="temp_bone_seg"; + const QString m_PythonFileName = "segment.py"; + const std::string m_TempNrrdFile = "temp_CT.nrrd"; + const std::string m_TempBoneSegFile = "temp_bone_seg"; }; #endif // BoneSegmentation_h diff --git a/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentationControls.ui b/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentationControls.ui index 93c6177b65..6479bed831 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentationControls.ui +++ b/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentationControls.ui @@ -1,88 +1,88 @@ BoneSegmentationControls 0 0 222 161 0 0 QmitkTemplate Please load a trained network! - + Load trained network QLabel { color: rgb(255, 0, 0) } Please select an image! - + Do image processing Do Something Qt::Vertical QSizePolicy::Expanding 20 220 QmitkDataStorageComboBoxWithSelectNone QComboBox
QmitkDataStorageComboBoxWithSelectNone.h