diff --git a/Plugins/org.mitk.gui.qt.imagecropper/files.cmake b/Plugins/org.mitk.gui.qt.imagecropper/files.cmake index 445c6ee2b2..39181649cf 100644 --- a/Plugins/org.mitk.gui.qt.imagecropper/files.cmake +++ b/Plugins/org.mitk.gui.qt.imagecropper/files.cmake @@ -1,36 +1,36 @@ set(SRC_CPP_FILES ) set(INTERNAL_CPP_FILES mitkPluginActivator.cpp QmitkImageCropper.cpp ) set(UI_FILES - src/internal/ImageCropperControls.ui + src/internal/QmitkImageCropperViewControls.ui ) set(MOC_H_FILES src/internal/mitkPluginActivator.h src/internal/QmitkImageCropper.h ) set(CACHED_RESOURCE_FILES resources/crop.svg plugin.xml ) set(QRC_FILES resources/imagecropper.qrc ) set(CPP_FILES) foreach(file ${SRC_CPP_FILES}) set(CPP_FILES ${CPP_FILES} src/${file}) endforeach(file ${SRC_CPP_FILES}) foreach(file ${INTERNAL_CPP_FILES}) set(CPP_FILES ${CPP_FILES} src/internal/${file}) endforeach(file ${INTERNAL_CPP_FILES}) diff --git a/Plugins/org.mitk.gui.qt.imagecropper/src/internal/ImageCropperControls.ui b/Plugins/org.mitk.gui.qt.imagecropper/src/internal/ImageCropperControls.ui deleted file mode 100644 index 2c0107ebed..0000000000 --- a/Plugins/org.mitk.gui.qt.imagecropper/src/internal/ImageCropperControls.ui +++ /dev/null @@ -1,1133 +0,0 @@ - - - ImageCropperControls - - - - 0 - 0 - 329 - 863 - - - - - 0 - 0 - - - - QmitkTemplate - - - - - - 3 - - - - - - 0 - 0 - - - - QLabel { color: rgb(255, 0, 0) } - - - Please select an image. - - - - - - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 127 - 127 - - - - - - - 255 - 63 - 63 - - - - - - - 127 - 0 - 0 - - - - - - - 170 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 255 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 127 - 127 - - - - - - - 255 - 255 - 220 - - - - - - - 0 - 0 - 0 - - - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 127 - 127 - - - - - - - 255 - 63 - 63 - - - - - - - 127 - 0 - 0 - - - - - - - 170 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 255 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 127 - 127 - - - - - - - 255 - 255 - 220 - - - - - - - 0 - 0 - 0 - - - - - - - - - 120 - 120 - 120 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 127 - 127 - - - - - - - 255 - 63 - 63 - - - - - - - 127 - 0 - 0 - - - - - - - 170 - 0 - 0 - - - - - - - 127 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 127 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 255 - 220 - - - - - - - 0 - 0 - 0 - - - - - - - - Please select a bounding object. - - - - - - - - - false - - - - 0 - 0 - - - - - 0 - 90 - - - - Bounding object - - - - - - - - - 0 - 0 - - - - - 16777215 - 16777215 - - - - - - - - - 0 - 0 - - - - New - - - - - - - - - - 0 - 0 - - - - Do Cropping - - - Mask - - - - - - - - 0 - 0 - - - - Crop - - - - - - - - - - true - - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 127 - 127 - - - - - - - 255 - 63 - 63 - - - - - - - 127 - 0 - 0 - - - - - - - 170 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 255 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 127 - 127 - - - - - - - 255 - 255 - 220 - - - - - - - 0 - 0 - 0 - - - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 127 - 127 - - - - - - - 255 - 63 - 63 - - - - - - - 127 - 0 - 0 - - - - - - - 170 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 255 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 127 - 127 - - - - - - - 255 - 255 - 220 - - - - - - - 0 - 0 - 0 - - - - - - - - - 127 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 127 - 127 - - - - - - - 255 - 63 - 63 - - - - - - - 127 - 0 - 0 - - - - - - - 170 - 0 - 0 - - - - - - - 127 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 127 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 0 - 0 - - - - - - - 255 - 255 - 220 - - - - - - - 0 - 0 - 0 - - - - - - - - Image geometry is rotated, result won't be pixel-aligned. You can reinit your image to get a pixel-aligned result, though. - - - Qt::PlainText - - - true - - - - - - - - 0 - 0 - - - - - 0 - 25 - - - - Advanced settings - - - false - - - 100 - - - Qt::ToolButtonTextBesideIcon - - - false - - - - - - - - 0 - 0 - - - - - 0 - 120 - - - - Output image settings - - - - - - - - - 0 - 0 - - - - Outside pixel value (masking): - - - - - - - false - - - - 0 - 0 - - - - - 16777210 - 16777215 - - - - - - - - - - - 0 - 0 - - - - Overrride original image - - - false - - - - - - - - 0 - 0 - - - - Only crop current timestep - / cut off other timesteps - - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - - QmitkDataStorageComboBox - QComboBox -
QmitkDataStorageComboBox.h
-
- - ctkExpandButton - QToolButton -
ctkExpandButton.h
-
-
- - -
diff --git a/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropper.cpp b/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropper.cpp index 8dbfb3effa..c197561f27 100644 --- a/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropper.cpp +++ b/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropper.cpp @@ -1,537 +1,510 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include "QmitkImageCropper.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include const std::string QmitkImageCropper::VIEW_ID = "org.mitk.views.qmitkimagecropper"; QmitkImageCropper::QmitkImageCropper(QObject *) : m_ParentWidget(nullptr), m_ImageNode(nullptr), - m_CroppingObject(nullptr), - m_CroppingObjectNode(nullptr), + m_BoundingBox(nullptr), + m_BoundingBoxNode(nullptr), m_BoundingShapeInteractor(nullptr), m_CropOutsideValue(0), - m_Advanced(0), m_Active(0), m_ScrollEnabled(true) { CreateBoundingShapeInteractor(false); } QmitkImageCropper::~QmitkImageCropper() { //delete pointer objects - m_CroppingObjectNode = nullptr; - m_CroppingObject = nullptr; + m_BoundingBoxNode = nullptr; + m_BoundingBox = nullptr; //disable interactor if (m_BoundingShapeInteractor != nullptr) { m_BoundingShapeInteractor->SetDataNode(nullptr); m_BoundingShapeInteractor->EnableInteraction(false); } } -void QmitkImageCropper::SetFocus() -{ - m_Controls.buttonCreateNewBoundingBox->setFocus(); -} - void QmitkImageCropper::CreateQtPartControl(QWidget *parent) { // create GUI widgets from the Qt Designer's .ui file m_Controls.setupUi(parent); - m_Controls.boundingShapeSelector->SetDataStorage(this->GetDataStorage()); - m_Controls.boundingShapeSelector->SetPredicate(mitk::NodePredicateAnd::New( + m_Controls.imageSelectionWidget->SetDataStorage(GetDataStorage()); + m_Controls.imageSelectionWidget->SetNodePredicate(mitk::NodePredicateNot::New(mitk::NodePredicateProperty::New("helper object"))); + m_Controls.imageSelectionWidget->SetSelectionIsOptional(true); + m_Controls.imageSelectionWidget->SetEmptyInfo(QString("Please select an image node")); + m_Controls.imageSelectionWidget->SetPopUpTitel(QString("Select image node")); + + connect(m_Controls.imageSelectionWidget, &QmitkSingleNodeSelectionWidget::CurrentSelectionChanged, + this, &QmitkImageCropper::OnImageSelectionChanged); + + m_Controls.boundingBoxSelectionWidget->SetDataStorage(GetDataStorage()); + m_Controls.boundingBoxSelectionWidget->SetNodePredicate(mitk::NodePredicateAnd::New( mitk::TNodePredicateDataType::New(), mitk::NodePredicateNot::New(mitk::NodePredicateProperty::New("helper object")))); - m_CroppingObjectNode = m_Controls.boundingShapeSelector->GetSelectedNode(); + m_Controls.boundingBoxSelectionWidget->SetSelectionIsOptional(true); + m_Controls.boundingBoxSelectionWidget->SetEmptyInfo(QString("Please select a bounding box")); + m_Controls.boundingBoxSelectionWidget->SetPopUpTitel(QString("Select bounding box node")); + + connect(m_Controls.boundingBoxSelectionWidget, &QmitkSingleNodeSelectionWidget::CurrentSelectionChanged, + this, &QmitkImageCropper::OnBoundingBoxSelectionChanged); + connect(m_Controls.buttonCreateNewBoundingBox, SIGNAL(clicked()), this, SLOT(DoCreateNewBoundingObject())); connect(m_Controls.buttonCropping, SIGNAL(clicked()), this, SLOT(DoCropping())); connect(m_Controls.buttonMasking, SIGNAL(clicked()), this, SLOT(DoMasking())); - connect(m_Controls.boundingShapeSelector, SIGNAL(OnSelectionChanged(const mitk::DataNode*)), - this, SLOT(OnDataSelectionChanged(const mitk::DataNode*))); - connect(m_Controls.buttonCreateNewBoundingBox, SIGNAL(clicked()), this, SLOT(DoCreateNewBoundingObject())); - connect(m_Controls.buttonAdvancedSettings, SIGNAL(clicked()), this, SLOT(OnAdvancedSettingsButtonToggled())); + connect(m_Controls.buttonAdvancedSettings, &ctkExpandButton::clicked, + this, [this]() + { + m_Controls.groupImageSettings->setVisible(!m_Controls.groupImageSettings->isVisible()); + }); + connect(m_Controls.spinBoxOutsidePixelValue, SIGNAL(valueChanged(int)), this, SLOT(OnSliderValueChanged(int))); SetDefaultGUI(); - m_Controls.labelWarningRotation->setVisible(false); - m_Advanced = false; - this->OnAdvancedSettingsButtonToggled(); m_ParentWidget = parent; } -void QmitkImageCropper::OnDataSelectionChanged(const mitk::DataNode*) +void QmitkImageCropper::OnBoundingBoxSelectionChanged(QList nodes) { - m_Controls.boundingShapeSelector->setEnabled(true); - m_CroppingObjectNode = m_Controls.boundingShapeSelector->GetSelectedNode(); + if (nodes.empty() || nodes.front().IsNull()) + { + m_BoundingBoxNode = nullptr; + m_BoundingBox = nullptr; + SetDefaultGUI(); + + m_BoundingShapeInteractor->EnableInteraction(false); + m_BoundingShapeInteractor->SetDataNode(nullptr); + return; + } + + auto boundingBoxNode = m_BoundingBoxNode.Lock(); + if (nodes.front() == boundingBoxNode) + { + // node already selected, nothing to change + return; + } - if (m_CroppingObjectNode.IsNotNull() && dynamic_cast(this->m_CroppingObjectNode->GetData())) + m_BoundingBox = dynamic_cast(nodes.front()->GetData()); + if (nullptr != m_BoundingBox) { - m_Controls.buttonAdvancedSettings->setEnabled(true); - m_Controls.groupImageSettings->setEnabled(true); - m_Controls.labelWarningBB->setVisible(false); - m_CroppingObject = dynamic_cast(m_CroppingObjectNode->GetData()); - m_Advanced = true; + // node newly selected + m_BoundingBoxNode = nodes.front(); + boundingBoxNode = m_BoundingBoxNode.Lock(); + boundingBoxNode->SetVisibility(true); m_BoundingShapeInteractor->EnableInteraction(true); - m_BoundingShapeInteractor->SetDataNode(m_CroppingObjectNode); + m_BoundingShapeInteractor->SetDataNode(boundingBoxNode); mitk::RenderingManager::GetInstance()->InitializeViews(); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); - } - else - { - SetDefaultGUI(); - m_CroppingObject = nullptr; - m_BoundingShapeInteractor->EnableInteraction(false); - m_BoundingShapeInteractor->SetDataNode(nullptr); - m_Advanced = false; - this->OnAdvancedSettingsButtonToggled(); - } -} -void QmitkImageCropper::OnAdvancedSettingsButtonToggled() -{ - m_Controls.groupImageSettings->setVisible(m_Advanced); - m_Advanced = !m_Advanced; + auto imageNode = m_ImageNode.Lock(); + if (imageNode.IsNotNull()) + { + m_Controls.buttonCropping->setEnabled(true); + m_Controls.buttonMasking->setEnabled(true); + m_Controls.buttonAdvancedSettings->setEnabled(true); + m_Controls.groupImageSettings->setEnabled(true); + } + } } void QmitkImageCropper::CreateBoundingShapeInteractor(bool rotationEnabled) { if (m_BoundingShapeInteractor.IsNull()) { m_BoundingShapeInteractor = mitk::BoundingShapeInteractor::New(); m_BoundingShapeInteractor->LoadStateMachine("BoundingShapeInteraction.xml", us::ModuleRegistry::GetModule("MitkBoundingShape")); m_BoundingShapeInteractor->SetEventConfig("BoundingShapeMouseConfig.xml", us::ModuleRegistry::GetModule("MitkBoundingShape")); } m_BoundingShapeInteractor->SetRotationEnabled(rotationEnabled); } mitk::Geometry3D::Pointer QmitkImageCropper::InitializeWithImageGeometry(mitk::BaseGeometry::Pointer geometry) { // convert a BaseGeometry into a Geometry3D (otherwise IO is not working properly) if (geometry == nullptr) mitkThrow() << "Geometry is not valid."; auto boundingGeometry = mitk::Geometry3D::New(); boundingGeometry->SetBounds(geometry->GetBounds()); boundingGeometry->SetImageGeometry(geometry->GetImageGeometry()); boundingGeometry->SetOrigin(geometry->GetOrigin()); boundingGeometry->SetSpacing(geometry->GetSpacing()); boundingGeometry->SetIndexToWorldTransform(geometry->GetIndexToWorldTransform()); boundingGeometry->Modified(); return boundingGeometry; } void QmitkImageCropper::DoCreateNewBoundingObject() { - if (!m_ImageNode.IsExpired()) + if (m_ImageNode.IsExpired()) { - auto imageNode = m_ImageNode.Lock(); - QString name = QString::fromStdString(imageNode->GetName() + " Bounding Shape"); + return; + } - auto boundingShape = this->GetDataStorage()->GetNode(mitk::NodePredicateFunction::New([&name](const mitk::DataNode *node) - { - return 0 == node->GetName().compare(name.toStdString()); - })); - - if (nullptr != boundingShape) - name = this->AdaptBoundingObjectName(name); - - m_Controls.buttonCropping->setEnabled(true); - m_Controls.buttonMasking->setEnabled(true); - m_Controls.boundingShapeSelector->setEnabled(true); - m_Controls.groupImageSettings->setEnabled(true); - - // get current timestep to support 3d+t images - auto renderWindowPart = this->GetRenderWindowPart(mitk::WorkbenchUtil::IRenderWindowPartStrategy::OPEN); - int timeStep = renderWindowPart->GetTimeNavigationController()->GetTime()->GetPos(); - mitk::BaseGeometry::Pointer imageGeometry = static_cast(imageNode->GetData()->GetGeometry(timeStep)); - - m_CroppingObject = mitk::GeometryData::New(); - m_CroppingObject->SetGeometry(static_cast(this->InitializeWithImageGeometry(imageGeometry))); - m_CroppingObjectNode = mitk::DataNode::New(); - m_CroppingObjectNode->SetData(m_CroppingObject); - m_CroppingObjectNode->SetProperty("name", mitk::StringProperty::New(name.toStdString())); - m_CroppingObjectNode->SetProperty("color", mitk::ColorProperty::New(1.0, 1.0, 1.0)); - m_CroppingObjectNode->SetProperty("opacity", mitk::FloatProperty::New(0.6)); - m_CroppingObjectNode->SetProperty("layer", mitk::IntProperty::New(99)); - m_CroppingObjectNode->AddProperty("handle size factor", mitk::DoubleProperty::New(1.0 / 40.0)); - m_CroppingObjectNode->SetBoolProperty("pickable", true); - - if (!this->GetDataStorage()->Exists(m_CroppingObjectNode)) - { - GetDataStorage()->Add(m_CroppingObjectNode, imageNode); - m_Controls.boundingShapeSelector->SetSelectedNode(m_CroppingObjectNode); - m_CroppingObjectNode->SetVisibility(true); - m_BoundingShapeInteractor->EnableInteraction(true); - m_BoundingShapeInteractor->SetDataNode(this->m_CroppingObjectNode); - this->OnDataSelectionChanged(m_CroppingObjectNode); - } + auto imageNode = m_ImageNode.Lock(); + QString name = QString::fromStdString(imageNode->GetName() + " Bounding Shape"); + + auto boundingShape = this->GetDataStorage()->GetNode(mitk::NodePredicateFunction::New([&name](const mitk::DataNode *node) + { + return 0 == node->GetName().compare(name.toStdString()); + })); + + if (nullptr != boundingShape) + { + name = this->AdaptBoundingObjectName(name); + } + + // get current timestep to support 3d+t images + auto renderWindowPart = this->GetRenderWindowPart(mitk::WorkbenchUtil::IRenderWindowPartStrategy::OPEN); + int timeStep = renderWindowPart->GetTimeNavigationController()->GetTime()->GetPos(); + auto imageGeometry = static_cast(imageNode->GetData()->GetGeometry(timeStep)); + + auto boundingBox = mitk::GeometryData::New(); + boundingBox->SetGeometry(static_cast(this->InitializeWithImageGeometry(imageGeometry))); + auto boundingBoxNode = mitk::DataNode::New(); + boundingBoxNode->SetData(boundingBox); + boundingBoxNode->SetProperty("name", mitk::StringProperty::New(name.toStdString())); + boundingBoxNode->SetProperty("color", mitk::ColorProperty::New(1.0, 1.0, 1.0)); + boundingBoxNode->SetProperty("opacity", mitk::FloatProperty::New(0.6)); + boundingBoxNode->SetProperty("layer", mitk::IntProperty::New(99)); + boundingBoxNode->AddProperty("handle size factor", mitk::DoubleProperty::New(1.0 / 40.0)); + boundingBoxNode->SetBoolProperty("pickable", true); + + if (!this->GetDataStorage()->Exists(boundingBoxNode)) + { + GetDataStorage()->Add(boundingBoxNode, imageNode); } - // Adjust coordinate system by doing a reinit on - auto tempDataStorage = mitk::DataStorage::SetOfObjects::New(); - tempDataStorage->InsertElement(0, m_CroppingObjectNode); - - //// initialize the views to the bounding geometry - //mitk::TimeGeometry::Pointer bounds = this->GetDataStorage()->ComputeBoundingGeometry3D(tempDataStorage); - //mitk::RenderingManager::GetInstance()->InitializeViews(bounds); - //mitk::RenderingManager::GetInstance()->RequestUpdateAll(); + + m_Controls.boundingBoxSelectionWidget->SetCurrentSelectedNode(boundingBoxNode); } -void QmitkImageCropper::OnSelectionChanged(berry::IWorkbenchPart::Pointer /*part*/, const QList& nodes) +void QmitkImageCropper::OnImageSelectionChanged(QList nodes) { bool rotationEnabled = false; - if (nodes.empty()) + if (nodes.empty() || nodes.front().IsNull()) { + m_ImageNode = nullptr; SetDefaultGUI(); return; } - m_ParentWidget->setEnabled(true); - foreach(mitk::DataNode::Pointer node, nodes) + auto imageNode = m_ImageNode.Lock(); + if (nodes.front() == imageNode) { - if (node.IsNotNull() && dynamic_cast(node->GetData())) + // node already selected, nothing to change + return; + } + + auto image = dynamic_cast(nodes.front()->GetData()); + if (nullptr != image) + { + if (image->GetDimension() < 3) { - m_ImageNode = nodes[0]; - m_Controls.groupBoundingObject->setEnabled(true); - m_Controls.labelWarningImage->setStyleSheet(""); - m_Controls.labelWarningImage->setText(QString::fromStdString("Selected image: " + nodes[0]->GetName())); - m_Controls.buttonCreateNewBoundingBox->setEnabled(true); - - mitk::Image::Pointer image = dynamic_cast(nodes[0]->GetData()); - if (image != nullptr) - { - if (image->GetDimension() < 3) { - QMessageBox::warning(nullptr, - tr("Invalid image selected"), - tr("ImageCropper only works with 3 or more dimensions."), - QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton); - SetDefaultGUI(); - return; - } + QMessageBox::warning(nullptr, + tr("Invalid image selected"), + tr("ImageCropper only works with 3 or more dimensions."), + QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton); + SetDefaultGUI(); + return; + } - vtkSmartPointer imageMat = image->GetGeometry()->GetVtkMatrix(); - // check whether the image geometry is rotated, if so, no pixel aligned cropping or masking can be performed - if ((imageMat->GetElement(1, 0) == 0.0) && (imageMat->GetElement(0, 1) == 0.0) && - (imageMat->GetElement(1, 2) == 0.0) && (imageMat->GetElement(2, 1) == 0.0) && - (imageMat->GetElement(2, 0) == 0.0) && (imageMat->GetElement(0, 2) == 0.0)) - { - rotationEnabled = false; - m_Controls.labelWarningRotation->setVisible(false); - } - else - { - rotationEnabled = true; - m_Controls.labelWarningRotation->setStyleSheet(" QLabel { color: rgb(255, 0, 0) }"); - m_Controls.labelWarningRotation->setVisible(true); - } + // node newly selected + m_ImageNode = nodes.front(); - this->CreateBoundingShapeInteractor(rotationEnabled); - m_CroppingObjectNode = m_Controls.boundingShapeSelector->GetSelectedNode(); - if (m_CroppingObjectNode != nullptr) - { - this->OnDataSelectionChanged(m_CroppingObjectNode); - m_BoundingShapeInteractor->EnableInteraction(true); - m_BoundingShapeInteractor->SetDataNode(this->m_CroppingObjectNode); - m_Controls.boundingShapeSelector->setEnabled(true); - } + m_ParentWidget->setEnabled(true); + m_Controls.buttonCreateNewBoundingBox->setEnabled(true); - if (image->GetPixelType().GetPixelType() == itk::ImageIOBase::SCALAR) - { - // Might be changed with the upcoming new image statistics plugin - //(recomputation might be very expensive for large images ;) ) - auto statistics = image->GetStatistics(); - auto minPixelValue = statistics->GetScalarValueMin(); - auto maxPixelValue = statistics->GetScalarValueMax(); - - if (minPixelValue < std::numeric_limits::min()) { - minPixelValue = std::numeric_limits::min(); - } - if (maxPixelValue > std::numeric_limits::max()) { - maxPixelValue = std::numeric_limits::max(); - } + vtkSmartPointer imageMat = image->GetGeometry()->GetVtkMatrix(); + // check whether the image geometry is rotated; if so, no pixel aligned cropping or masking can be performed + if ((imageMat->GetElement(1, 0) == 0.0) && (imageMat->GetElement(0, 1) == 0.0) && + (imageMat->GetElement(1, 2) == 0.0) && (imageMat->GetElement(2, 1) == 0.0) && + (imageMat->GetElement(2, 0) == 0.0) && (imageMat->GetElement(0, 2) == 0.0)) + { + rotationEnabled = false; + m_Controls.labelWarningRotation->setVisible(false); + } + else + { + rotationEnabled = true; + m_Controls.labelWarningRotation->setStyleSheet(" QLabel { color: rgb(255, 0, 0) }"); + m_Controls.labelWarningRotation->setVisible(true); + } - m_Controls.spinBoxOutsidePixelValue->setEnabled(true); - m_Controls.spinBoxOutsidePixelValue->setMaximum(static_cast(maxPixelValue)); - m_Controls.spinBoxOutsidePixelValue->setMinimum(static_cast(minPixelValue)); - m_Controls.spinBoxOutsidePixelValue->setValue(static_cast(minPixelValue)); - } - else - { - m_Controls.spinBoxOutsidePixelValue->setEnabled(false); - } + this->CreateBoundingShapeInteractor(rotationEnabled); - unsigned int dim = image->GetDimension(); - if (dim < 2 || dim > 4) - { - m_Controls.labelWarningImage->setStyleSheet(" QLabel { color: rgb(255, 0, 0) }"); - m_Controls.labelWarningImage->setText(QString::fromStdString("Select an image.")); - m_ParentWidget->setEnabled(false); - } - if (m_CroppingObjectNode != nullptr) - { - m_Controls.buttonCropping->setEnabled(true); - m_Controls.buttonMasking->setEnabled(true); - m_Controls.boundingShapeSelector->setEnabled(true); - m_Controls.labelWarningBB->setVisible(false); - } - else - { - m_Controls.buttonCropping->setEnabled(false); - m_Controls.buttonMasking->setEnabled(false); - m_Controls.boundingShapeSelector->setEnabled(false); - m_Controls.labelWarningBB->setVisible(true); - } - return; + if (itk::ImageIOBase::SCALAR == image->GetPixelType().GetPixelType()) + { + // Might be changed with the upcoming new image statistics plugin + //(recomputation might be very expensive for large images ;) ) + auto statistics = image->GetStatistics(); + auto minPixelValue = statistics->GetScalarValueMin(); + auto maxPixelValue = statistics->GetScalarValueMax(); + + if (minPixelValue < std::numeric_limits::min()) + { + minPixelValue = std::numeric_limits::min(); } - // iterate all selected objects, adjust warning visibility - SetDefaultGUI(); - m_ParentWidget->setEnabled(true); - m_Controls.labelWarningRotation->setVisible(false); + if (maxPixelValue > std::numeric_limits::max()) + { + maxPixelValue = std::numeric_limits::max(); + } + + m_Controls.spinBoxOutsidePixelValue->setEnabled(true); + m_Controls.spinBoxOutsidePixelValue->setMaximum(static_cast(maxPixelValue)); + m_Controls.spinBoxOutsidePixelValue->setMinimum(static_cast(minPixelValue)); + m_Controls.spinBoxOutsidePixelValue->setValue(static_cast(minPixelValue)); + } + else + { + m_Controls.spinBoxOutsidePixelValue->setEnabled(false); + } + + unsigned int dim = image->GetDimension(); + if (dim < 2 || dim > 4) + { + m_ParentWidget->setEnabled(false); + } + + auto boundingBoxNode = m_BoundingBoxNode.Lock(); + if (boundingBoxNode.IsNotNull()) + { + m_Controls.buttonCropping->setEnabled(true); + m_Controls.buttonMasking->setEnabled(true); + m_Controls.buttonAdvancedSettings->setEnabled(true); + m_Controls.groupImageSettings->setEnabled(true); } } } - void QmitkImageCropper::OnSliderValueChanged(int slidervalue) { m_CropOutsideValue = slidervalue; } void QmitkImageCropper::DoMasking() { this->ProcessImage(true); } void QmitkImageCropper::DoCropping() { this->ProcessImage(false); } void QmitkImageCropper::ProcessImage(bool mask) { - // cropping only possible if valid bounding shape as well as a valid image are loaded - QList nodes = this->GetDataManagerSelection(); - auto renderWindowPart = this->GetRenderWindowPart(mitk::WorkbenchUtil::IRenderWindowPartStrategy::OPEN); int timeStep = renderWindowPart->GetTimeNavigationController()->GetTime()->GetPos(); - if (nodes.empty()) return; - - mitk::DataNode* node = nodes[0]; - - if (node == nullptr) + auto imageNode = m_ImageNode.Lock(); + if (imageNode.IsNull()) { QMessageBox::information(nullptr, "Warning", "Please load and select an image before starting image processing."); return; } - if (m_CroppingObject == nullptr) + + auto boundingBoxNode = m_BoundingBoxNode.Lock(); + if (boundingBoxNode.IsNull()) { QMessageBox::information(nullptr, "Warning", "Please load and select a cropping object before starting image processing."); return; } - mitk::BaseData* data = node->GetData(); //get data from node - if (data != nullptr) + mitk::BaseData* image = imageNode->GetData(); + if (nullptr != image) { QString imageName; if (mask) { - imageName = QString::fromStdString(node->GetName() + "_" + m_CroppingObjectNode->GetName() + "_masked"); + imageName = QString::fromStdString(imageNode->GetName() + "_" + boundingBoxNode->GetName() + "_masked"); } else { - imageName = QString::fromStdString(node->GetName() + "_" + m_CroppingObjectNode->GetName() + "_cropped"); + imageName = QString::fromStdString(imageNode->GetName() + "_" + boundingBoxNode->GetName() + "_cropped"); } if (m_Controls.checkBoxCropTimeStepOnly->isChecked()) { imageName = imageName + "_T" + QString::number(timeStep); } // image and bounding shape ok, set as input auto croppedImageNode = mitk::DataNode::New(); auto cutter = mitk::BoundingShapeCropper::New(); - cutter->SetGeometry(m_CroppingObject); + cutter->SetGeometry(m_BoundingBox); // adjustable in advanced settings cutter->SetUseWholeInputRegion(mask); //either mask (mask=true) or crop (mask=false) cutter->SetOutsideValue(m_CropOutsideValue); cutter->SetUseCropTimeStepOnly(m_Controls.checkBoxCropTimeStepOnly->isChecked()); cutter->SetCurrentTimeStep(timeStep); // TODO: Add support for MultiLayer (right now only Mulitlabel support) - mitk::LabelSetImage* labelsetImageInput = dynamic_cast(data); - if (labelsetImageInput != nullptr) + auto labelsetImageInput = dynamic_cast(image); + if (nullptr != labelsetImageInput) { cutter->SetInput(labelsetImageInput); // do the actual cutting try { cutter->Update(); } catch (const itk::ExceptionObject& e) { std::string message = std::string("The Cropping filter could not process because of: \n ") + e.GetDescription(); - QMessageBox::warning(nullptr, - tr("Cropping not possible!"), - tr(message.c_str()), + QMessageBox::warning(nullptr, tr("Cropping not possible!"), tr(message.c_str()), QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton); return; } auto labelSetImage = mitk::LabelSetImage::New(); labelSetImage->InitializeByLabeledImage(cutter->GetOutput()); for (unsigned int i = 0; i < labelsetImageInput->GetNumberOfLayers(); i++) { labelSetImage->AddLabelSetToLayer(i, labelsetImageInput->GetLabelSet(i)); } croppedImageNode->SetData(labelSetImage); croppedImageNode->SetProperty("name", mitk::StringProperty::New(imageName.toStdString())); //add cropping result to the current data storage as child node to the image node if (!m_Controls.checkOverwriteImage->isChecked()) { if (!this->GetDataStorage()->Exists(croppedImageNode)) { this->GetDataStorage()->Add(croppedImageNode, m_ImageNode.Lock()); } } else // original image will be overwritten by the result image and the bounding box of the result is adjusted { - node->SetData(labelSetImage); - node->Modified(); + imageNode->SetData(labelSetImage); + imageNode->Modified(); // Adjust coordinate system by doing a reinit on auto tempDataStorage = mitk::DataStorage::SetOfObjects::New(); - tempDataStorage->InsertElement(0, node); + tempDataStorage->InsertElement(0, imageNode); // initialize the views to the bounding geometry auto bounds = this->GetDataStorage()->ComputeBoundingGeometry3D(tempDataStorage); mitk::RenderingManager::GetInstance()->InitializeViews(bounds); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); } } else { - mitk::Image::Pointer imageInput = dynamic_cast(data); - if (imageInput != nullptr) + auto imageInput = dynamic_cast(image); + if (nullptr != imageInput) { cutter->SetInput(imageInput); // do the actual cutting try { cutter->Update(); } catch (const itk::ExceptionObject& e) { std::string message = std::string("The Cropping filter could not process because of: \n ") + e.GetDescription(); - - QMessageBox::warning(nullptr, - tr("Cropping not possible!"), - tr(message.c_str()), + QMessageBox::warning(nullptr, tr("Cropping not possible!"), tr(message.c_str()), QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton); return; } //add cropping result to the current data storage as child node to the image node if (!m_Controls.checkOverwriteImage->isChecked()) { croppedImageNode->SetData(cutter->GetOutput()); croppedImageNode->SetProperty("name", mitk::StringProperty::New(imageName.toStdString())); croppedImageNode->SetProperty("color", mitk::ColorProperty::New(1.0, 1.0, 1.0)); croppedImageNode->SetProperty("layer", mitk::IntProperty::New(99)); // arbitrary, copied from segmentation functionality if (!this->GetDataStorage()->Exists(croppedImageNode)) { this->GetDataStorage()->Add(croppedImageNode, m_ImageNode.Lock()); } } else // original image will be overwritten by the result image and the bounding box of the result is adjusted { - node->SetData(cutter->GetOutput()); - node->Modified(); + imageNode->SetData(cutter->GetOutput()); + imageNode->Modified(); // Adjust coordinate system by doing a reinit on auto tempDataStorage = mitk::DataStorage::SetOfObjects::New(); - tempDataStorage->InsertElement(0, node); + tempDataStorage->InsertElement(0, imageNode); // initialize the views to the bounding geometry auto bounds = this->GetDataStorage()->ComputeBoundingGeometry3D(tempDataStorage); mitk::RenderingManager::GetInstance()->InitializeViews(bounds); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); } } } } else { QMessageBox::information(nullptr, "Warning", "Please load and select an image before starting image processing."); } } void QmitkImageCropper::SetDefaultGUI() { - m_Controls.labelWarningImage->setStyleSheet(" QLabel { color: rgb(255, 0, 0) }"); - m_Controls.labelWarningImage->setText(QString::fromStdString("Select an image.")); - m_Controls.labelWarningImage->setVisible(true); - m_Controls.labelWarningBB->setStyleSheet(" QLabel { color: rgb(255, 0, 0) }"); - m_Controls.labelWarningBB->setText(QString::fromStdString("Create a bounding shape below.")); - m_Controls.labelWarningBB->setVisible(true); - m_Controls.buttonCreateNewBoundingBox->setEnabled(false); m_Controls.labelWarningRotation->setVisible(false); + m_Controls.buttonCreateNewBoundingBox->setEnabled(false); m_Controls.buttonCropping->setEnabled(false); m_Controls.buttonMasking->setEnabled(false); - m_Controls.boundingShapeSelector->setEnabled(false); m_Controls.buttonAdvancedSettings->setEnabled(false); m_Controls.groupImageSettings->setEnabled(false); + m_Controls.groupImageSettings->setVisible(false); m_Controls.checkOverwriteImage->setChecked(false); m_Controls.checkBoxCropTimeStepOnly->setChecked(false); } QString QmitkImageCropper::AdaptBoundingObjectName(const QString& name) const { unsigned int counter = 2; QString newName = QString("%1 %2").arg(name).arg(counter); while (nullptr != this->GetDataStorage()->GetNode(mitk::NodePredicateFunction::New([&newName](const mitk::DataNode *node) { return 0 == node->GetName().compare(newName.toStdString()); }))) { newName = QString("%1 %2").arg(name).arg(++counter); } return newName; } diff --git a/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropper.h b/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropper.h index 8c193333be..77165871f1 100644 --- a/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropper.h +++ b/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropper.h @@ -1,120 +1,110 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef QmitkImageCropper_h #define QmitkImageCropper_h #include #include #include -#include "ui_ImageCropperControls.h" +#include "ui_QmitkImageCropperViewControls.h" class QmitkImageCropper : public QmitkAbstractView { Q_OBJECT public: QmitkImageCropper(QObject *parent = nullptr); ~QmitkImageCropper() override; static const std::string VIEW_ID; void CreateQtPartControl(QWidget *parent) override; - void SetFocus() override; + void SetFocus() override { }; protected Q_SLOTS: /*! * @brief Creates a new bounding object */ virtual void DoCreateNewBoundingObject(); /*! * @brief Whenever Crop button is pressed, issue a cropping action */ void DoCropping(); /*! * @brief Whenever Mask button is pressed, issue a masking action */ void DoMasking(); /*! - * @brief Dis- or enable the advanced setting section - */ - void OnAdvancedSettingsButtonToggled(); - /*! * @brief Updates current selection of the bounding object */ - void OnDataSelectionChanged(const mitk::DataNode* node); + void OnBoundingBoxSelectionChanged(QList nodes); /*! * @brief Sets the scalar value for outside pixels in case of masking */ void OnSliderValueChanged(int slidervalue); protected: - /*! - * @brief called by QmitkAbstractView when DataManager's selection has changed - */ - void OnSelectionChanged(berry::IWorkbenchPart::Pointer part, const QList& nodes) override; - /*! + void OnImageSelectionChanged(QList nodes); /*! * @brief Initializes a new bounding shape using the selected image geometry. */ mitk::Geometry3D::Pointer InitializeWithImageGeometry(mitk::BaseGeometry::Pointer geometry); void CreateBoundingShapeInteractor(bool rotationEnabled); private: QWidget* m_ParentWidget; /*! * @brief A pointer to the node of the image to be cropped. */ mitk::WeakPointer m_ImageNode; /*! - * @brief The cuboid used for cropping. + * @brief A pointer to the node of the bounding box used for cropping. */ - mitk::GeometryData::Pointer m_CroppingObject; - + mitk::WeakPointer m_BoundingBoxNode; /*! - * @brief Tree node of the cuboid used for cropping. + * @brief The cuboid used for cropping. */ - mitk::DataNode::Pointer m_CroppingObjectNode; - + mitk::GeometryData::Pointer m_BoundingBox; /*! * @brief Interactor for moving and scaling the cuboid */ mitk::BoundingShapeInteractor::Pointer m_BoundingShapeInteractor; void ProcessImage(bool crop); /*! * @brief Resets GUI to default */ void SetDefaultGUI(); QString AdaptBoundingObjectName(const QString& name) const; // cropping parameter mitk::ScalarType m_CropOutsideValue; bool m_Advanced; bool m_Active; bool m_ScrollEnabled; - Ui::ImageCropperControls m_Controls; + Ui::QmitkImageCropperViewControls m_Controls; }; #endif // QmitkImageCropper_h diff --git a/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropperViewControls.ui b/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropperViewControls.ui new file mode 100644 index 0000000000..3f04d50a73 --- /dev/null +++ b/Plugins/org.mitk.gui.qt.imagecropper/src/internal/QmitkImageCropperViewControls.ui @@ -0,0 +1,316 @@ + + + QmitkImageCropperViewControls + + + + 0 + 0 + 300 + 600 + + + + + + + + 0 + 0 + + + + Data Selection + + + + + + + 0 + 0 + + + + Image + + + + + + + + 0 + 0 + + + + + 0 + 40 + + + + + + + + + 0 + 0 + + + + Bounding Box + + + + + + + + 0 + 0 + + + + + 0 + 40 + + + + + + + + + 0 + 0 + + + + + 0 + 40 + + + + New + + + + + + + + + + + 0 + 0 + + + + + 0 + 90 + + + + Processing + + + + + + + 0 + 0 + + + + Mask + + + + + + + + 0 + 0 + + + + Crop + + + + + + + + + + true + + + Image geometry is rotated, result won't be pixel-aligned. You can reinit your image to get a pixel-aligned result, though. + + + Qt::PlainText + + + true + + + + + + + + 0 + 0 + + + + + 0 + 25 + + + + Advanced settings + + + false + + + 100 + + + Qt::ToolButtonTextBesideIcon + + + false + + + + + + + + 0 + 0 + + + + + 0 + 120 + + + + Output image settings + + + + + + + + + 0 + 0 + + + + Outside pixel value (masking): + + + + + + + false + + + + 0 + 0 + + + + + + + + + + + 0 + 0 + + + + Overrride original image + + + false + + + + + + + + 0 + 0 + + + + Only crop current timestep +/ cut off other timesteps + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + QmitkDataStorageComboBox + QComboBox +
QmitkDataStorageComboBox.h
+
+ + ctkExpandButton + QToolButton +
ctkExpandButton.h
+
+ + QmitkSingleNodeSelectionWidget + QWidget +
qmitksinglenodeselectionwidget.h
+
+
+ + +