diff --git a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationView.cpp b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationView.cpp index 0469eac1ac..5a69a8175f 100644 --- a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationView.cpp +++ b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationView.cpp @@ -1,520 +1,453 @@ /*=================================================================== 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 #include #include #include // Qmitk #include "QmitkTbssSkeletonizationView.h" #include #include #include #include #include #include #include #include // Qt #include #include //vtk #include #include // Boost #include const std::string QmitkTbssSkeletonizationView::VIEW_ID = "org.mitk.views.tbssskeletonization"; using namespace berry; -struct TbssSkeletonizationSelListener : ISelectionListener +QmitkTbssSkeletonizationView::QmitkTbssSkeletonizationView() +: QmitkFunctionality() +, m_Controls( 0 ) +, m_MultiWidget( NULL ) { - berryObjectMacro(TbssSkeletonizationSelListener) +} - TbssSkeletonizationSelListener(QmitkTbssSkeletonizationView* view) - { - m_View = view; - } +QmitkTbssSkeletonizationView::~QmitkTbssSkeletonizationView() +{ +} +void QmitkTbssSkeletonizationView::OnSelectionChanged(std::vector nodes) +{ + //datamanager selection changed + if (!this->IsActivated()) + return; + + bool found3dImage = false; + bool found4dImage = false; - void DoSelectionChanged(ISelection::ConstPointer selection) + // iterate selection + for ( int i=0; im_CurrentSelection = selection.Cast(); - // do something with the selected items - if(m_View->m_CurrentSelection) - { - bool found3dImage = false; - bool found4dImage = false; + // only look at interesting types from valid nodes + mitk::BaseData* nodeData = nodes[i]->GetData(); - // iterate selection - for (IStructuredSelection::iterator i = m_View->m_CurrentSelection->Begin(); - i != m_View->m_CurrentSelection->End(); ++i) + if(nodeData) + { + if(QString("Image").compare(nodeData->GetNameOfClass())==0) { - - // extract datatree node - if (mitk::DataNodeObject::Pointer nodeObj = i->Cast()) + mitk::Image* img = static_cast(nodeData); + if(img->GetDimension() == 3) { - mitk::DataNode::Pointer node = nodeObj->GetDataNode(); - - // only look at interesting types - // from valid nodes - mitk::BaseData* nodeData = node->GetData(); - - if(nodeData) - { - if(QString("Image").compare(nodeData->GetNameOfClass())==0) - { - mitk::Image* img = static_cast(nodeData); - if(img->GetDimension() == 3) - { - found3dImage = true; - } - else if(img->GetDimension() == 4) - { - found4dImage = true; - } - } - } + found3dImage = true; + } + else if(img->GetDimension() == 4) + { + found4dImage = true; } - } - - m_View->m_Controls->m_Skeletonize->setEnabled(found3dImage); - m_View->m_Controls->m_Project->setEnabled(found3dImage && found4dImage); - m_View->m_Controls->m_OutputMask->setEnabled(found3dImage && found4dImage); - m_View->m_Controls->m_OutputDistanceMap->setEnabled(found3dImage && found4dImage); } - } - - void SelectionChanged(IWorkbenchPart::Pointer part, ISelection::ConstPointer selection) - { - // check, if selection comes from datamanager - if (part) - { - QString partname(part->GetPartName().c_str()); - if(partname.compare("Datamanager")==0) - { - // apply selection - DoSelectionChanged(selection); - } - } } - QmitkTbssSkeletonizationView* m_View; -}; - - -QmitkTbssSkeletonizationView::QmitkTbssSkeletonizationView() -: QmitkFunctionality() -, m_Controls( 0 ) -, m_MultiWidget( NULL ) -{ + this->m_Controls->m_Skeletonize->setEnabled(found3dImage); + this->m_Controls->m_Project->setEnabled(found3dImage && found4dImage); + this->m_Controls->m_OutputMask->setEnabled(found3dImage && found4dImage); + this->m_Controls->m_OutputDistanceMap->setEnabled(found3dImage && found4dImage); } -QmitkTbssSkeletonizationView::~QmitkTbssSkeletonizationView() -{ -} - -void QmitkTbssSkeletonizationView::OnSelectionChanged(std::vector nodes) -{ - //datamanager selection changed - if (!this->IsActivated()) - return; -} - void QmitkTbssSkeletonizationView::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::QmitkTbssSkeletonizationViewControls; m_Controls->setupUi( parent ); this->CreateConnections(); } - m_SelListener = berry::ISelectionListener::Pointer(new TbssSkeletonizationSelListener(this)); - this->GetSite()->GetWorkbenchWindow()->GetSelectionService()->AddPostSelectionListener(/*"org.mitk.views.datamanager",*/ m_SelListener); - berry::ISelection::ConstPointer sel( - this->GetSite()->GetWorkbenchWindow()->GetSelectionService()->GetSelection("org.mitk.views.datamanager")); - m_CurrentSelection = sel.Cast(); - m_SelListener.Cast()->DoSelectionChanged(sel); - m_IsInitialized = false; - } void QmitkTbssSkeletonizationView::Activated() { QmitkFunctionality::Activated(); berry::ISelection::ConstPointer sel( this->GetSite()->GetWorkbenchWindow()->GetSelectionService()->GetSelection("org.mitk.views.datamanager")); - m_CurrentSelection = sel.Cast(); - m_SelListener.Cast()->DoSelectionChanged(sel); + //m_CurrentSelection = sel.Cast(); + //m_SelListener.Cast()->DoSelectionChanged(sel); } void QmitkTbssSkeletonizationView::Deactivated() { QmitkFunctionality::Deactivated(); } void QmitkTbssSkeletonizationView::CreateConnections() { if ( m_Controls ) { connect( (QObject*)(m_Controls->m_Skeletonize), SIGNAL(clicked()), this, SLOT(Skeletonize() )); connect( (QObject*)(m_Controls->m_Project), SIGNAL(clicked()), this, SLOT(Project() )); } } void QmitkTbssSkeletonizationView::StdMultiWidgetAvailable (QmitkStdMultiWidget &stdMultiWidget) { m_MultiWidget = &stdMultiWidget; } void QmitkTbssSkeletonizationView::StdMultiWidgetNotAvailable() { m_MultiWidget = NULL; } void QmitkTbssSkeletonizationView::Skeletonize() { typedef itk::SkeletonizationFilter SkeletonisationFilterType; SkeletonisationFilterType::Pointer skeletonizer = SkeletonisationFilterType::New(); - if (m_CurrentSelection) - { - mitk::DataStorage::SetOfObjects::Pointer set = - mitk::DataStorage::SetOfObjects::New(); + std::vector nodes = this->GetDataManagerSelection(); - mitk::Image::Pointer meanImage = mitk::Image::New(); - for (IStructuredSelection::iterator i = m_CurrentSelection->Begin(); - i != m_CurrentSelection->End(); - ++i) - { - if (mitk::DataNodeObject::Pointer nodeObj = i->Cast()) - { - mitk::DataNode::Pointer node = nodeObj->GetDataNode(); + mitk::Image::Pointer meanImage = mitk::Image::New(); + std::string name = ""; - // process only on valid nodes - mitk::BaseData* nodeData = node->GetData(); + for ( int i=0; iGetData(); - if(nodeData) + if(nodeData) + { + if(QString("Image").compare(nodeData->GetNameOfClass())==0) + { + mitk::Image* img = static_cast(nodeData); + if(img->GetDimension() == 3) { - if(QString("Image").compare(nodeData->GetNameOfClass())==0) - { - mitk::Image* img = static_cast(nodeData); - if(img->GetDimension() == 3) - { - meanImage = img; - } - } + meanImage = img; + name = nodes[i]->GetName(); } } } + } - // Calculate skeleton - FloatImageType::Pointer itkImg = FloatImageType::New(); - mitk::CastToItkImage(meanImage, itkImg); - skeletonizer->SetInput(itkImg); - skeletonizer->Update(); + // Calculate skeleton + FloatImageType::Pointer itkImg = FloatImageType::New(); + mitk::CastToItkImage(meanImage, itkImg); + skeletonizer->SetInput(itkImg); + skeletonizer->Update(); + + + FloatImageType::Pointer output = skeletonizer->GetOutput(); + mitk::Image::Pointer mitkOutput = mitk::Image::New(); + mitk::CastToMitkImage(output, mitkOutput); + name += "_skeleton"; + AddToDataStorage(mitkOutput, name); - FloatImageType::Pointer output = skeletonizer->GetOutput(); - mitk::Image::Pointer mitkOutput = mitk::Image::New(); - mitk::CastToMitkImage(output, mitkOutput); - AddToDataStorage(mitkOutput, "all_FA_skeletonised"); - } } void QmitkTbssSkeletonizationView::Project() { + typedef itk::SkeletonizationFilter SkeletonisationFilterType; typedef itk::ProjectionFilter ProjectionFilterType; typedef itk::DistanceMapFilter DistanceMapFilterType; SkeletonisationFilterType::Pointer skeletonizer = SkeletonisationFilterType::New(); - if (m_CurrentSelection) - { - mitk::DataStorage::SetOfObjects::Pointer set = - mitk::DataStorage::SetOfObjects::New(); - mitk::Image::Pointer meanImage = mitk::Image::New(); - mitk::Image::Pointer subjects = mitk::Image::New(); + std::vector nodes = this->GetDataManagerSelection(); - for (IStructuredSelection::iterator i = m_CurrentSelection->Begin(); - i != m_CurrentSelection->End(); - ++i) - { - if (mitk::DataNodeObject::Pointer nodeObj = i->Cast()) - { - mitk::DataNode::Pointer node = nodeObj->GetDataNode(); - // process only on valid nodes - mitk::BaseData* nodeData = node->GetData(); + mitk::Image::Pointer meanImage = mitk::Image::New(); + mitk::Image::Pointer subjects = mitk::Image::New(); - if(nodeData) + for ( int i=0; iGetData(); + + if(nodeData) + { + if(QString("Image").compare(nodeData->GetNameOfClass())==0) + { + mitk::Image* img = static_cast(nodeData); + if(img->GetDimension() == 3) { - if(QString("Image").compare(nodeData->GetNameOfClass())==0) - { - mitk::Image* img = static_cast(nodeData); - if(img->GetDimension() == 3) - { - meanImage = img; - } - else if(img->GetDimension() == 4) - { - subjects = img; - } - } + meanImage = img; + } + else if(img->GetDimension() == 4) + { + subjects = img; } - } } - Float4DImageType::Pointer allFA = ConvertToItk(subjects); + } + Float4DImageType::Pointer allFA = ConvertToItk(subjects); - // Calculate skeleton - FloatImageType::Pointer itkImg = FloatImageType::New(); - mitk::CastToItkImage(meanImage, itkImg); - skeletonizer->SetInput(itkImg); - skeletonizer->Update(); + // Calculate skeleton + FloatImageType::Pointer itkImg = FloatImageType::New(); + mitk::CastToItkImage(meanImage, itkImg); + skeletonizer->SetInput(itkImg); + skeletonizer->Update(); - FloatImageType::Pointer output = skeletonizer->GetOutput(); - mitk::Image::Pointer mitkOutput = mitk::Image::New(); - mitk::CastToMitkImage(output, mitkOutput); - AddToDataStorage(mitkOutput, "mean_FA_skeletonised"); + FloatImageType::Pointer output = skeletonizer->GetOutput(); + mitk::Image::Pointer mitkOutput = mitk::Image::New(); + mitk::CastToMitkImage(output, mitkOutput); + AddToDataStorage(mitkOutput, "mean_FA_skeletonised"); - // Retrieve direction image needed later by the projection filter - DirectionImageType::Pointer directionImg = skeletonizer->GetVectorImage(); + // Retrieve direction image needed later by the projection filter + DirectionImageType::Pointer directionImg = skeletonizer->GetVectorImage(); - // Calculate distance image - DistanceMapFilterType::Pointer distanceMapFilter = DistanceMapFilterType::New(); - distanceMapFilter->SetInput(output); - distanceMapFilter->Update(); - FloatImageType::Pointer distanceMap = distanceMapFilter->GetOutput(); + // Calculate distance image + DistanceMapFilterType::Pointer distanceMapFilter = DistanceMapFilterType::New(); + distanceMapFilter->SetInput(output); + distanceMapFilter->Update(); + FloatImageType::Pointer distanceMap = distanceMapFilter->GetOutput(); - if(m_Controls->m_OutputDistanceMap->isChecked()) - { - mitk::Image::Pointer mitkDistance = mitk::Image::New(); - mitk::CastToMitkImage(distanceMap, mitkDistance); - AddToDataStorage(mitkDistance, "distance map"); - } + if(m_Controls->m_OutputDistanceMap->isChecked()) + { + mitk::Image::Pointer mitkDistance = mitk::Image::New(); + mitk::CastToMitkImage(distanceMap, mitkDistance); + AddToDataStorage(mitkDistance, "distance map"); + } - // Do projection + // Do projection - // Ask a threshold to create a skeleton mask - double threshold = -1.0; - while(threshold == -1.0) - { - threshold = QInputDialog::getDouble(m_Controls->m_Skeletonize, tr("Specify the FA threshold"), - tr("Threshold:"), QLineEdit::Normal, - 0.2); + // Ask a threshold to create a skeleton mask + double threshold = -1.0; + while(threshold == -1.0) + { + threshold = QInputDialog::getDouble(m_Controls->m_Skeletonize, tr("Specify the FA threshold"), + tr("Threshold:"), QLineEdit::Normal, + 0.2); - if(threshold < 0.0 || threshold > 1.0) - { - QMessageBox msgBox; - msgBox.setText("Please choose a value between 0 and 1"); - msgBox.exec(); - threshold = -1.0; - } + if(threshold < 0.0 || threshold > 1.0) + { + QMessageBox msgBox; + msgBox.setText("Please choose a value between 0 and 1"); + msgBox.exec(); + threshold = -1.0; } + } - typedef itk::BinaryThresholdImageFilter ThresholdFilterType; - ThresholdFilterType::Pointer thresholder = ThresholdFilterType::New(); - thresholder->SetInput(output); - thresholder->SetLowerThreshold(threshold); - thresholder->SetUpperThreshold(std::numeric_limits::max()); - thresholder->SetOutsideValue(0); - thresholder->SetInsideValue(1); - thresholder->Update(); + typedef itk::BinaryThresholdImageFilter ThresholdFilterType; + ThresholdFilterType::Pointer thresholder = ThresholdFilterType::New(); + thresholder->SetInput(output); + thresholder->SetLowerThreshold(threshold); + thresholder->SetUpperThreshold(std::numeric_limits::max()); + thresholder->SetOutsideValue(0); + thresholder->SetInsideValue(1); + thresholder->Update(); - CharImageType::Pointer thresholdedImg = thresholder->GetOutput(); + CharImageType::Pointer thresholdedImg = thresholder->GetOutput(); - if(m_Controls->m_OutputMask->isChecked()) - { - mitk::Image::Pointer mitkThresholded = mitk::Image::New(); - mitk::CastToMitkImage(thresholdedImg, mitkThresholded); - std::string maskName = "skeleton_mask_at_" + boost::lexical_cast(threshold); - AddToDataStorage(mitkThresholded, maskName); - } + if(m_Controls->m_OutputMask->isChecked()) + { + mitk::Image::Pointer mitkThresholded = mitk::Image::New(); + mitk::CastToMitkImage(thresholdedImg, mitkThresholded); + std::string maskName = "skeleton_mask_at_" + boost::lexical_cast(threshold); + AddToDataStorage(mitkThresholded, maskName); + } - typedef itk::ImageFileReader< CharImageType > CharReaderType; - CharReaderType::Pointer reader = CharReaderType::New(); - reader->SetFileName("/local/testing/LowerCingulum_1mm.nii.gz"); - reader->Update(); - CharImageType::Pointer cingulum = reader->GetOutput(); + typedef itk::ImageFileReader< CharImageType > CharReaderType; + CharReaderType::Pointer reader = CharReaderType::New(); + reader->SetFileName("/local/testing/LowerCingulum_1mm.nii.gz"); + reader->Update(); + CharImageType::Pointer cingulum = reader->GetOutput(); - ProjectionFilterType::Pointer projectionFilter = ProjectionFilterType::New(); - projectionFilter->SetDistanceMap(distanceMap); - projectionFilter->SetDirections(directionImg); - projectionFilter->SetAllFA(allFA); - projectionFilter->SetTube(cingulum); - projectionFilter->SetSkeleton(thresholdedImg); - projectionFilter->Project(); + ProjectionFilterType::Pointer projectionFilter = ProjectionFilterType::New(); + projectionFilter->SetDistanceMap(distanceMap); + projectionFilter->SetDirections(directionImg); + projectionFilter->SetAllFA(allFA); + projectionFilter->SetTube(cingulum); + projectionFilter->SetSkeleton(thresholdedImg); + projectionFilter->Project(); - Float4DImageType::Pointer projected = projectionFilter->GetProjections(); + Float4DImageType::Pointer projected = projectionFilter->GetProjections(); + + mitk::Image::Pointer mitkProjections = mitk::Image::New(); + mitk::CastToMitkImage(projected, mitkProjections); + + AddToDataStorage(mitkProjections, "all_FA_projected"); - mitk::Image::Pointer mitkProjections = mitk::Image::New(); - mitk::CastToMitkImage(projected, mitkProjections); - AddToDataStorage(mitkProjections, "all_FA_projected"); - } } void QmitkTbssSkeletonizationView::AddToDataStorage(mitk::Image* img, std::string name) { mitk::DataNode::Pointer result = mitk::DataNode::New(); result->SetProperty( "name", mitk::StringProperty::New(name) ); result->SetData( img ); // add new image to data storage and set as active to ease further processing GetDefaultDataStorage()->Add( result ); } Float4DImageType::Pointer QmitkTbssSkeletonizationView::ConvertToItk(mitk::Image::Pointer image) { Float4DImageType::Pointer output = Float4DImageType::New(); mitk::Geometry3D* geo = image->GetGeometry(); mitk::Vector3D mitkSpacing = geo->GetSpacing(); mitk::Point3D mitkOrigin = geo->GetOrigin(); Float4DImageType::SpacingType spacing; spacing[0] = mitkSpacing[0]; spacing[1] = mitkSpacing[1]; spacing[2] = mitkSpacing[2]; spacing[3] = 1.0; // todo: check if spacing has length 4 Float4DImageType::PointType origin; origin[0] = mitkOrigin[0]; origin[1] = mitkOrigin[1]; origin[2] = mitkOrigin[2]; origin[3] = 0; Float4DImageType::SizeType size; size[0] = image->GetDimension(0); size[1] = image->GetDimension(1); size[2] = image->GetDimension(2); size[3] = image->GetDimension(3); Float4DImageType::DirectionType dir; vtkLinearTransform* lin = geo->GetVtkTransform(); vtkMatrix4x4 *m = lin->GetMatrix(); dir.Fill(0.0); for(int x=0; x<3; x++) { for(int y=0; y<3; y++) { dir[x][y] = m->GetElement(x,y); } } dir[3][3] = 1; output->SetSpacing(spacing); output->SetOrigin(origin); output->SetRegions(size); output->SetDirection(dir); output->Allocate(); if(image->GetDimension() == 4) { int timesteps = image->GetDimension(3); // iterate through the subjects and copy data to output for(int t=0; tGetDimension(0); x++) { for(int y=0; yGetDimension(1); y++) { for(int z=0; zGetDimension(2); z++) { itk::Index<4> ix4; ix4[0] = x; ix4[1] = y; ix4[2] = z; ix4[3] = t; mitk::Index3D ix; ix[0] = x; ix[1] = y; ix[2] = z; output->SetPixel(ix4, image->GetPixelValueByIndex(ix, t)); } } } } } return output; } diff --git a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationView.h b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationView.h index 42d4763751..0e9833af02 100644 --- a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationView.h +++ b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationView.h @@ -1,122 +1,122 @@ /*=================================================================== 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 QmitkTbssSkeletonizationView_h #define QmitkTbssSkeletonizationView_h // Blueberry -#include -#include -#include +//#include +//#include +//#include // itk #include "itkImage.h" #include "itkVectorImage.h" #include #include "ui_QmitkTbssSkeletonizationViewControls.h" typedef itk::Image FloatImageType; typedef itk::Image CharImageType; typedef itk::Image Float4DImageType; typedef itk::CovariantVector VectorType; typedef itk::Image DirectionImageType; struct TbssSkeletonizationSelListener; /*! \brief QmitkTbssSkeletonizationView \warning This application module is not yet documented. Use "svn blame/praise/annotate" and ask the author to provide basic documentation. \sa QmitkFunctionalitymitkTbssWorkspaceManager \ingroup Functionalities */ class QmitkTbssSkeletonizationView : public QmitkFunctionality { friend struct TbssSkeletonizationSelListener; // this is needed for all Qt objesetupUicts 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; QmitkTbssSkeletonizationView(); virtual ~QmitkTbssSkeletonizationView(); virtual void CreateQtPartControl(QWidget *parent); /// \brief Creation of the connections of main and control widget virtual void CreateConnections(); virtual void StdMultiWidgetAvailable (QmitkStdMultiWidget &stdMultiWidget); virtual void StdMultiWidgetNotAvailable(); /// \brief Called when the functionality is activated virtual void Activated(); virtual void Deactivated(); protected slots: void Skeletonize(); void Project(); protected: /// \brief called by QmitkFunctionality when DataManager's selection has changed virtual void OnSelectionChanged( std::vector nodes ); void InitPointsets(); void SetDefaultNodeProperties(mitk::DataNode::Pointer node, std::string name); - berry::ISelectionListener::Pointer m_SelListener; - berry::IStructuredSelection::ConstPointer m_CurrentSelection; + //berry::ISelectionListener::Pointer m_SelListener; + //berry::IStructuredSelection::ConstPointer m_CurrentSelection; bool m_IsInitialized; Ui::QmitkTbssSkeletonizationViewControls* m_Controls; QmitkStdMultiWidget* m_MultiWidget; void AddToDataStorage(mitk::Image* img, std::string name); Float4DImageType::Pointer ConvertToItk(mitk::Image::Pointer image); }; #endif // _QMITKTbssSkeletonizationVIEW_H_INCLUDED diff --git a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationViewControls.ui b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationViewControls.ui index 55f6829fa8..3a238d4d04 100644 --- a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationViewControls.ui +++ b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkTbssSkeletonizationViewControls.ui @@ -1,71 +1,74 @@ QmitkTbssSkeletonizationViewControls 0 0 431 811 0 0 QmitkTemplate false Skeletonize + + false + Skeletonize and Project false Output binary mask true false Output distance map true