diff --git a/Plugins/org.mitk.gui.qt.bonesegmentation/resources/boneseg.qrc b/Plugins/org.mitk.gui.qt.bonesegmentation/resources/boneseg.qrc index 53910ba52f..b1940d6050 100644 --- a/Plugins/org.mitk.gui.qt.bonesegmentation/resources/boneseg.qrc +++ b/Plugins/org.mitk.gui.qt.bonesegmentation/resources/boneseg.qrc @@ -1,6 +1,5 @@ segment.py - trained_bone_seg_unet.pth.tar - \ No newline at end of file + 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 2244d242a5..b64461fe8a 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,233 @@ /*=================================================================== 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()); // 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"))) { m_Controls.labelLoadTrainedNet->setText("Network loaded!"); } else { m_Controls.labelLoadTrainedNet->setText("Please load a trained network!"); } } 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::DoLoadTrainedNet() { QString pretrainedNetResourcesPath = QFileDialog::getOpenFileName(nullptr, tr("Open File"), - "/home", + 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!"); } 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; } 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() + m_TempNrrdFile; if(QFile::exists(QString::fromStdString(file_name))) { QFile::remove(QString::fromStdString(file_name)); } std::string file_name_temp_seg = mitk::IOUtil::GetTempPath() + m_TempBoneSegFile + ".nrrd"; if(QFile::exists(QString::fromStdString(file_name_temp_seg))) { QFile::remove(QString::fromStdString(file_name_temp_seg)); } mitk::IOUtil::Save(mitk_image, file_name); 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.buttonPerformImageProcessing->setEnabled(false); m_Controls.m_ButtonLoadTrainedNet->setEnabled(false); m_Controls.m_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; 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()); } catch(...) { MITK_WARN << "Error loading output file. Maybe it does not exist." ; } std::string temp_CT = mitk::IOUtil::GetTempPath() + m_TempNrrdFile + ".nrrd"; 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; if(remove(temp_output.c_str()) != 0) { MITK_WARN << "Error deleting file: " + m_TempBoneSegFile; } else { MITK_INFO << "File successfully deleted: " + m_TempBoneSegFile; } 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 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(); }