Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkMRNormTwoRegionBasedFilter.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 
18 
19 #include "mitkImageToItk.h"
20 #include "mitkImageAccessByItk.h"
21 
22 #include "itkImageRegionIterator.h"
23 // MITK
24 #include <mitkITKImageImport.h>
25 #include <mitkImageCast.h>
26 #include <mitkImageAccessByItk.h>
27 // ITK
28 #include <itkLabelStatisticsImageFilter.h>
29 #include <itkMinimumMaximumImageCalculator.h>
30 
32  m_Area1(MRNormTwoRegionsBasedFilter::MEDIAN),
33  m_Area2(MRNormTwoRegionsBasedFilter::MEDIAN)
34 {
35  this->SetNumberOfIndexedInputs(3);
36  this->SetNumberOfRequiredInputs(1);
37 }
38 
40 {
41 }
42 
44 {
45  // Process object is not const-correct so the const_cast is required here
46  Image* nonconstMask = const_cast< mitk::Image * >( mask );
47  this->SetNthInput(1, nonconstMask );
48 }
49 
51 {
52  // Process object is not const-correct so the const_cast is required here
53  Image* nonconstMask = const_cast< mitk::Image * >( mask );
54  this->SetNthInput(2, nonconstMask );
55 }
56 
58 {
59  return this->GetInput(1);
60 }
61 
63 {
64  return this->GetInput(2);
65 }
66 
68 {
69  Superclass::GenerateInputRequestedRegion();
70 
71  mitk::Image* input = const_cast< mitk::Image * > ( this->GetInput() );
72 
74 }
75 
77 {
78  mitk::Image::ConstPointer input = this->GetInput();
79  mitk::Image::Pointer output = this->GetOutput();
80 
81  itkDebugMacro(<<"GenerateOutputInformation()");
82 
83  output->Initialize(input->GetPixelType(), *input->GetTimeGeometry());
84  output->SetPropertyList(input->GetPropertyList()->Clone());
85 }
86 
87 template < typename TPixel, unsigned int VImageDimension >
88 void mitk::MRNormTwoRegionsBasedFilter::InternalComputeMask(itk::Image<TPixel, VImageDimension>* itkImage)
89 {
90  // Define all necessary Types
91  typedef itk::Image<TPixel, VImageDimension> ImageType;
92  typedef itk::Image<int, VImageDimension> MaskType;
93  typedef itk::LabelStatisticsImageFilter<ImageType, MaskType> FilterType;
94  typedef itk::MinimumMaximumImageCalculator<ImageType> MinMaxComputerType;
95 
96  typename MaskType::Pointer itkMask0 = MaskType::New();
97  typename MaskType::Pointer itkMask1 = MaskType::New();
98  mitk::CastToItkImage(this->GetMask1(), itkMask0);
99  mitk::CastToItkImage(this->GetMask2(), itkMask1);
100 
101  typename ImageType::Pointer outImage = ImageType::New();
102  mitk::CastToItkImage(this->GetOutput(0), outImage);
103 
104  typename MinMaxComputerType::Pointer minMaxComputer = MinMaxComputerType::New();
105  minMaxComputer->SetImage(itkImage);
106  minMaxComputer->Compute();
107 
108  typename FilterType::Pointer labelStatisticsImageFilter = FilterType::New();
109  labelStatisticsImageFilter->SetUseHistograms(true);
110  labelStatisticsImageFilter->SetHistogramParameters(256, minMaxComputer->GetMinimum(),minMaxComputer->GetMaximum());
111  labelStatisticsImageFilter->SetInput( itkImage );
112 
113  labelStatisticsImageFilter->SetLabelInput(itkMask0);
114  labelStatisticsImageFilter->Update();
115  double median0 = labelStatisticsImageFilter->GetMedian(1);
116  double mean0 = labelStatisticsImageFilter->GetMean(1);
117  double modulo0=0;
118  {
119  auto histo = labelStatisticsImageFilter->GetHistogram(1);
120  double maxFrequency=0;
121  for (auto hIter=histo->Begin();hIter!=histo->End();++hIter)
122  {
123  if (maxFrequency < hIter.GetFrequency())
124  {
125  maxFrequency = hIter.GetFrequency();
126  modulo0 = (histo->GetBinMin(0,hIter.GetInstanceIdentifier()) + histo->GetBinMax(0,hIter.GetInstanceIdentifier())) / 2.0;
127  }
128  }
129  }
130  labelStatisticsImageFilter->SetLabelInput(itkMask1);
131  labelStatisticsImageFilter->Update();
132  double median1 = labelStatisticsImageFilter->GetMedian(1);
133  double mean1 = labelStatisticsImageFilter->GetMean(1);
134  double modulo1 = 0;
135  {
136  auto histo = labelStatisticsImageFilter->GetHistogram(1);
137  double maxFrequency=0;
138  for (auto hIter=histo->Begin();hIter!=histo->End();++hIter)
139  {
140  if (maxFrequency < hIter.GetFrequency())
141  {
142  maxFrequency = hIter.GetFrequency();
143  modulo1 = (histo->GetBinMin(0,hIter.GetInstanceIdentifier()) + histo->GetBinMax(0,hIter.GetInstanceIdentifier())) / 2.0;
144  }
145  }
146  }
147 
148  double value0=0;
149  double value1=0;
150  switch (m_Area1)
151  {
153  value0=mean0; break;
155  value0=median0; break;
157  value0=modulo0; break;
158  }
159  switch (m_Area2)
160  {
162  value1=mean1; break;
164  value1=median1; break;
166  value1=modulo1; break;
167  }
168 
169  double offset = std::min(value0, value1);
170  double scaling = std::max(value0, value1) - offset;
171  if (scaling < 0.0001)
172  return;
173 
174  itk::ImageRegionIterator<ImageType> inIter(itkImage, itkImage->GetLargestPossibleRegion());
175  itk::ImageRegionIterator<ImageType> outIter(outImage, outImage->GetLargestPossibleRegion());
176  while (! inIter.IsAtEnd())
177  {
178  TPixel value = inIter.Value();
179  outIter.Set((value - offset) / scaling);
180  ++inIter;
181  ++outIter;
182  }
183 }
184 
186 {
187  AccessByItk(GetInput(0),InternalComputeMask);
188 }
virtual void SetRequestedRegionToLargestPossibleRegion() override
void InternalComputeMask(itk::Image< TPixel, VImageDimension > *itkImage)
itk::SmartPointer< Self > Pointer
static Vector3D offset
virtual void GenerateData() override
A version of GenerateData() specific for image processing filters.
map::core::discrete::Elements< 3 >::InternalImageType ImageType
Image class for storing images.
Definition: mitkImage.h:76
#define AccessByItk(mitkImage, itkImageTypeFunction)
Access a MITK image by an ITK image.
static T max(T x, T y)
Definition: svm.cpp:70
static T min(T x, T y)
Definition: svm.cpp:67
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.