DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testITKImage.cpp
1 
30 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/kernel/SpaceND.h"
34 #include "DGtal/kernel/domains/HyperRectDomain.h"
35 #include "DGtal/images/ImageContainerByITKImage.h"
36 #include <boost/foreach.hpp>
37 
38 //specific itk method
39 #include <itkBinaryThresholdImageFilter.h>
40 #include <itkImageFileWriter.h>
42 
43 using namespace std;
44 using namespace DGtal;
45 
47 // Functions for testing class ITKImage.
49 
53 bool testITKImage()
54 {
55  unsigned int nbok = 0;
56  unsigned int nb = 0;
57 
58  trace.beginBlock ( "ITK Image init..." );
59 
60  typedef DGtal::int32_t Integer;
61  typedef SpaceND<3, Integer > Space3Type;
63  typedef Domain::Point Point;
64 
65  //ATTENTION only the int container works at this point
67 
68  const Integer t[ ] = { 1, 1, 1};
69  const Integer t2[ ] = { 5, 5, 5};
70  const Integer t3[ ] = { 2, 2, 2};
71  Point a ( t );
72  Point b ( t2 );
73  Point c ( t3 );
74  Integer val;
75 
76  Image myImage ( a, b );
77 
78  trace.info() << myImage << std::endl;
79  trace.info() << "getvalue= " << myImage(c) << endl;
80  trace.info() << "set value 23 " << endl;
81  myImage.setValue( c, 23);
82 
83  val = myImage(c);
84 
85  if (val == 23)
86  nbok++;
87  trace.info() << "getvalue= " << val << endl;
88  nb++;
89 
90  //Iterator test
91  trace.info() << "Simple Iterator=";
92  for (Image::ConstIterator it = myImage.begin(), itend = myImage.end();
93  it != itend;
94  ++it)
95  trace.warning() << myImage(it) << " ";
96  trace.info() << endl;
97 
98  //We rewrite the image
99  int nbVal = 0;
100  for (Image::Iterator it = myImage.begin(), itend = myImage.end();
101  it != itend;
102  ++it)
103  myImage.setValue(it, nbVal++);
104 
105  trace.info() << "Set Iterator=";
106  for (Image::ConstIterator it = myImage.begin(), itend = myImage.end();
107  it != itend;
108  ++it)
109  trace.warning() << myImage(it) << " ";
110  trace.info() << endl;
111 
112 
113  trace.info() << "(" << nbok << "/" << nb << ") "
114  << "true == true" << std::endl;
115  trace.endBlock();
116 
117  return nbok == nb;
118 }
119 
120 bool testITKMethod()
121 {
122  unsigned int nbok = 0;
123  unsigned int nb = 0;
124 
125  trace.beginBlock ( "Test the use of a pure ITK method..." );
126 
127  typedef DGtal::int32_t Integer;
129  typedef HyperRectDomain<Space2Type> Domain;
130  typedef Domain::Point Point;
131 
132 
134 
135  Point a ( 0, 0 );
136  Point b ( 25, 25);
137 
138  Image myImage ( a, b );
139  trace.info() << myImage << std::endl;
140 
141  //We fill the image
142  Integer nbVal = 0;
143  for (Image::Iterator it = myImage.begin(), itend = myImage.end();
144  it != itend;
145  ++it)
146  myImage.setValue(it, nbVal++);
147 
148  trace.info() << "Input image=";
149  for (Image::ConstIterator it = myImage.begin(), itend = myImage.end();
150  it != itend;
151  ++it)
152  trace.warning() << myImage(it) << " ";
153  trace.info() << endl;
154 
155  //We construct an ITK pipeline
156  typedef itk::BinaryThresholdImageFilter< Image::ITKImage, Image::ITKImage> Filter;
157  Filter::Pointer filter = Filter::New();
158 
159  filter->SetInput( myImage.getImagePointer() );
160  filter->SetOutsideValue( 0 );
161  filter->SetInsideValue( 10 );
162  filter->SetLowerThreshold( 34 );;
163  filter->SetUpperThreshold( 400 );;
164  filter->Update();
165 
166  //We create a DGtal::Image from a pointer to the pipeline output
167  Image::ITKImagePointer handleOut = filter->GetOutput();
168  Image myImageOut ( a, b, handleOut );
169 
170  //We trace the result of the thresholding
171  trace.info() << "Output image=";
172 
173  for (Image::ConstIterator it = myImageOut.begin(), itend = myImageOut.end();
174  it != itend;
175  ++it)
176  trace.warning() << myImageOut(it) << " ";
177  trace.info() << endl;
178 
179  trace.info() << "(" << nbok << "/" << nb << ") "
180  << "true == true" << std::endl;
181  trace.endBlock();
182 
183 
184 
185 
186  return nbok == nb;
187 }
188 
190 // Standard services - public :
191 
192 int main( int argc, char** argv )
193 {
194  trace.beginBlock ( "Testing class ITKImage" );
195  trace.info() << "Args:";
196  for ( int i = 0; i < argc; ++i )
197  trace.info() << " " << argv[ i ];
198  trace.info() << endl;
199 
200  bool res = testITKImage() && testITKMethod();
201  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
202  trace.endBlock();
203  return res ? 0 : 1;
204 }
205 // //