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 5aad043f1e..8835a710fd 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.cpp +++ b/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.cpp @@ -1,183 +1,195 @@ /*=================================================================== 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 #include const std::string BoneSegmentation::VIEW_ID = "org.mitk.views.bonesegmentation"; void BoneSegmentation::SetFocus() { m_Controls.buttonPerformImageProcessing->setFocus(); } void BoneSegmentation::CreateQtPartControl(QWidget *parent) { m_BoneSegmentationPythonProcess = new QProcess(); 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_BoneSegmentationPythonProcess, SIGNAL(finished(int, QProcess::ExitStatus )),this, SLOT(DoBoneSegmentationProcessFinished())); connect(&m_Thread, SIGNAL(started()), this, SLOT(DoSaveTempNrrd())); connect(&m_Thread, SIGNAL(finished()), this, SLOT(DoStartBoneSegmentationProcess())); // connect(this, SIGNAL(FinishedSaveTempNrrd()), this, SLOT(DoStartBoneSegmentationProcess())); - // auto isImagePredicate = mitk::GetImageStatisticsImagePredicate(); + auto isImagePredicate = GetImagePredicate(); m_Controls.m_ImageSelector->SetDataStorage(GetDataStorage()); - // m_Controls.imageSelector->SetPredicate(isImagePredicate); + m_Controls.m_ImageSelector->SetPredicate(isImagePredicate); connect(this->m_Controls.m_ImageSelector, static_cast(&QComboBox::currentIndexChanged), this, &BoneSegmentation::OnImageSelectorChanged); } void BoneSegmentation::OnImageSelectorChanged() { auto selectedImageNode = m_Controls.m_ImageSelector->GetSelectedNode(); if (selectedImageNode != m_selectedImageNode) { m_selectedImageNode = selectedImageNode; if (m_selectedImageNode.IsNotNull()) { 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() { 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; } // a node itself is not very useful, we need its data item (the image) mitk::BaseData *data = m_selectedImageNode->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 (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() + "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_Controls.m_ImageSelector->setEnabled(false); m_Thread.start(); } void BoneSegmentation::DoStartBoneSegmentationProcess() { MITK_INFO << "[Start] DoStartBoneSegmentationProcess()"; QString fileName(":/segment.py"); QString pythonFileName = QString::fromStdString(mitk::IOUtil::GetTempPath() + "segment.py"); QFile::copy(fileName, pythonFileName); QString pretrainedNetResourcesPath(":/trained_bone_seg_unet.pth.tar"); - //QString pretrainedNetResourcesPath("/media/kleina/Data/ubuntu_dev/201904mitk/src/Plugins/org.mitk.gui.qt.bonesegmentation/resources/trained_bone_seg_unet.pth.tar"); QString pretrainedNetTmpPath = QString::fromStdString(mitk::IOUtil::GetTempPath() + "trained_bone_seg_unet.pth.tar"); bool test = QFile::copy(pretrainedNetResourcesPath, pretrainedNetTmpPath); if(test) MITK_WARN << "Bla"; QString programCall("python"); QStringList arguments = QStringList() << pythonFileName; MITK_INFO << programCall; m_BoneSegmentationPythonProcess->start(programCall, arguments); MITK_INFO << "[END] DoStartBoneSegmentationProcess()"; } void BoneSegmentation::DoBoneSegmentationProcessFinished() { 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_seg_" + m_selectedImageNode->GetName()); } catch(...) { MITK_WARN << "Error loading output file. Maybe it does not exist." ; } std::string temp_CT = mitk::IOUtil::GetTempPath() + "temp_CT_1.nrrd"; 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_output.c_str()) != 0) MITK_WARN << "Error deleting file" ; else MITK_INFO << "File successfully deleted" ; m_Controls.labelWarning->setVisible(false); m_Controls.m_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 3b3155aef1..99a84c8206 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.h +++ b/Plugins/org.mitk.gui.qt.bonesegmentation/src/internal/BoneSegmentation.h @@ -1,70 +1,79 @@ /*=================================================================== 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(); 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; QProcess* m_BoneSegmentationPythonProcess; QThread m_Thread; }; #endif // BoneSegmentation_h