Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkMultiComponentImageDataComparisonFilter.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
20 #include "mitkImageReadAccessor.h"
21 
22 // other includes
23 // #include <omp.h>
24 
25 namespace mitk
26 {
28  : ImageToImageFilter(), m_Tolerance(0.0f), m_CompareResult(false), m_CompareDetails(nullptr)
29  {
30  this->SetNumberOfRequiredInputs(2);
31  }
32 
35  {
36  if (!m_CompareResult)
37  {
38  return false;
39  }
40 
42  {
43  return false;
44  }
45 
46  return true;
47  }
48 
54  {
55  m_CompareDetails = results;
56  }
57 
60  {
61  // check inputs
62  const Image *testInput = this->GetTestImage();
63  const Image *validInput = this->GetValidImage();
64 
65  // Generally this filter is part of the mitk::Image::Equal() method and only checks the equality of the image data
66  // so no further image type comparison is performed!
67  // CAVE: If the images differ in a parameter other then the image data, the filter may fail!!
68 
69  PixelType type = validInput->GetPixelType();
70 
71  if (type.GetComponentType() == itk::ImageIOBase::CHAR)
72  {
73  CompareMultiComponentImage<char>(testInput, validInput);
74  }
75  else if (type.GetComponentType() == itk::ImageIOBase::UCHAR)
76  {
77  CompareMultiComponentImage<unsigned char>(testInput, validInput);
78  }
79  else if (type.GetComponentType() == itk::ImageIOBase::INT)
80  {
81  CompareMultiComponentImage<int>(testInput, validInput);
82  }
83  else if (type.GetComponentType() == itk::ImageIOBase::UINT)
84  {
85  CompareMultiComponentImage<unsigned int>(testInput, validInput);
86  }
87  else if (type.GetComponentType() == itk::ImageIOBase::SHORT)
88  {
89  CompareMultiComponentImage<short>(testInput, validInput);
90  }
91  else if (type.GetComponentType() == itk::ImageIOBase::USHORT)
92  {
93  CompareMultiComponentImage<unsigned short>(testInput, validInput);
94  }
95  else if (type.GetComponentType() == itk::ImageIOBase::LONG)
96  {
97  CompareMultiComponentImage<long>(testInput, validInput);
98  }
99  else if (type.GetComponentType() == itk::ImageIOBase::ULONG)
100  {
101  CompareMultiComponentImage<unsigned long>(testInput, validInput);
102  }
103  else if (type.GetComponentType() == itk::ImageIOBase::FLOAT)
104  {
105  CompareMultiComponentImage<float>(testInput, validInput);
106  }
107  else if (type.GetComponentType() == itk::ImageIOBase::DOUBLE)
108  {
109  CompareMultiComponentImage<double>(testInput, validInput);
110  }
111  else
112  {
113  mitkThrow() << "Pixel component type not supported!";
114  }
115  }
116 
117  template <typename TPixel>
119  const Image *validImage)
120  {
121  unsigned int noOfTimes = validImage->GetDimension(3);
122  unsigned int noOfPixels = validImage->GetDimension(0) * validImage->GetDimension(1) * validImage->GetDimension(2);
123  unsigned int noOfComponents = validImage->GetPixelType().GetNumberOfComponents();
124 
125  for (unsigned int t = 0; t < noOfTimes; ++t)
126  {
127  ImageReadAccessor readAccTImage(testImage, testImage->GetVolumeData(t));
128  ImageReadAccessor readAccVImage(validImage, validImage->GetVolumeData(t));
129 
130  for (unsigned int p = 0; p < noOfPixels * noOfComponents; ++p)
131  {
132  TPixel vDataItem = static_cast<TPixel *>(const_cast<void *>(readAccVImage.GetData()))[p];
133  TPixel tDataItem = static_cast<TPixel *>(const_cast<void *>(readAccTImage.GetData()))[p];
134 
135  if (std::abs(static_cast<double>(tDataItem - vDataItem)) > m_Tolerance)
136  {
137  ++m_CompareDetails->m_PixelsWithDifference;
138 
139  m_CompareDetails->m_MaximumDifference =
140  std::max(m_CompareDetails->m_MaximumDifference, std::abs(static_cast<double>(tDataItem - vDataItem)));
141 
142  double min =
143  std::min(m_CompareDetails->m_MinimumDifference, std::abs(static_cast<double>(tDataItem - vDataItem)));
144 
145  if (min != 0.0f) // a difference of zero is not a difference!
146  m_CompareDetails->m_MinimumDifference = min;
147 
148  m_CompareDetails->m_TotalDifference += std::abs(static_cast<double>(tDataItem - vDataItem));
149  }
150  }
151  }
152  if (m_CompareDetails->m_PixelsWithDifference > 0)
153  {
154  m_CompareDetails->m_MeanDifference =
155  m_CompareDetails->m_TotalDifference / m_CompareDetails->m_PixelsWithDifference;
156  m_CompareResult = false;
157  }
158  else
159  {
160  m_CompareResult = true;
161  }
162  m_CompareDetails->m_FilterCompleted = true;
163  }
164 
165 } // end namespace mitk
virtual void SetInput(const InputImageType *image)
DataCollection - Class to facilitate loading/accessing structured data.
virtual ImageDataItemPointer GetVolumeData(int t=0, int n=0, void *data=nullptr, ImportMemoryManagementType importMemoryManagement=CopyMemory) const
Definition: mitkImage.cpp:330
void CompareMultiComponentImage(const Image *testImage, const Image *validImage)
virtual void GenerateData() override
A version of GenerateData() specific for image processing filters.
#define mitkThrow()
Image class for storing images.
Definition: mitkImage.h:76
A simple struct to hold the result of the comparison filter.
int GetComponentType() const
Get the component type (the scalar (!) type). Each element may contain m_NumberOfComponents (more tha...
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
Superclass of all classes having one or more Images as input and generating Images as output...
static T min(T x, T y)
Definition: svm.cpp:67
InputImageType * GetInput(void)
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
unsigned int GetDimension() const
Get dimension of the image.
Definition: mitkImage.cpp:110
ImageReadAccessor class to get locked read access for a particular image part.
Class for defining the data type of pixels.
Definition: mitkPixelType.h:55