Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkCompareImageDataFilter.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 
17 // mitk includes
19 #include "mitkITKImageImport.h"
20 #include "mitkImageAccessByItk.h"
21 #include "mitkImageCaster.h"
23 
24 // itk includes
25 #include <itkTestingComparisonImageFilter.h>
26 
28 {
29  this->SetNumberOfRequiredInputs(2);
30 
32 }
33 
35 {
36  m_CompareDetails.m_MaximumDifference = 0.0f;
37  m_CompareDetails.m_MinimumDifference = itk::NumericTraits<double>::max();
38  m_CompareDetails.m_MeanDifference = 0.0f;
39  m_CompareDetails.m_TotalDifference = 0.0f;
40  m_CompareDetails.m_PixelsWithDifference = 0;
41  m_CompareDetails.m_FilterCompleted = false;
42  m_CompareDetails.m_ExceptionMessage = "";
43 }
44 
46 {
47  // check inputs
48 
49  const mitk::Image *input1 = this->GetInput(0);
50  const mitk::Image *input2 = this->GetInput(1);
51 
52  // Generally this filter is part of the mitk::Image::Equal() method and only checks the equality of the image data
53  // so no further image type comparison is performed!
54  // CAVE: If the images differ in a parameter other then the image data, the filter may fail!!
55 
56  // check what number of components the inputs have
57  if (input1->GetPixelType().GetNumberOfComponents() == 1 && input2->GetPixelType().GetNumberOfComponents() == 1)
58  {
59  AccessByItk_1(input1, EstimateValueDifference, input2);
60  }
61  else if (input1->GetPixelType().GetNumberOfComponents() > 1 && input2->GetPixelType().GetNumberOfComponents() > 1)
62  {
63  this->ResetCompareResultsToInitial();
64 
66  mcComparator->SetTestImage(input1);
67  mcComparator->SetValidImage(input2);
68  mcComparator->SetCompareFilterResult(&m_CompareDetails);
69  mcComparator->SetTolerance(m_Tolerance);
70  mcComparator->Update();
71 
72  m_CompareResult = mcComparator->GetResult();
73  }
74 }
75 
76 bool mitk::CompareImageDataFilter::GetResult(size_t threshold)
77 {
78  if (!m_CompareResult)
79  {
80  return false;
81  }
82 
83  if (m_CompareDetails.m_PixelsWithDifference > threshold)
84  {
85  return false;
86  }
87 
88  return true;
89 }
90 
91 template <typename TPixel, unsigned int VImageDimension>
92 void mitk::CompareImageDataFilter::EstimateValueDifference(const itk::Image<TPixel, VImageDimension> *itkImage1,
93  const mitk::Image *referenceImage)
94 {
95  typedef itk::Image<TPixel, VImageDimension> InputImageType;
96  typedef itk::Image<double, VImageDimension> OutputImageType;
97 
98  typename InputImageType::Pointer itk_reference = InputImageType::New();
99 
100  mitk::CastToItkImage(referenceImage, itk_reference);
101 
102  typedef itk::Testing::ComparisonImageFilter<InputImageType, OutputImageType> CompareFilterType;
103  typename CompareFilterType::Pointer compare_filter = CompareFilterType::New();
104  compare_filter->SetTestInput(itkImage1);
105  compare_filter->SetValidInput(itk_reference);
106  compare_filter->SetDifferenceThreshold(m_Tolerance);
107 
108  try
109  {
110  compare_filter->Update();
111  }
112  catch (const itk::ExceptionObject &e)
113  {
114  m_CompareDetails.m_FilterCompleted = false;
115  m_CompareDetails.m_ExceptionMessage = e.what();
116 
117  MITK_WARN << e.what();
118 
119  m_CompareResult = false;
120  return;
121  }
122 
123  // the filter has completed the calculation
124  m_CompareResult = true;
125  m_CompareDetails.m_FilterCompleted = true;
126 
127  m_CompareDetails.m_MaximumDifference = compare_filter->GetMaximumDifference();
128  m_CompareDetails.m_MinimumDifference = compare_filter->GetMinimumDifference();
129  m_CompareDetails.m_MeanDifference = compare_filter->GetMeanDifference();
130  m_CompareDetails.m_TotalDifference = compare_filter->GetTotalDifference();
131  m_CompareDetails.m_PixelsWithDifference = compare_filter->GetNumberOfPixelsWithDifferences();
132 
133  mitk::Image::Pointer output = mitk::GrabItkImageMemory(compare_filter->GetOutput());
134  this->SetOutput(MakeNameFromOutputIndex(0), output.GetPointer());
135 }
itk::SmartPointer< Self > Pointer
virtual void GenerateData() override
A version of GenerateData() specific for image processing filters.
void EstimateValueDifference(const itk::Image< TPixel, VImageDimension > *itkImage1, const mitk::Image *referenceImage)
#define AccessByItk_1(mitkImage, itkImageTypeFunction, arg1)
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
#define MITK_WARN
Definition: mitkLogMacros.h:23
itk::Image< double, 3 > InputImageType
static Pointer New()
Image class for storing images.
Definition: mitkImage.h:76
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Definition: mitkImage.cpp:105
static T max(T x, T y)
Definition: svm.cpp:70
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
void ResetCompareResultsToInitial()
Method resets the compare detail memeber struct to its initial state.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.