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 ce8b8d9320..84db3968ec 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.cpp +++ b/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.cpp @@ -1,205 +1,178 @@ /*=================================================================== 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 // mitk image #include #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_process = new QProcess(); // create GUI widgets from the Qt Designer's .ui file m_Controls.setupUi(parent); + m_Controls.buttonPerformImageProcessing->setText("Start Bone Segmentation"); + m_Controls.buttonPerformImageProcessing->setEnabled(false); connect(m_Controls.buttonPerformImageProcessing, &QPushButton::clicked, this, &BoneSegmentation::DoImageProcessing); connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus )),this, SLOT(doSomethingWhenItsDone())); connect(&m_Thread, SIGNAL(started()), this, SLOT(DoSaveTempNrrd())); connect(&m_Thread, SIGNAL(finished()), this, SLOT(DoStartBoneSegmentationProcess())); // connect(this, SIGNAL(FinishedSaveTempNrrd()), this, SLOT(DoStartBoneSegmentationProcess())); } void BoneSegmentation::OnSelectionChanged(berry::IWorkbenchPart::Pointer /*source*/, const QList &nodes) { // iterate all selected objects, adjust warning visibility foreach (mitk::DataNode::Pointer node, nodes) { if (node.IsNotNull() && dynamic_cast(node->GetData())) { m_Controls.labelWarning->setVisible(false); 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 BoneSegmentation::DoSaveTempNrrd() { QList nodes = this->GetDataManagerSelection(); if (nodes.empty()) return; mitk::DataNode *node = nodes.front(); if (!node) { // Nothing selected. Inform the user and return QMessageBox::information(nullptr, "Template", "Please load and select an image before starting image processing."); return; } // a node itself is not very useful, we need its data item (the image) mitk::BaseData *data = node->GetData(); if (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) { std::stringstream message; std::string name; message << "Performing image processing for image "; if (node->GetName(name)) { // a property called "name" was found for this DataNode message << "'" << name << "'"; } message << "."; MITK_INFO << message.str(); MITK_INFO << "[START] StartPythonProcess()"; - //m_Controls->m_StartButton->setText("Segmentation running... This can take a few minutes."); - //m_Controls->m_StartButton->setEnabled(false); std::string file_name = mitk::IOUtil::GetTempPath() + "temp_CT_1.nrrd"; mitk::IOUtil::Save(mitk_image, file_name); } } m_Thread.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.buttonPerformImageProcessing->setEnabled(false); m_Thread.start(); } void BoneSegmentation::DoStartBoneSegmentationProcess() { QString fileName(":/segment.py"); QString pythonFileName = QString::fromStdString(mitk::IOUtil::GetTempPath() + "segment.py"); MITK_INFO << "[Start] DoStartBoneSegmentationProcess()"; QFile::copy(fileName, pythonFileName); QString pretrainedNetResources(":/trained_bone_seg_unet.pth.tar"); QString pretrainedNet = QString::fromStdString(mitk::IOUtil::GetTempPath() + "trained_bone_seg_unet.pth.tar"); QFile::copy(pretrainedNetResources, pretrainedNet); QString programCall("python"); QStringList arguments = QStringList() << pythonFileName; //QStringList arguments = QStringList() << "/media/kleina/Data/ubuntu_dev/201904mitk/src/Plugins/org.mitk.gui.qt.bonesegmentation/resources/segment.py"; MITK_INFO << programCall; m_process->start(programCall, arguments); MITK_INFO << "[END] DoStartBoneSegmentationProcess()"; } void BoneSegmentation::doSomethingWhenItsDone() { MITK_INFO << "Ended calculation thread."; m_Controls.buttonPerformImageProcessing->setText("Start Bone Segmentation"); m_Controls.buttonPerformImageProcessing->setEnabled(true); try { mitk::IOUtil::Load(mitk::IOUtil::GetTempPath() + "temp_CT_output.nrrd", *GetDataStorage()); + GetDataStorage().GetPointer()->GetNamedNode("temp_CT_output")->SetName("Bone_segmentation"); } catch(...) { MITK_WARN << "Error loading file. Maybe it does not exist." ; } std::string temp_CT = mitk::IOUtil::GetTempPath() + "temp_CT_1.nrrd"; - if(remove("temp_CT_1.nrrd") != 0) + if(remove(temp_CT.c_str()) != 0) MITK_WARN << "Error deleting file" ; else MITK_INFO << "File successfully deleted" ; std::string temp_output = mitk::IOUtil::GetTempPath() + "temp_CT_output.nrrd"; - if(remove("temp_CT_output.nrrd") != 0) + if(remove(temp_output.c_str()) != 0) MITK_WARN << "Error deleting file" ; else MITK_INFO << "File successfully deleted" ; -} - -std::string BoneSegmentation::GetPythonFile() //std::string filename) -{ - std::string out = ""; - out = mitk::IOUtil::GetTempPath(); - - return out; - /*std::string exec_dir = QCoreApplication::applicationDirPath().toStdString(); - for (auto dir : mitk::bet::relative_search_dirs) - { - if ( ist::FileExists( ist::GetCurrentWorkingDirectory() + dir + filename) ) - { - out = ist::GetCurrentWorkingDirectory() + dir + filename; - return out; - } - if ( ist::FileExists( exec_dir + dir + filename) ) - { - out = exec_dir + dir + filename; - return out; - } - } - for (auto dir : mitk::bet::absolute_search_dirs) - { - if ( ist::FileExists( dir + filename) ) - { - out = dir + filename; - return out; - } - } - MITK_WARN << out; - return out;*/ + m_Controls.labelWarning->setVisible(false); } 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 329772c168..675cd98f27 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.h +++ b/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.h @@ -1,72 +1,70 @@ /*=================================================================== 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 /** \brief BoneSegmentation \warning This class is not yet documented. Use "git blame" and ask the author to provide basic documentation. \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 DoSaveTempNrrd(); void DoImageProcessing(); void DoStartBoneSegmentationProcess(); void doSomethingWhenItsDone(); protected: virtual void CreateQtPartControl(QWidget *parent) override; virtual void SetFocus() override; /// \brief called by QmitkFunctionality when DataManager's selection has changed virtual void OnSelectionChanged(berry::IWorkbenchPart::Pointer source, const QList &nodes) override; - std::string GetPythonFile(); //std::string filename); - Ui::BoneSegmentationControls m_Controls; QProcess* m_process; QThread m_Thread; }; #endif // BoneSegmentation_h