Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkMRNormLinearStatisticBasedFilter.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_CenterMode(MRNormLinearStatisticBasedFilter::MEDIAN)
33 {
34  this->SetNumberOfIndexedInputs(3);
35  this->SetNumberOfRequiredInputs(3);
36 }
37 
39 {
40 }
41 
43 {
44  // Process object is not const-correct so the const_cast is required here
45  Image* nonconstMask = const_cast< mitk::Image * >( mask );
46  this->SetNthInput(1, nonconstMask );
47 }
48 
50 {
51  return this->GetInput(1);
52 }
53 
55 {
56  Superclass::GenerateInputRequestedRegion();
57 
58  mitk::Image* input = const_cast< mitk::Image * > ( this->GetInput() );
59 
61 }
62 
64 {
65  mitk::Image::ConstPointer input = this->GetInput();
66  mitk::Image::Pointer output = this->GetOutput();
67 
68  itkDebugMacro(<<"GenerateOutputInformation()");
69 
70  output->Initialize(input->GetPixelType(), *input->GetTimeGeometry());
71  output->SetPropertyList(input->GetPropertyList()->Clone());
72 }
73 
74 template < typename TPixel, unsigned int VImageDimension >
75 void mitk::MRNormLinearStatisticBasedFilter::InternalComputeMask(itk::Image<TPixel, VImageDimension>* itkImage)
76 {
77  // Define all necessary Types
78  typedef itk::Image<TPixel, VImageDimension> ImageType;
79  typedef itk::Image<int, VImageDimension> MaskType;
80  typedef itk::LabelStatisticsImageFilter<ImageType, MaskType> FilterType;
81  typedef itk::MinimumMaximumImageCalculator<ImageType> MinMaxComputerType;
82 
83  typename MaskType::Pointer itkMask0 = MaskType::New();
84  mitk::CastToItkImage(this->GetMask(), itkMask0);
85 
86  typename ImageType::Pointer outImage = ImageType::New();
87  mitk::CastToItkImage(this->GetOutput(0), outImage);
88 
89  typename MinMaxComputerType::Pointer minMaxComputer = MinMaxComputerType::New();
90  minMaxComputer->SetImage(itkImage);
91  minMaxComputer->Compute();
92 
93  typename FilterType::Pointer labelStatisticsImageFilter = FilterType::New();
94  labelStatisticsImageFilter->SetUseHistograms(true);
95  labelStatisticsImageFilter->SetHistogramParameters(256, minMaxComputer->GetMinimum(),minMaxComputer->GetMaximum());
96  labelStatisticsImageFilter->SetInput( itkImage );
97 
98  labelStatisticsImageFilter->SetLabelInput(itkMask0);
99  labelStatisticsImageFilter->Update();
100  double median0 = labelStatisticsImageFilter->GetMedian(1);
101  double mean0 = labelStatisticsImageFilter->GetMean(1);
102  double stddev = labelStatisticsImageFilter->GetSigma(1);
103  double modulo0=0;
104  {
105  auto histo = labelStatisticsImageFilter->GetHistogram(1);
106  double maxFrequency=0;
107  for (auto hIter=histo->Begin();hIter!=histo->End();++hIter)
108  {
109  if (maxFrequency < hIter.GetFrequency())
110  {
111  maxFrequency = hIter.GetFrequency();
112  modulo0 = (histo->GetBinMin(0,hIter.GetInstanceIdentifier()) + histo->GetBinMax(0,hIter.GetInstanceIdentifier())) / 2.0;
113  }
114  }
115  }
116 
117  double value0=0;
118  switch (m_CenterMode)
119  {
121  value0=mean0; break;
123  value0=median0; break;
125  value0=modulo0; break;
126  }
127 
128  double offset = value0;
129  double scaling = stddev;
130  if (scaling < 0.0001)
131  return;
132 
133  itk::ImageRegionIterator<ImageType> inIter(itkImage, itkImage->GetLargestPossibleRegion());
134  itk::ImageRegionIterator<ImageType> outIter(outImage, outImage->GetLargestPossibleRegion());
135  while (! inIter.IsAtEnd())
136  {
137  TPixel value = inIter.Value();
138  outIter.Set((value - offset) / scaling);
139  ++inIter;
140  ++outIter;
141  }
142 }
143 
145 {
146  AccessByItk(GetInput(0),InternalComputeMask);
147 }
virtual void SetRequestedRegionToLargestPossibleRegion() override
itk::SmartPointer< Self > Pointer
virtual void GenerateData() override
A version of GenerateData() specific for image processing filters.
void InternalComputeMask(itk::Image< TPixel, VImageDimension > *itkImage)
static Vector3D offset
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.
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.