Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkImageSliceSelectorTest.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 #include <mitkIOUtil.h>
18 #include <mitkImage.h>
19 #include <mitkImageSliceSelector.h>
20 
21 #include <fstream>
22 int mitkImageSliceSelectorTest(int argc, char *argv[])
23 {
24  int slice_nr = 1;
25  std::cout << "Loading file: ";
26  if (argc == 0)
27  {
28  std::cout << "no file specified [FAILED]" << std::endl;
29  return EXIT_FAILURE;
30  }
31 
33  try
34  {
35  image = mitk::IOUtil::LoadImage(argv[1]);
36  }
37  catch (const mitk::Exception &)
38  {
39  std::cout << "file not an image - test will not be applied [PASSED]" << std::endl;
40  std::cout << "[TEST DONE]" << std::endl;
41  return EXIT_SUCCESS;
42  }
43  catch (itk::ExceptionObject &ex)
44  {
45  std::cout << "Exception: " << ex << "[FAILED]" << std::endl;
46  return EXIT_FAILURE;
47  }
48 
49  if (image->GetDimension(2) < 2)
50  slice_nr = 0;
51 
52  // Take a slice
54  slice->SetInput(image);
55  slice->SetSliceNr(slice_nr);
56  slice->Update();
57 
58  std::cout << "Testing IsInitialized(): ";
59  if (slice->GetOutput()->IsInitialized() == false)
60  {
61  std::cout << "[FAILED]" << std::endl;
62  return EXIT_FAILURE;
63  }
64  std::cout << "[PASSED]" << std::endl;
65 
66  std::cout << "Testing IsSliceSet(): ";
67  if (slice->GetOutput()->IsSliceSet(0) == false)
68  {
69  std::cout << "[FAILED]" << std::endl;
70  return EXIT_FAILURE;
71  }
72  std::cout << "[PASSED]" << std::endl;
73 
74  /* deactivated because IpPic is not available any more (see bug 16662)
75  if(itksys::SystemTools::LowerCase(itksys::SystemTools::GetFilenameExtension(argv[1])).find(".pic")!=std::string::npos)
76  {
77  std::cout << "Testing whether the slice is identical with a slice loaded by mitkIpPicGetSlice:";
78  mitkIpPicDescriptor *picslice = mitkIpPicGetSlice(argv[1], NULL, (image->GetDimension(2)-1-slice_nr)+1);
79  int i, size = _mitkIpPicSize(picslice);
80  char * p1 = (char*)slice->GetPic()->data;
81  char * p2 = (char*)picslice->data;
82  //picslice->info->write_protect=mitkIpFalse;
83  //mitkIpPicPut("C:\\1aaaaIPPIC.pic", picslice);
84  //mitkIpPicPut("C:\\1aaaaSEL.pic", slice->GetPic());
85  for(i=0; i<size; ++i, ++p1, ++p2)
86  {
87  if((*p1) != (*p2))
88  {
89  std::cout<<"[FAILED]"<<std::endl;
90  return EXIT_FAILURE;
91  }
92  }
93  std::cout<<"[PASSED]"<<std::endl;
94  mitkIpPicFree(picslice);
95  }
96  */
97 
98  /* Deactivated because CylindricToCartesianFilter is not available any more (see bug 16662)
99  try
100  {
101  std::cout << "Testing another, smaller (!!) input with the same slice-selector(): ";
102  //Use CylindricToCartesianFilter
103  mitk::CylindricToCartesianFilter::Pointer cyl2cart = mitk::CylindricToCartesianFilter::New();
104  cyl2cart->SetInput(image);
105  //the output size of this filter is smaller than the of the input!!
106  cyl2cart->SetTargetXSize( 64 );
107 
108  //Use the same slice-selector again, this time to take a slice of the filtered image
109  //which is smaller than the one of the old input!!
110  slice->SetInput(cyl2cart->GetOutput());
111  slice->SetSliceNr(1);
112 
113  //The requested region is still the old one,
114  //therefore the following results in most ITK versions
115  //in an exception!
116  slice->Update();
117 
118  //If no exception occured, check that the requested region is now
119  //the one of the smaller image
120  if(cyl2cart->GetOutput()->GetLargestPossibleRegion().GetSize()[0]!=64)
121  {
122  std::cout<<"Part 1 [FAILED]"<<std::endl;
123  return EXIT_FAILURE;
124  }
125  std::cout<<"Part 1 (without exception) [PASSED] ";
126 
127  //Check that the size of the output is now the one of the smaller image
128  if((cyl2cart->GetOutput()->GetDimensions()[0]!=64) || (cyl2cart->GetOutput()->GetDimensions()[1]!=64))
129  {
130  std::cout<<"Part 1b [FAILED]"<<std::endl;
131  return EXIT_FAILURE;
132  }
133  std::cout<<"Part 1b [PASSED] ";
134  }
135  catch ( itk::ExceptionObject &err)
136  {
137  std::cout<<"Part 1(with expected exception) ... seems to be not ITK 2.0.0 [PASSED]"<<std::endl;
138  std::cout<<err<<std::endl;
139  //after such an exception, we need to call ResetPipeline.
140  slice->ResetPipeline();
141  }
142  */
143  try
144  {
145  slice->UpdateLargestPossibleRegion();
146  }
147  catch (itk::ExceptionObject)
148  {
149  std::cout << "Part 2 [FAILED]" << std::endl;
150  return EXIT_FAILURE;
151  }
152  std::cout << "Part 2 [PASSED]" << std::endl;
153 
154  std::cout << "Testing IsInitialized(): ";
155  if (slice->GetOutput()->IsInitialized() == false)
156  {
157  std::cout << "[FAILED]" << std::endl;
158  return EXIT_FAILURE;
159  }
160  std::cout << "[PASSED]" << std::endl;
161 
162  std::cout << "Testing IsSliceSet(): ";
163  if (slice->GetOutput()->IsSliceSet(0) == false)
164  {
165  std::cout << "[FAILED]" << std::endl;
166  return EXIT_FAILURE;
167  }
168  std::cout << "[PASSED]" << std::endl;
169 
170  if (image->GetDimension(3) > 1)
171  {
172  int time = image->GetDimension(3) - 1;
173 
174  std::cout << "Testing 3D+t: Setting time to " << time << ": ";
175  slice->SetTimeNr(time);
176  if (slice->GetTimeNr() != time)
177  {
178  std::cout << "[FAILED]" << std::endl;
179  return EXIT_FAILURE;
180  }
181  std::cout << "[PASSED]" << std::endl;
182 
183  std::cout << "Testing 3D+t: Updating slice: ";
184  slice->Update();
185  if (slice->GetOutput()->IsInitialized() == false)
186  {
187  std::cout << "[FAILED]" << std::endl;
188  return EXIT_FAILURE;
189  }
190  std::cout << "[PASSED]" << std::endl;
191 
192  std::cout << "Testing 3D+t: IsSliceSet(): ";
193  if (slice->GetOutput()->IsSliceSet(0) == false)
194  {
195  std::cout << "[FAILED]" << std::endl;
196  return EXIT_FAILURE;
197  }
198  std::cout << "[PASSED]" << std::endl;
199 
200  std::cout << "Testing 3D+t: First slice in reader available: ";
201  if (image->IsSliceSet(0, time) == false)
202  {
203  std::cout << "[FAILED]" << std::endl;
204  return EXIT_FAILURE;
205  }
206  std::cout << "[PASSED]" << std::endl;
207  }
208 
209  std::cout << "[TEST DONE]" << std::endl;
210  return EXIT_SUCCESS;
211 }
int mitkImageSliceSelectorTest(int argc, char *argv[])
static Pointer New()
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:597