Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
Medical Imaging Interaction Toolkit
mitkMaskedAlgorithmHelper.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 
14 
15 #include <itkImageMaskSpatialObject.h>
16 
17 // Mitk
18 #include <mitkImageAccessByItk.h>
19 
20 // MatchPoint
21 #include "mapMaskedRegistrationAlgorithmInterface.h"
22 #include <mapRegistrationAlgorithmInterface.h>
23 
24 namespace mitk
25 {
26 
27  MaskedAlgorithmHelper::MaskedAlgorithmHelper(map::algorithm::RegistrationAlgorithmBase* algorithm) : m_AlgorithmBase(algorithm)
28  {
29  }
30 
31  bool MaskedAlgorithmHelper::HasMaskedRegistrationAlgorithmInterface(const map::algorithm::RegistrationAlgorithmBase* algorithm)
32  {
33  using MaskedInterface2D = const ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<2, 2>;
34  using MaskedInterface3D = const ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<3, 3>;
35 
36  return dynamic_cast<MaskedInterface2D*>(algorithm) != nullptr && dynamic_cast<MaskedInterface3D*>(algorithm) != nullptr;
37  };
38 
39  bool
41  CheckSupport(const mitk::Image* movingMask, const mitk::Image* targetMask) const
42  {
43  if (! m_AlgorithmBase) mapDefaultExceptionStaticMacro(<< "Error, cannot check data. Helper has no algorithm defined.");
44 
45  unsigned int movingDim = m_AlgorithmBase->getMovingDimensions();
46  unsigned int targetDim = m_AlgorithmBase->getTargetDimensions();
47 
48  bool result = movingDim == targetDim;
49 
50  if ( movingMask)
51  {
52  result = result && (movingMask->GetDimension() == movingDim);
53  }
54 
55  if ( targetMask)
56  {
57  result = result && (targetMask->GetDimension() == targetDim);
58  }
59 
60  if (movingDim == 2)
61  {
62  typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<2, 2> MaskedInterface;
63  const MaskedInterface* pMaskedReg = dynamic_cast<const MaskedInterface*>(m_AlgorithmBase.GetPointer());
64 
65  result = result && pMaskedReg;
66  }
67  else if (movingDim == 3)
68  {
69  typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<3, 3> MaskedInterface;
70  const MaskedInterface* pMaskedReg = dynamic_cast<const MaskedInterface*>(m_AlgorithmBase.GetPointer());
71 
72  result = result && pMaskedReg;
73  }
74  else
75  {
76  result = false;
77  }
78 
79  return result;
80  };
81 
82  bool MaskedAlgorithmHelper::SetMasks(const mitk::Image* movingMask, const mitk::Image* targetMask)
83  {
84  if (! m_AlgorithmBase) mapDefaultExceptionStaticMacro(<< "Error, cannot set data. Helper has no algorithm defined.");
85 
86  if (! CheckSupport(movingMask, targetMask)) return false;
87 
88  unsigned int movingDim = m_AlgorithmBase->getMovingDimensions();
89  unsigned int targetDim = m_AlgorithmBase->getTargetDimensions();
90 
91  if (movingDim!=targetDim) return false;
92 
93  if (movingDim == 2)
94  {
95  return DoSetMasks<2,2>(movingMask, targetMask);
96  }
97  else if (movingDim == 3)
98  {
99  return DoSetMasks<3,3>(movingMask, targetMask);
100  }
101  return false;
102  };
103 
104  template<unsigned int VMovingDimension, unsigned int VTargetDimension>
105  bool MaskedAlgorithmHelper::DoSetMasks(const mitk::Image* movingMask, const mitk::Image* targetMask)
106  {
107  typedef itk::SpatialObject<VMovingDimension> MovingSpatialType;
108  typedef itk::SpatialObject<VTargetDimension> TargetSpatialType;
109 
110  typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<VMovingDimension, VTargetDimension> MaskedRegInterface;
111  MaskedRegInterface* pAlg = dynamic_cast<MaskedRegInterface*>(m_AlgorithmBase.GetPointer());
112 
113  if (!pAlg) return false;
114 
115  if (movingMask)
116  {
117  AccessFixedDimensionByItk(movingMask, DoConvertMask, VMovingDimension);
118  typename MovingSpatialType::Pointer movingSpatial = dynamic_cast<MovingSpatialType*>(m_convertResult.GetPointer());
119  if (!movingSpatial) mapDefaultExceptionStaticMacro(<< "Error, cannot convert moving mask.");
120  pAlg->setMovingMask(movingSpatial);
121  }
122  else
123  {
124  pAlg->setMovingMask(nullptr);
125  }
126 
127  if (targetMask)
128  {
129  AccessFixedDimensionByItk(targetMask, DoConvertMask, VTargetDimension);
130  typename TargetSpatialType::Pointer targetSpatial = dynamic_cast<TargetSpatialType*>(m_convertResult.GetPointer());
131  if (! targetSpatial) mapDefaultExceptionStaticMacro(<< "Error, cannot convert moving mask.");
132  pAlg->setTargetMask(targetSpatial);
133  }
134  else
135  {
136  pAlg->setTargetMask(nullptr);
137  }
138 
139  return true;
140  }
141 
142  template<unsigned int VImageDimension>
143  typename itk::SpatialObject<VImageDimension>::Pointer
144  MaskedAlgorithmHelper::ConvertMaskSO(const itk::Image<MaskPixelType, VImageDimension>* mask) const
145  {
146  typedef itk::ImageMaskSpatialObject<VImageDimension> SpatialType;
147 
148  typename SpatialType::Pointer spatial = SpatialType::New();
149  spatial->SetImage(mask);
150 
151  return spatial.GetPointer();
152  }
153 
154  template<typename TPixelType, unsigned int VImageDimension>
155  void MaskedAlgorithmHelper::DoConvertMask(const itk::Image<TPixelType,VImageDimension>* mask)
156  {
157  using InImageType = itk::Image<TPixelType, VImageDimension>;
158  using MaskImageType = itk::Image<MaskPixelType, VImageDimension>;
159 
160  typedef itk::CastImageFilter< InImageType, MaskImageType > CastFilterType;
161  typename CastFilterType::Pointer imageCaster = CastFilterType::New();
162 
163  imageCaster->SetInput(mask);
164 
165  auto castedMask = imageCaster->GetOutput();
166  imageCaster->Update();
167  m_convertResult = ConvertMaskSO<VImageDimension>(castedMask);
168  }
169 
170  template<unsigned int VImageDimension>
171  void MaskedAlgorithmHelper::DoConvertMask(const itk::Image<MaskPixelType, VImageDimension>* mask)
172  {
173  m_convertResult = ConvertMaskSO<VImageDimension>(mask);
174  }
175 }
#define AccessFixedDimensionByItk(mitkImage, itkImageTypeFunction, dimension)
Access a mitk-image with known dimension by an itk-image.
DataCollection - Class to facilitate loading/accessing structured data.
bool CheckSupport(const mitk::Image *movingMask, const mitk::Image *targetMask) const
static bool HasMaskedRegistrationAlgorithmInterface(const map::algorithm::RegistrationAlgorithmBase *algorithm)
unsigned int GetDimension() const
Get dimension of the image.
Definition: mitkImage.cpp:106
Image class for storing images.
Definition: mitkImage.h:72
itk::Image< unsigned char, 3 > MaskImageType
Definition: CLBrainMask.cpp:32
bool SetMasks(const mitk::Image *movingMask, const mitk::Image *targetMask)
mitk::Image::Pointer mask
MaskedAlgorithmHelper(map::algorithm::RegistrationAlgorithmBase *algorithm)