Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
DirectOverlayTest.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 <mitkIOUtil.h>
14 #include <mitkITKImageImport.h>
15 #include <mitkImage.h>
16 #include <mitkImageAccessByItk.h>
17 #include <mitkImageCast.h>
18 #include <mitkTestingMacros.h>
19 
20 #include <itkBinaryThresholdImageFilter.h>
21 #include <itkImageFileWriter.h>
22 #include <itkTestingComparisonImageFilter.h>
23 
25 
26 //#include <mitkLexicalCast.h>
27 
28 class DirectOverlayTestClass
29 {
30 public:
31  template <typename TPixel, unsigned int VDimensions>
32  static void InternalThreshold(const itk::Image<TPixel, VDimensions> *image,
33  mitk::Image::Pointer &output,
34  const double th[])
35  {
36  typedef itk::Image<TPixel, VDimensions> InputImageType;
37  typedef itk::Image<unsigned int, VDimensions> OutputImageType;
38  typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdFilterType;
39 
40  typename BinaryThresholdFilterType::Pointer thresholder = BinaryThresholdFilterType::New();
41  thresholder->SetInput(image);
42  thresholder->SetLowerThreshold(th[0]);
43  thresholder->SetUpperThreshold(th[1]);
44  thresholder->SetInsideValue(255);
45  thresholder->SetOutsideValue(0);
46  thresholder->Update();
47 
48  output = mitk::ImportItkImage(thresholder->GetOutput());
49  // mitk::IOUtil::Save( output, "/tmp/out.nii" );
50  std::cout << "extra line";
51  }
52 
53  template <typename TPixel, unsigned int VDimensions>
54  static void InternalThreshold2(const itk::Image<TPixel, VDimensions> *image,
55  itk::Image<unsigned int, 3>::Pointer &output,
56  const double th[])
57  {
58  typedef itk::Image<TPixel, VDimensions> InputImageType;
59  typedef itk::Image<unsigned int, VDimensions> OutputImageType;
60  typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdFilterType;
61 
62  typename BinaryThresholdFilterType::Pointer thresholder = BinaryThresholdFilterType::New();
63  thresholder->SetInput(image);
64  thresholder->SetLowerThreshold(th[0]);
65  thresholder->SetUpperThreshold(th[1]);
66  thresholder->SetInsideValue(255);
67  thresholder->SetOutsideValue(0);
68  thresholder->Update();
69 
70  output = thresholder->GetOutput();
71  }
72 
73  static void TestOverlay(mitk::Image::Pointer original,
75  const double lower,
76  const double upper)
77  {
78  mitk::Image::Pointer overlayImage;
79  const double th[] = {lower, upper};
80  typedef itk::Image<unsigned int, 3> ImageType;
81  ImageType::Pointer itkOverlayImage = ImageType::New();
82 
83  AccessByItk_2(original, InternalThreshold, overlayImage, th);
84  /*
85  AccessFixedDimensionByItk_2( original, InternalThreshold2,
86  3, itkOverlayImage,
87  th );
88 
89  overlayImage = mitk::ImportItkImage( itkOverlayImage );
90  */
91  // mitk::IOUtil::Save(truth, "/tmp/truth_TestOverlay.nii");
92  try
93  {
94  // mitk::Image::Pointer temp = overlayImage;
95  mitk::IOUtil::Save(overlayImage, "/tmp/overlayImage_TestOverlay.nrrd");
96  }
97  catch (const itk::ExceptionObject &e)
98  {
99  MITK_ERROR << "Save image: exception : " << e.what();
100  }
101 
102  typedef itk::Image<unsigned int, 3> InputImageType;
103 
104  InputImageType::Pointer overlayItk;
105  try
106  {
107  mitk::CastToItkImage(overlayImage, overlayItk);
108  }
109  catch (const mitk::Exception &e)
110  {
111  MITK_ERROR << "(CAST) Catched exception while creating accessor " << e.what();
112  // MITK_TEST_FAILED_MSG("Exception for ouverlay image");
113  }
114 
115  /*
116  typedef itk::ImageFileWriter< InputImageType > WriterType;
117  WriterType::Pointer writer = WriterType::New();
118  writer->SetFileName("/tmp/overlayITK_TestOverlay.nii");
119  writer->SetInput(overlayItk);
120  writer->Update();
121  */
122  InputImageType::Pointer truthItk;
123  mitk::CastToItkImage(truth, truthItk);
124 
125  bool difference = false;
126  /*
127  try
128  {
129  typedef unsigned int TPixel;
130 
131  itk::ImageRegionConstIteratorWithIndex< InputImageType > iter( truthItk, truthItk->GetLargestPossibleRegion()
132  );
133  iter.GoToBegin();
134 
135  mitk::ImagePixelReadAccessor< TPixel, 3 > readAccessor( overlayImage, overlayImage->GetVolumeData(0),
136  mitk::ImageAccessorBase::ExceptionIfLocked );
137 
138  while( !iter.IsAtEnd() )
139  {
140  TPixel ref = iter.Get();
141  TPixel val = readAccessor.GetPixelByIndex( iter.GetIndex() );
142 
143  difference |= ( ref != val );
144  //if( difference )
145  //{
146  std::cout << iter.GetIndex() << ":" << ref << " ? " << val << "\n";
147  //}
148  ++iter;
149  }
150  }
151  catch( const mitk::Exception &e)
152  {
153  MITK_ERROR << "Catched exception while creating accessor "<< e.what();
154  //MITK_TEST_FAILED_MSG("Exception for ouverlay image");
155  }
156 
157 
158 
159  */
160 
161  /*
162  typedef itk::Testing::ComparisonImageFilter
163  <InputImageType, InputImageType> ComparisonImageFilterType;
164  ComparisonImageFilterType::Pointer comp =
165  ComparisonImageFilterType::New();
166  comp->SetValidInput(truthItk);
167  comp->SetTestInput(overlayItk);
168  try
169  {
170  comp->Update();
171  }
172  catch( const itk::ExceptionObject& e)
173  {
174  MITK_ERROR << "ITK Exception: " << e.what();
175  }
176  */
177  typedef unsigned int TPixel;
178 
179  itk::ImageRegionConstIteratorWithIndex<InputImageType> iter(truthItk, truthItk->GetLargestPossibleRegion());
180  itk::ImageRegionConstIteratorWithIndex<InputImageType> iter2(overlayItk, overlayItk->GetLargestPossibleRegion());
181  iter.GoToBegin();
182 
183  unsigned int counter = 0;
184  while (!iter.IsAtEnd() && !iter2.IsAtEnd())
185  {
186  TPixel ref = iter.Get();
187  TPixel val = iter2.Get();
188 
189  if (ref != val)
190  {
191  counter++;
192  // std::cout << iter.GetIndex() << ":" << ref << " ? " << val << "\n";
193  }
194 
195  ++iter;
196  ++iter2;
197  }
198 
199  std::cout << "Differs in " << counter << "voxels" << std::endl;
200 
202  // comp->GetNumberOfPixelsWithDifferences() == 0,
203  counter == 0,
204  "Comparing overlay with ground truth")
205  }
206 
207  static void TestDirectOverlay(char *in, char *gt, const int lower, const int upper)
208  {
209  mitk::Image::Pointer original = mitk::IOUtil::Load<mitk::Image>(in);
210  mitk::Image::Pointer truth = mitk::IOUtil::Load<mitk::Image>(gt);
211 
212  if (original.IsNotNull() && original->GetDimension() == 3 && truth.IsNotNull() && truth->GetDimension() == 3 &&
213  upper > lower)
214  {
215  TestOverlay(original, truth, lower, upper);
216  }
217  else
218  {
219  MITK_TEST_FAILED_MSG(<< "Invalid parameters");
220  }
221  }
222 };
223 
224 int DirectOverlayTest(int argc, char *argv[])
225 {
226  MITK_TEST_BEGIN("DirectOverlay")
227 
228  MITK_TEST_CONDITION_REQUIRED(argc >= 5, "File to load has been specified on the command line");
229 
230  unsigned int lower = 0, upper = 0;
231 
232  try
233  {
234  sscanf(argv[3], "%u", &lower);
235  sscanf(argv[4], "%u", &upper);
236  // lower = boost::lexical_cast<double>(argv[3]);
237  // upper = boost::lexical_cast<double>(argv[4]);
238 
239  MITK_INFO << "Got values: " << lower << " : " << upper;
240  }
241  catch (std::exception &e)
242  {
243  MITK_TEST_FAILED_MSG(<< e.what());
244  }
245 
246  DirectOverlayTestClass::TestDirectOverlay(argv[1], argv[2], lower, upper);
247 
248  MITK_TEST_END()
249 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
itk::Image< unsigned char, 3 > ImageType
#define MITK_ERROR
Definition: mitkLogMacros.h:20
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
Image::Pointer ImportItkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, const BaseGeometry *geometry=nullptr, bool update=true)
Imports an itk::Image (with a specific type) as an mitk::Image.Instantiates instance of ITKImageImpor...
int DirectOverlayTest(int argc, char *argv[])
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
#define MITK_TEST_FAILED_MSG(MSG)
Fail and finish test with message MSG.
mitk::Image::Pointer image
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:244
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 void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
#define AccessByItk_2(mitkImage, itkImageTypeFunction, arg1, arg2)
and MITK_TEST_END()