diff --git a/Plugins/org.mitk.gui.qt.diffusionimaging.preprocessing/src/internal/QmitkBrainExtractionView.cpp b/Plugins/org.mitk.gui.qt.diffusionimaging.preprocessing/src/internal/QmitkBrainExtractionView.cpp index 0f3b2340ac..a0f5da805b 100644 --- a/Plugins/org.mitk.gui.qt.diffusionimaging.preprocessing/src/internal/QmitkBrainExtractionView.cpp +++ b/Plugins/org.mitk.gui.qt.diffusionimaging.preprocessing/src/internal/QmitkBrainExtractionView.cpp @@ -1,203 +1,229 @@ /*=================================================================== 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. ===================================================================*/ //misc #define _USE_MATH_DEFINES #include // Blueberry #include #include // Qmitk #include "QmitkBrainExtractionView.h" // MITK #include // Qt #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include #define _USE_MATH_DEFINES #include FileDownloader::FileDownloader(QObject *parent) : QObject(parent) { } void FileDownloader::download(QUrl url) { connect(&m_WebCtrl, SIGNAL (finished(QNetworkReply*)), SLOT (Downloaded(QNetworkReply*))); QNetworkRequest request(url); m_WebCtrl.get(request); } void FileDownloader::Downloaded(QNetworkReply *reply) { MITK_INFO << "FileDownloader::Downloaded TESTTEST"; QFile localFile("/home/neher/test_download.tar.gz"); if (!localFile.open(QIODevice::WriteOnly)) return; localFile.write(reply->readAll()); localFile.close(); delete reply; QMessageBox::information(nullptr, "FILE DOWNLOADED", "BLABL"); } FileDownloader::~FileDownloader() { } const std::string QmitkBrainExtractionView::VIEW_ID = "org.mitk.views.brainextraction"; QmitkBrainExtractionView::QmitkBrainExtractionView() : QmitkAbstractView() , m_Controls( 0 ) , m_DiffusionImage( nullptr ) { } // Destructor QmitkBrainExtractionView::~QmitkBrainExtractionView() { } void QmitkBrainExtractionView::CreateQtPartControl( QWidget *parent ) { // build up qt view, unless already done if ( !m_Controls ) { // create GUI widgets from the Qt Designer's .ui file m_Controls = new Ui::QmitkBrainExtractionViewControls; m_Controls->setupUi( parent ); connect( m_Controls->m_ImageBox, SIGNAL(currentIndexChanged(int)), this, SLOT(UpdateGUI()) ); connect( m_Controls->m_StartButton, SIGNAL(clicked()), this, SLOT(StartBrainExtraction()) ); this->m_Parent = parent; m_Controls->m_ImageBox->SetDataStorage(this->GetDataStorage()); mitk::NodePredicateDimension::Pointer dimPred = mitk::NodePredicateDimension::New(3); mitk::TNodePredicateDataType::Pointer isImagePredicate = mitk::TNodePredicateDataType::New(); - m_Controls->m_ImageBox->SetPredicate(mitk::NodePredicateAnd::New(isImagePredicate,dimPred)); +// m_Controls->m_ImageBox->SetPredicate(mitk::NodePredicateAnd::New(isImagePredicate,dimPred)); + m_Controls->m_ImageBox->SetPredicate(isImagePredicate); UpdateGUI(); std::string module_library_full_path = us::GetModuleContext()->GetModule()->GetLocation(); std::string library_file; itksys::SystemTools::SplitProgramPath(module_library_full_path, m_ModulePath, library_file); + m_ModulePath += "/BET"; } } void QmitkBrainExtractionView::OnSelectionChanged(berry::IWorkbenchPart::Pointer, const QList& ) { } void QmitkBrainExtractionView::UpdateGUI() { if (m_Controls->m_ImageBox->GetSelectedNode().IsNotNull()) m_Controls->m_StartButton->setEnabled(true); else m_Controls->m_StartButton->setEnabled(false); } void QmitkBrainExtractionView::SetFocus() { UpdateGUI(); m_Controls->m_StartButton->setFocus(); } void QmitkBrainExtractionView::StartBrainExtraction() { // FileDownloader dl; // dl.download(QUrl("http://mitk.org/download/releases/MITK-2012.06/MITK-2012.06.0-src.tar.gz")); mitk::DataNode::Pointer node = m_Controls->m_ImageBox->GetSelectedNode(); mitk::Image::Pointer mitk_image = dynamic_cast(node->GetData()); + bool missing_file = false; + std::string missing_file_string = ""; if ( !itksys::SystemTools::FileExists(m_ModulePath + "/brain_extraction_script.py") ) { - QMessageBox::warning(nullptr, "Error", ("Brain extraction script file missing:\n" + m_ModulePath + "/brain_extraction_script.py").c_str(), QMessageBox::Ok); - return; + missing_file_string += "Brain extraction script file missing:\n" + m_ModulePath + "/brain_extraction_script.py\n\n"; + missing_file = true; } if ( !itksys::SystemTools::FileExists(m_ModulePath + "/brain_extraction_model.model") ) { - QMessageBox::warning(nullptr, "Error", ("Brain extraction model file missing:\n" + m_ModulePath + "/brain_extraction_model.model").c_str(), QMessageBox::Ok); + missing_file_string += "Brain extraction model file missing:\n" + m_ModulePath + "/brain_extraction_model.model\n\n"; + missing_file = true; + } + + if ( !itksys::SystemTools::FileExists(m_ModulePath + "/basic_config_just_like_braintumor.py") ) + { + missing_file_string += "Config file missing:\n" + m_ModulePath + "/basic_config_just_like_braintumor.py\n\n"; + missing_file = true; + } + + if (missing_file) + { + QMessageBox::warning(nullptr, "Error", (missing_file_string).c_str(), QMessageBox::Ok); return; } try { us::ModuleContext* context = us::GetModuleContext(); us::ServiceReference m_PythonServiceRef = context->GetServiceReference(); mitk::IPythonService* m_PythonService = dynamic_cast ( context->GetService(m_PythonServiceRef) ); mitk::IPythonService::ForceLoadModule(); m_PythonService->Execute("import SimpleITK as sitk"); m_PythonService->Execute("import SimpleITK._SimpleITK as _SimpleITK"); m_PythonService->Execute("import numpy"); m_PythonService->CopyToPythonAsSimpleItkImage( mitk_image, "in_image"); - m_PythonService->Execute("model=\""+m_ModulePath+"/brain_extraction_model.model\""); + m_PythonService->Execute("model_file=\""+m_ModulePath+"/brain_extraction_model.model\""); + m_PythonService->Execute("config_file=\""+m_ModulePath+"/basic_config_just_like_braintumor.py\""); m_PythonService->ExecuteScript(m_ModulePath + "/brain_extraction_script.py"); { mitk::Image::Pointer image = m_PythonService->CopySimpleItkImageFromPython("brain_mask"); mitk::DataNode::Pointer corrected_node = mitk::DataNode::New(); corrected_node->SetData( image ); QString name(node->GetName().c_str()); name += "_BrainMask"; corrected_node->SetName(name.toStdString()); GetDataStorage()->Add(corrected_node, node); } { mitk::Image::Pointer image = m_PythonService->CopySimpleItkImageFromPython("brain_extracted"); + + if(mitk::DiffusionPropertyHelper::IsDiffusionWeightedImage(mitk_image)) + { + mitk::DiffusionPropertyHelper propertyHelper(image); + mitk::DiffusionPropertyHelper::CopyProperties(mitk_image, image, true); + propertyHelper.InitializeImage(); + } + mitk::DataNode::Pointer corrected_node = mitk::DataNode::New(); corrected_node->SetData( image ); QString name(node->GetName().c_str()); name += "_SkullStripped"; corrected_node->SetName(name.toStdString()); GetDataStorage()->Add(corrected_node, node); } } catch(...) { QMessageBox::warning(nullptr, "Error", "File could not be processed.\nIs pytorch installed on your system?\nDoes your script use the correct input and output variable names (in: in_image & model, out: brain_mask & brain_extracted)?", QMessageBox::Ok); } } diff --git a/Plugins/org.mitk.gui.qt.diffusionimaging.preprocessing/src/internal/QmitkBrainExtractionView.h b/Plugins/org.mitk.gui.qt.diffusionimaging.preprocessing/src/internal/QmitkBrainExtractionView.h index cab1e08a64..6ffc91add7 100644 --- a/Plugins/org.mitk.gui.qt.diffusionimaging.preprocessing/src/internal/QmitkBrainExtractionView.h +++ b/Plugins/org.mitk.gui.qt.diffusionimaging.preprocessing/src/internal/QmitkBrainExtractionView.h @@ -1,100 +1,104 @@ /*=================================================================== 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. ===================================================================*/ #include #include #include #include "ui_QmitkBrainExtractionViewControls.h" #include #include #include #include #include #include #include +#include class QmitkBrainExtractionView; class FileDownloader : public QObject { Q_OBJECT public: explicit FileDownloader(QObject *parent = 0); virtual ~FileDownloader(); QByteArray downloadedData() const; void download(QUrl url); protected slots: void Downloaded(QNetworkReply* reply); private: QNetworkAccessManager m_WebCtrl; }; /*! \brief View for diffusion image registration / head motion correction */ class QmitkBrainExtractionView : 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; + typedef itk::VectorImage< short, 3 > ItkDwiType; + typedef mitk::GradientDirectionsProperty GradProp; + QmitkBrainExtractionView(); virtual ~QmitkBrainExtractionView(); virtual void CreateQtPartControl(QWidget *parent) override; void SetFocus() override; protected slots: void StartBrainExtraction(); void UpdateGUI(); ///< update button activity etc. dpending on current datamanager selection protected: /// \brief called by QmitkAbstractView when DataManager's selection has changed virtual void OnSelectionChanged(berry::IWorkbenchPart::Pointer part, const QList& nodes) override; Ui::QmitkBrainExtractionViewControls* m_Controls; mitk::Image::Pointer m_DiffusionImage; std::vector< mitk::DataNode::Pointer > m_SelectedDiffusionNodes; private: void UpdateRegistrationStatus(); ///< update textual status display of the Registration process // the Qt parent of our GUI (NOT of this object) QWidget* m_Parent; std::string m_ModulePath; };