Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
QmitkBSplineRegistrationView.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 #include "itkImageFileReader.h"
19 #include "itkRegularStepGradientDescentOptimizer.h"
21 #include "ui_QmitkBSplineRegistrationViewControls.h"
22 #include <mitkImageCast.h>
24 #include <mitkRenderingManager.h>
25 #include <qfiledialog.h>
26 #include <qmessagebox.h>
27 #include <qvalidator.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 typedef itk::Vector<float, 3> VectorType;
32 typedef itk::Image<VectorType, 3> DeformationFieldType;
33 
34 typedef itk::ImageFileReader<DeformationFieldType> ImageReaderType;
35 
37  : QWidget(parent, f), m_FixedNode(nullptr), m_MovingNode(nullptr)
38 {
39  m_Controls.setupUi(parent);
40 
41  QObject::connect(
42  (QObject *)(m_Controls.m_PrintDeformField), SIGNAL(clicked()), (QObject *)this, SLOT(PrintDeformationField()));
43 
44  QObject::connect((QObject *)(m_Controls.m_BrowseDeformationField),
45  SIGNAL(clicked()),
46  (QObject *)this,
47  SLOT(SelectDeformationField()));
48 
49  connect(m_Controls.m_OptimizerSelector,
50  SIGNAL(activated(int)),
51  m_Controls.m_OptimizerWidgetStack,
52  SLOT(setCurrentIndex(int)));
53  connect(m_Controls.m_OptimizerSelector, SIGNAL(activated(int)), this, SLOT(OptimizerSelected(int)));
54 }
55 
57 {
58 }
59 
61 {
63  if (optimizer == 0)
64  {
65  m_Controls.m_LBFGSFrame->show();
66  }
67  else if (optimizer == 1)
68  {
69  m_Controls.m_GradientDescentFrame->show();
70  }
71 }
72 
74 {
75  m_Controls.m_LBFGSFrame->hide();
76  m_Controls.m_GradientDescentFrame->hide();
77 }
78 
80 {
81  // SELECT FOLDER DIALOG
82  QFileDialog *w = new QFileDialog(this, "Select Deformation Field");
83  w->setFileMode(QFileDialog::ExistingFiles);
84  w->setNameFilter("Images (*.mhd)");
85  w->setDirectory("G:\\home\\vanbrugg\\testimages\\deformable");
86 
87  // RETRIEVE SELECTION
88  if (w->exec() != QDialog::Accepted)
89  {
90  return;
91  cout << "Failed to load" << endl;
92  }
93 
94  QStringList filenames = w->selectedFiles();
95  QStringList::Iterator it = filenames.begin();
96  if (it != filenames.end())
97  {
98  std::string filename = (*it).toStdString();
99  ++it;
100  QString qStr = QString(filename.c_str());
101  m_Controls.m_DeformationField->setText(qStr);
102  }
103 }
104 
106 {
108  reader->SetFileName(m_Controls.m_DeformationField->text().toStdString());
109  reader->Update();
110 
111  DeformationFieldType::Pointer deformationField = reader->GetOutput();
112 
113  typedef itk::ImageRegionIterator<DeformationFieldType> IteratorType;
114  IteratorType deformIter(deformationField, deformationField->GetRequestedRegion());
115 
116  for (deformIter.GoToBegin(); !deformIter.IsAtEnd(); ++deformIter)
117  {
118  std::cout << deformIter.Get() << std::endl;
119  }
120 }
121 
123 {
124  if (m_FixedNode != nullptr && m_MovingNode != nullptr)
125  {
126  mitk::Image::Pointer fimage = dynamic_cast<mitk::Image *>(m_FixedNode->GetData());
127  mitk::Image::Pointer mimage = dynamic_cast<mitk::Image *>(m_MovingNode->GetData());
128 
130 
131  registration->SetSaveResult(false);
132  registration->SetReferenceImage(fimage);
133  registration->SetInput(mimage);
134 
135  // Read out optimizer parameters from the interface
137 
138  registration->SetNumberOfGridPoints(m_Controls.m_NumberOfGridNodes->text().toInt());
139  registration->SetOptimizerParameters(m_OptimizerParameters);
140  registration->SetUpdateInputImage(true);
141 
142  if (m_Controls.m_SaveDeformFieldCheck->isChecked())
143  {
144  // Set some parameters to save the deformation field
145  registration->SetSaveDeformationField(true);
146  registration->SetDeformationFileName(m_Controls.m_DeformationField->text().toStdString());
147  }
148 
149  try
150  {
151  registration->Update();
152  }
153  catch (itk::ExceptionObject &excpt)
154  {
155  QMessageBox::information(this, "Registration exception", excpt.GetDescription(), QMessageBox::Ok);
156  }
157 
158  mitk::Image::Pointer image = registration->GetOutput();
159 
160  if (image.IsNotNull())
161  {
162  m_MovingNode->SetData(image);
164  mitk::LevelWindow levelWindow;
165  levelWindow.SetAuto(image);
166  levWinProp->SetLevelWindow(levelWindow);
167  m_MovingNode->GetPropertyList()->SetProperty("levelwindow", levWinProp);
168  }
169 
171  }
172 }
173 
175 {
177 
178  if (m_Controls.m_OptimizerSelector->currentText() == "LBFGSOptimizer")
179  {
181  m_OptimizerParameters->SetGradientConvergenceToleranceLBFGS(m_Controls.m_GradConvTolerance->text().toFloat());
182  m_OptimizerParameters->SetLineSearchAccuracyLBFGS(m_Controls.m_LineSearchAccuracy->text().toFloat());
183  m_OptimizerParameters->SetDefaultStepLengthLBFGS(m_Controls.m_DefaultStepLength->text().toFloat());
184  m_OptimizerParameters->SetNumberOfIterationsLBFGS(m_Controls.m_FunctionEvaluations->text().toInt());
185  }
186  else if (m_Controls.m_OptimizerSelector->currentText() == "Gradient Descent")
187  {
189  m_OptimizerParameters->SetLearningRateGradientDescent(m_Controls.m_LearningRateGradientDescent->text().toFloat());
190  m_OptimizerParameters->SetNumberOfIterationsGradientDescent(
191  m_Controls.m_NumberOfIterationsGradientDescent->text().toInt());
192  }
193 }
194 
196 {
197  m_FixedNode = fixedNode;
198 }
199 
201 {
202  m_MovingNode = movingNode;
203 }
mitk::PropertyList * GetPropertyList(const mitk::BaseRenderer *renderer=nullptr) const
Get the PropertyList of the renderer. If renderer is NULL, the BaseRenderer-independent PropertyList ...
itk::SmartPointer< Self > Pointer
static Pointer New()
void SetProperty(const std::string &propertyKey, BaseProperty *property)
Set a property in the list/map by value.
Ui::QmitkBSplineRegistrationViewControls m_Controls
virtual void SetData(mitk::BaseData *baseData)
Set the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
QmitkBSplineRegistrationView(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
mitk::OptimizerParameters::Pointer m_OptimizerParameters
itk::Vector< float, 3 > VectorType
The LevelWindow class Class to store level/window values.
void SetMovingNode(mitk::DataNode *movingNode)
itk::ImageFileReader< DeformationFieldType > ImageReaderType
static RenderingManager * GetInstance()
static const std::string filename
static Pointer New()
Image class for storing images.
Definition: mitkImage.h:76
void SetAuto(const Image *image, bool tryPicTags=true, bool guessByCentralSlice=true, unsigned selectedComponent=0)
sets level/window to optimize the contrast of the given Image
itk::Image< VectorType, 3 > DeformationFieldType
void SetFixedNode(mitk::DataNode *fixedNode)
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
static Pointer New()
Method for creation through the object factory.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.