Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkTimeFramesRegistrationHelper.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "itkCommand.h"
14 
16 #include <mitkImageTimeSelector.h>
17 #include <mitkImageReadAccessor.h>
18 
20 #include <mitkAlgorithmHelper.h>
21 
24  mitk::TimePointType timePoint) const
25 {
27  imageTimeSelector->SetInput(image);
28  imageTimeSelector->SetTimeNr(timePoint);
29  imageTimeSelector->UpdateLargestPossibleRegion();
30 
31  mitk::Image::Pointer frameImage = imageTimeSelector->GetOutput();
32  return frameImage;
33 };
34 
35 void
37 {
39 
40  //prepare processing
41  mitk::Image::Pointer targetFrame = GetFrameImage(this->m_4DImage, 0);
42 
43  this->m_Registered4DImage = this->m_4DImage->Clone();
44 
46 
47  if (m_TargetMask.IsNotNull())
48  {
49  if (m_TargetMask->GetTimeSteps() > 1)
50  {
51  mask = GetFrameImage(m_TargetMask, 0);
52  }
53  else
54  {
55  mask = m_TargetMask;
56  }
57  }
58 
59  double progressDelta = 1.0 / ((this->m_4DImage->GetTimeSteps() - 1) * 3.0);
60  m_Progress = 0.0;
61 
62  //process the frames
63  for (unsigned int i = 1; i < this->m_4DImage->GetTimeSteps(); ++i)
64  {
65  Image::Pointer movingFrame = GetFrameImage(this->m_4DImage, i);
66  Image::Pointer mappedFrame;
67 
68  IgnoreListType::iterator finding = std::find(m_IgnoreList.begin(), m_IgnoreList.end(), i);
69 
70 
71  if (finding == m_IgnoreList.end())
72  {
73  //frame should be processed
74  RegistrationPointer reg = DoFrameRegistration(movingFrame, targetFrame, mask);
75 
76  m_Progress += progressDelta;
77  this->InvokeEvent(::mitk::FrameRegistrationEvent(nullptr,
78  "Registred frame #" +::map::core::convert::toStr(i)));
79 
80  mappedFrame = DoFrameMapping(movingFrame, reg, targetFrame);
81 
82  m_Progress += progressDelta;
83  this->InvokeEvent(::mitk::FrameMappingEvent(nullptr,
84  "Mapped frame #" + ::map::core::convert::toStr(i)));
85 
86  mitk::ImageReadAccessor accessor(mappedFrame, mappedFrame->GetVolumeData(0, 0, nullptr,
88 
89 
90  this->m_Registered4DImage->SetVolume(accessor.GetData(), i);
91  this->m_Registered4DImage->GetTimeGeometry()->SetTimeStepGeometry(mappedFrame->GetGeometry(), i);
92 
93  m_Progress += progressDelta;
94  }
95  else
96  {
97  m_Progress += 3 * progressDelta;
98  }
99 
100  this->InvokeEvent(::itk::ProgressEvent());
101 
102  }
103 
104 };
105 
108 {
109  if (this->HasOutdatedResult())
110  {
111  Generate();
112  }
113 
114  return m_Registered4DImage;
115 };
116 
117 void
120 {
121  m_IgnoreList = il;
122  this->Modified();
123 }
124 
125 void
127 {
128  m_IgnoreList.clear();
129  this->Modified();
130 };
131 
132 
135  const mitk::Image* targetFrame, const mitk::Image* targetMask) const
136 {
138  algHelper.SetAllowImageCasting(true);
139  algHelper.SetData(movingFrame, targetFrame);
140 
141  if (targetMask)
142  {
144  maskHelper.SetMasks(nullptr, targetMask);
145  }
146 
147  return algHelper.GetRegistration();
148 };
149 
151  const mitk::Image* movingFrame, const RegistrationType* reg, const mitk::Image* targetFrame) const
152 {
153  return mitk::ImageMappingHelper::map(movingFrame, reg, !m_AllowUndefPixels, m_PaddingValue,
154  targetFrame->GetGeometry(), !m_AllowUnregPixels, m_ErrorValue, m_InterpolatorType);
155 };
156 
157 bool
159 {
160  if (m_Registered4DImage.IsNull())
161  {
162  return true;
163  }
164 
165  bool result = false;
166 
167  if (m_Registered4DImage->GetMTime() > this->GetMTime())
168  {
169  result = true;
170  }
171 
172 
173  if (m_Algorithm.IsNotNull())
174  {
175  if (m_Algorithm->GetMTime() > this->GetMTime())
176  {
177  result = true;
178  }
179  }
180 
181  if (m_4DImage.IsNotNull())
182  {
183  if (m_4DImage->GetMTime() > this->GetMTime())
184  {
185  result = true;
186  }
187  }
188 
189  if (m_TargetMask.IsNotNull())
190  {
191  if (m_TargetMask->GetMTime() > this->GetMTime())
192  {
193  result = true;
194  }
195  }
196 
197  return result;
198 };
199 
200 void
202 {
203  if (m_4DImage.IsNull())
204  {
205  mitkThrow() << "Cannot register image. Input 4D image is not set.";
206  }
207 
208  if (m_Algorithm.IsNull())
209  {
210  mitkThrow() << "Cannot register image. Algorithm is not set.";
211  }
212 
213  if (m_4DImage->GetTimeSteps() <= 1)
214  {
215  mitkThrow() << "Cannot register image. Input 4D image must have 2 or more time steps.";
216  }
217 
218  for (IgnoreListType::const_iterator pos = this->m_IgnoreList.begin();
219  pos != this->m_IgnoreList.end(); ++pos)
220  {
221  if (*pos >= m_4DImage->GetTimeSteps())
222  {
223  mitkThrow() <<
224  "Cannot register image. Ignore list containes at least one inexistant frame. Invalid frame index: "
225  << *pos;
226  }
227  }
228 };
229 
230 double
232 {
233  return m_Progress;
234 };
void SetAllowImageCasting(bool allowCasting)
MITKAlgorithmHelper.
map::core::RegistrationBase::Pointer GetRegistration() const
mitk::Image::Pointer GetFrameImage(const mitk::Image *image, mitk::TimePointType timePoint) const
mitk::Image::Pointer DoFrameMapping(const mitk::Image *movingFrame, const RegistrationType *reg, const mitk::Image *targetFrame) const
void SetData(const mitk::BaseData *moving, const mitk::BaseData *target)
#define mitkThrow()
Image class for storing images.
Definition: mitkImage.h:72
mitk::ScalarType TimePointType
mitk::Image::Pointer image
MITKMATCHPOINTREGISTRATION_EXPORT ResultImageType::Pointer map(const InputImageType *input, const RegistrationType *registration, bool throwOnOutOfInputAreaError=false, const double &paddingValue=0, const ResultImageGeometryType *resultGeometry=nullptr, bool throwOnMappingError=true, const double &errorValue=0, mitk::ImageMappingInterpolator::Type interpolatorType=mitk::ImageMappingInterpolator::Linear)
bool SetMasks(const mitk::Image *movingMask, const mitk::Image *targetMask)
RegistrationPointer DoFrameRegistration(const mitk::Image *movingFrame, const mitk::Image *targetFrame, const mitk::Image *targetMask) const
MaskedAlgorithmHelper Helper class as an easy bridge to set mitk images as masks for registration alg...
mitk::Image::Pointer mask
ImageReadAccessor class to get locked read access for a particular image part.
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
Definition: mitkBaseData.h:138
std::vector< mitk::TimeStepType > IgnoreListType
static Pointer New()