Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkMaskImageFilter.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 "mitkMaskImageFilter.h"
14 #include "mitkImageTimeSelector.h"
15 #include "mitkProperties.h"
16 #include "mitkTimeHelper.h"
17 
18 #include "mitkImageAccessByItk.h"
19 #include "mitkImageToItk.h"
20 
21 #include "itkImageRegionConstIterator.h"
22 #include "itkImageRegionIteratorWithIndex.h"
23 
24 #include <limits>
25 
27 {
28  this->SetNumberOfIndexedInputs(2);
29  this->SetNumberOfRequiredInputs(2);
33  m_OverrideOutsideValue = false;
34  m_OutsideValue = 0;
35 }
36 
38 {
39 }
40 
42 {
43  // Process object is not const-correct so the const_cast is required here
44  m_Mask = const_cast<mitk::Image *>(mask);
45  this->ProcessObject::SetNthInput(1, m_Mask);
46 }
47 
49 {
50  return m_Mask;
51 }
52 
54 {
55  Superclass::GenerateInputRequestedRegion();
56 
57  mitk::Image *output = this->GetOutput();
58  mitk::Image *input = this->GetInput();
60  if ((output->IsInitialized() == false) || (mask == nullptr) || (mask->GetTimeGeometry()->CountTimeSteps() == 0))
61  return;
62 
65 
66  GenerateTimeInInputRegion(output, input);
67  GenerateTimeInInputRegion(output, mask);
68 }
69 
71 {
72  mitk::Image::ConstPointer input = this->GetInput();
73  mitk::Image::Pointer output = this->GetOutput();
74 
75  if ((output->IsInitialized()) && (this->GetMTime() <= m_TimeOfHeaderInitialization.GetMTime()))
76  return;
77 
78  itkDebugMacro(<< "GenerateOutputInformation()");
79 
80  output->Initialize(input->GetPixelType(), *input->GetTimeGeometry());
81 
82  output->SetPropertyList(input->GetPropertyList()->Clone());
83 
85 }
86 
87 template <typename TPixel, unsigned int VImageDimension>
88 void mitk::MaskImageFilter::InternalComputeMask(itk::Image<TPixel, VImageDimension> *inputItkImage)
89 {
90  // dirty quick fix, duplicating code so both unsigned char and unsigned short are supported
91  // this should be changed once unsigned char segmentations can be converted to unsigned short
92  mitk::PixelType pixelType =
93  m_MaskTimeSelector->GetOutput()->GetImageDescriptor()->GetChannelDescriptor().GetPixelType();
94  if (pixelType.GetComponentType() == itk::ImageIOBase::UCHAR)
95  {
96  typedef itk::Image<TPixel, VImageDimension> ItkInputImageType;
97  typedef itk::Image<unsigned char, VImageDimension> ItkMaskImageType;
98  typedef itk::Image<TPixel, VImageDimension> ItkOutputImageType;
99 
100  typedef itk::ImageRegionConstIterator<ItkInputImageType> ItkInputImageIteratorType;
101  typedef itk::ImageRegionConstIterator<ItkMaskImageType> ItkMaskImageIteratorType;
102  typedef itk::ImageRegionIteratorWithIndex<ItkOutputImageType> ItkOutputImageIteratorType;
103 
105  maskimagetoitk->SetInput(m_MaskTimeSelector->GetOutput());
106  maskimagetoitk->Update();
107  typename ItkMaskImageType::Pointer maskItkImage = maskimagetoitk->GetOutput();
108 
109  typename mitk::ImageToItk<ItkOutputImageType>::Pointer outputimagetoitk =
111  outputimagetoitk->SetInput(m_OutputTimeSelector->GetOutput());
112  outputimagetoitk->Update();
113  typename ItkOutputImageType::Pointer outputItkImage = outputimagetoitk->GetOutput();
114 
115  // create the iterators
116  typename ItkInputImageType::RegionType inputRegionOfInterest = inputItkImage->GetLargestPossibleRegion();
117  ItkInputImageIteratorType inputIt(inputItkImage, inputRegionOfInterest);
118  ItkMaskImageIteratorType maskIt(maskItkImage, inputRegionOfInterest);
119  ItkOutputImageIteratorType outputIt(outputItkImage, inputRegionOfInterest);
120 
121  // typename ItkOutputImageType::PixelType outsideValue = itk::NumericTraits<typename
122  // ItkOutputImageType::PixelType>::min();
125 
128 
129  for (inputIt.GoToBegin(), maskIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd() && !maskIt.IsAtEnd();
130  ++inputIt, ++maskIt, ++outputIt)
131  {
132  if (maskIt.Get() > itk::NumericTraits<typename ItkMaskImageType::PixelType>::Zero)
133  {
134  outputIt.Set(inputIt.Get());
135  m_MinValue = vnl_math_min((float)inputIt.Get(), (float)m_MinValue);
136  m_MaxValue = vnl_math_max((float)inputIt.Get(), (float)m_MaxValue);
137  }
138  else
139  {
140  outputIt.Set(m_OutsideValue);
141  }
142  }
143  }
144  else
145  {
146  {
147  typedef itk::Image<TPixel, VImageDimension> ItkInputImageType;
148  typedef itk::Image<unsigned short, VImageDimension> ItkMaskImageType;
149  typedef itk::Image<TPixel, VImageDimension> ItkOutputImageType;
150 
151  typedef itk::ImageRegionConstIterator<ItkInputImageType> ItkInputImageIteratorType;
152  typedef itk::ImageRegionConstIterator<ItkMaskImageType> ItkMaskImageIteratorType;
153  typedef itk::ImageRegionIteratorWithIndex<ItkOutputImageType> ItkOutputImageIteratorType;
154 
156  maskimagetoitk->SetInput(m_MaskTimeSelector->GetOutput());
157  maskimagetoitk->Update();
158  typename ItkMaskImageType::Pointer maskItkImage = maskimagetoitk->GetOutput();
159 
160  typename mitk::ImageToItk<ItkOutputImageType>::Pointer outputimagetoitk =
162  outputimagetoitk->SetInput(m_OutputTimeSelector->GetOutput());
163  outputimagetoitk->Update();
164  typename ItkOutputImageType::Pointer outputItkImage = outputimagetoitk->GetOutput();
165 
166  // create the iterators
167  typename ItkInputImageType::RegionType inputRegionOfInterest = inputItkImage->GetLargestPossibleRegion();
168  ItkInputImageIteratorType inputIt(inputItkImage, inputRegionOfInterest);
169  ItkMaskImageIteratorType maskIt(maskItkImage, inputRegionOfInterest);
170  ItkOutputImageIteratorType outputIt(outputItkImage, inputRegionOfInterest);
171 
172  // typename ItkOutputImageType::PixelType outsideValue = itk::NumericTraits<typename
173  // ItkOutputImageType::PixelType>::min();
176 
179 
180  for (inputIt.GoToBegin(), maskIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd() && !maskIt.IsAtEnd();
181  ++inputIt, ++maskIt, ++outputIt)
182  {
183  if (maskIt.Get() > itk::NumericTraits<typename ItkMaskImageType::PixelType>::Zero)
184  {
185  outputIt.Set(inputIt.Get());
186  m_MinValue = vnl_math_min((float)inputIt.Get(), (float)m_MinValue);
187  m_MaxValue = vnl_math_max((float)inputIt.Get(), (float)m_MaxValue);
188  }
189  else
190  {
191  outputIt.Set(m_OutsideValue);
192  }
193  }
194  }
195  }
196 }
197 
199 {
200  mitk::Image::ConstPointer input = this->GetInput();
202  mitk::Image::Pointer output = this->GetOutput();
203 
204  if ((output->IsInitialized() == false) || (mask.IsNull()) || (mask->GetTimeGeometry()->CountTimeSteps() == 0))
205  return;
206 
207  m_InputTimeSelector->SetInput(input);
208  m_MaskTimeSelector->SetInput(mask);
209  m_OutputTimeSelector->SetInput(this->GetOutput());
210 
211  mitk::Image::RegionType outputRegion = output->GetRequestedRegion();
212  const mitk::TimeGeometry *outputTimeGeometry = output->GetTimeGeometry();
213  const mitk::TimeGeometry *inputTimeGeometry = input->GetTimeGeometry();
214  const mitk::TimeGeometry *maskTimeGeometry = mask->GetTimeGeometry();
215  ScalarType timeInMS;
216 
217  int timestep = 0;
218  int tstart = outputRegion.GetIndex(3);
219  int tmax = tstart + outputRegion.GetSize(3);
220 
221  int t;
222  for (t = tstart; t < tmax; ++t)
223  {
224  timeInMS = outputTimeGeometry->TimeStepToTimePoint(t);
225 
226  timestep = inputTimeGeometry->TimePointToTimeStep(timeInMS);
227 
228  m_InputTimeSelector->SetTimeNr(timestep);
229  m_InputTimeSelector->UpdateLargestPossibleRegion();
230  m_OutputTimeSelector->SetTimeNr(t);
231  m_OutputTimeSelector->UpdateLargestPossibleRegion();
232 
233  timestep = maskTimeGeometry->TimePointToTimeStep(timeInMS);
234  m_MaskTimeSelector->SetTimeNr(timestep);
235  m_MaskTimeSelector->UpdateLargestPossibleRegion();
236 
238  }
239 
240  m_TimeOfHeaderInitialization.Modified();
241 }
void SetRequestedRegionToLargestPossibleRegion() override
virtual TimeStepType CountTimeSteps() const =0
Returns the number of time steps.
double ScalarType
mitk::ScalarType m_MaxValue
int GetComponentType() const
Get the component type (the scalar (!) type). Each element may contain m_NumberOfComponents (more tha...
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
Definition: mitkBaseData.h:61
mitk::Image::Pointer m_Mask
mitk::ScalarType m_MinValue
itk::ImageRegion< RegionDimension > RegionType
virtual TimeStepType TimePointToTimeStep(TimePointType timePoint) const =0
Converts a time point to the corresponding time step.
Image class for storing images.
Definition: mitkImage.h:72
virtual TimePointType TimeStepToTimePoint(TimeStepType timeStep) const =0
Converts a time step to a time point.
#define AccessByItk(mitkImage, itkImageTypeFunction)
Access a MITK image by an ITK image.
static T max(T x, T y)
Definition: svm.cpp:56
void GenerateTimeInInputRegion(const mitk::TimeGeometry *outputTimeGeometry, const TOutputRegion &outputRegion, const mitk::TimeGeometry *inputTimeGeometry, TInputRegion &inputRegion)
static T min(T x, T y)
Definition: svm.cpp:53
static Pointer New()
void SetMask(const mitk::Image *mask)
InputImageType * GetInput(void)
virtual bool IsInitialized() const
Check whether the data has been initialized, i.e., at least the Geometry and other header data has be...
mitk::Image::Pointer mask
OutputType * GetOutput()
Get the output data of this image source object.
void InternalComputeMask(itk::Image< TPixel, VImageDimension > *itkImage)
itk::TimeStamp m_TimeOfHeaderInitialization
const mitk::Image * GetMask() const
void GenerateOutputInformation() override
mitk::ImageTimeSelector::Pointer m_InputTimeSelector
mitk::ScalarType m_OutsideValue
mitk::ImageTimeSelector::Pointer m_MaskTimeSelector
static Pointer New()
void GenerateInputRequestedRegion() override
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51
mitk::ImageTimeSelector::Pointer m_OutputTimeSelector