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