Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
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  if (movingDim == 2)
55  {
56  typedef itk::Image<MaskPixelType,2> MaskImageType;
57  mitk::PixelType maskPixelType = mitk::MakePixelType<MaskImageType>();
58 
59  result = result && (maskPixelType == movingMask->GetPixelType());
60  }
61  else if (movingDim == 3)
62  {
63  typedef itk::Image<MaskPixelType,3> MaskImageType;
64  mitk::PixelType maskPixelType = mitk::MakePixelType<MaskImageType>();
65 
66  result = result && (maskPixelType == movingMask->GetPixelType());
67  }
68  }
69 
70  if ( targetMask)
71  {
72  result = result && (targetMask->GetDimension() == targetDim);
73 
74  if (movingDim == 2)
75  {
76  typedef itk::Image<MaskPixelType,2> MaskImageType;
77  mitk::PixelType maskPixelType = mitk::MakePixelType<MaskImageType>();
78 
79  result = result && (maskPixelType == targetMask->GetPixelType());
80  }
81  else if (movingDim == 3)
82  {
83  typedef itk::Image<MaskPixelType,3> MaskImageType;
84  mitk::PixelType maskPixelType = mitk::MakePixelType<MaskImageType>();
85 
86  result = result && (maskPixelType == targetMask->GetPixelType());
87  }
88  }
89 
90  if (movingDim == 2)
91  {
92  typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<2, 2> MaskedInterface;
93  const MaskedInterface* pMaskedReg = dynamic_cast<const MaskedInterface*>(m_AlgorithmBase.GetPointer());
94 
95  result = result && pMaskedReg;
96  }
97  else if (movingDim == 3)
98  {
99  typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<3, 3> MaskedInterface;
100  const MaskedInterface* pMaskedReg = dynamic_cast<const MaskedInterface*>(m_AlgorithmBase.GetPointer());
101 
102  result = result && pMaskedReg;
103  }
104  else
105  {
106  result = false;
107  }
108 
109  return result;
110  };
111 
112  bool MaskedAlgorithmHelper::SetMasks(const mitk::Image* movingMask, const mitk::Image* targetMask)
113  {
114  if (! m_AlgorithmBase) mapDefaultExceptionStaticMacro(<< "Error, cannot set data. Helper has no algorithm defined.");
115 
116  if (! CheckSupport(movingMask, targetMask)) return false;
117 
118  unsigned int movingDim = m_AlgorithmBase->getMovingDimensions();
119  unsigned int targetDim = m_AlgorithmBase->getTargetDimensions();
120 
121  if (movingDim!=targetDim) return false;
122 
123  if (movingDim == 2)
124  {
125  return DoSetMasks<2,2>(movingMask, targetMask);
126  }
127  else if (movingDim == 3)
128  {
129  return DoSetMasks<3,3>(movingMask, targetMask);
130  }
131  return false;
132  };
133 
134  template<unsigned int VImageDimension1, unsigned int VImageDimension2>
135  bool MaskedAlgorithmHelper::DoSetMasks(const mitk::Image* movingMask, const mitk::Image* targetMask)
136  {
137  typedef itk::SpatialObject<VImageDimension1> MovingSpatialType;
138  typedef itk::SpatialObject<VImageDimension2> TargetSpatialType;
139 
140  typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<VImageDimension1, VImageDimension2> MaskedRegInterface;
141  MaskedRegInterface* pAlg = dynamic_cast<MaskedRegInterface*>(m_AlgorithmBase.GetPointer());
142 
143  if (!pAlg) return false;
144 
145  if (movingMask)
146  {
147  AccessFixedTypeByItk(movingMask, DoConvertMask, (MaskPixelType), (VImageDimension1));
148  typename MovingSpatialType::Pointer movingSpatial = dynamic_cast<MovingSpatialType*>(m_convertResult.GetPointer());
149  if (! movingSpatial) mapDefaultExceptionStaticMacro(<< "Error, cannot convert moving mask.");
150  pAlg->setMovingMask(movingSpatial);
151  }
152 
153  if (targetMask)
154  {
155  AccessFixedTypeByItk(targetMask, DoConvertMask, (MaskPixelType), (VImageDimension2));
156  typename TargetSpatialType::Pointer targetSpatial = dynamic_cast<TargetSpatialType*>(m_convertResult.GetPointer());
157  if (! targetSpatial) mapDefaultExceptionStaticMacro(<< "Error, cannot convert moving mask.");
158  pAlg->setTargetMask(targetSpatial);
159  }
160 
161  return true;
162  }
163 
164  template<typename TPixelType, unsigned int VImageDimension>
165  void MaskedAlgorithmHelper::DoConvertMask(const itk::Image<TPixelType,VImageDimension>* mask)
166  {
167  typedef itk::ImageMaskSpatialObject<VImageDimension> SpatialType;
168 
169  typename SpatialType::Pointer spatial = SpatialType::New();
170  spatial->SetImage(mask);
171 
172  m_convertResult = spatial.GetPointer();
173  }
174 
175 }
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Definition: mitkImage.cpp:101
DataCollection - Class to facilitate loading/accessing structured data.
bool CheckSupport(const mitk::Image *movingMask, const mitk::Image *targetMask) const
#define AccessFixedTypeByItk(mitkImage, itkImageTypeFunction, pixelTypeSeq, dimSeq)
Access a mitk-image with known type (pixel type and dimension) by an itk-image.
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)
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51