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