DGtal  0.9.3
testITKImage.cpp
Go to the documentation of this file.
1 
30 #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 #if defined(__GNUG__)
40 #pragma GCC diagnostic push
41 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
42 #endif
43 #if defined(__clang__)
44 #pragma clang diagnostic push
45 #pragma clang diagnostic ignored "-Wdocumentation"
46 #endif
47 #include <itkExtractImageFilter.h>
48 #if defined(__clang__)
49 #pragma clang diagnostic pop
50 #endif
51 #if defined(__GNUG__)
52 #endif
53 #pragma GCC diagnostic pop
54 
56 
57 using namespace std;
58 using namespace DGtal;
59 
61 // Functions for testing class ITKImage.
63 
68 {
69  unsigned int nbok = 0;
70  unsigned int nb = 0;
71 
72  trace.beginBlock ( "ITK Image init..." );
73 
74  typedef DGtal::int32_t Integer;
75  typedef SpaceND<3, Integer > Space3Type;
77  typedef Domain::Point Point;
78 
80 
81  const Integer t[ ] = { 1, 1, 1};
82  const Integer t2[ ] = { 5, 5, 5};
83  const Integer t3[ ] = { 2, 2, 2};
84  Point a ( t );
85  Point b ( t2 );
86  Point c ( t3 );
87  Integer val;
88 
89  Image myImage ( Domain(a, b) );
90 
91  trace.info() << myImage << std::endl;
92  trace.info() << "getvalue= " << myImage(c) << endl;
93  trace.info() << "set value 23 " << endl;
94  myImage.setValue( c, 23);
95 
96  val = myImage(c);
97 
98  if (val == 23)
99  nbok++;
100  trace.info() << "getvalue= " << val << endl;
101  nb++;
102 
103  //Iterator test
104  trace.info() << "Simple Iterator=";
105  for (Image::ConstIterator it = myImage.begin(), itend = myImage.end();
106  it != itend;
107  ++it)
108  trace.warning() << myImage(it) << " ";
109  trace.info() << endl;
110 
111  //We rewrite the image
112  int nbVal = 0;
113  for (Image::Iterator it = myImage.begin(), itend = myImage.end();
114  it != itend;
115  ++it)
116  myImage.setValue(it, nbVal++);
117 
118  trace.info() << "Set Iterator=";
119  for (Image::ConstIterator it = myImage.begin(), itend = myImage.end();
120  it != itend;
121  ++it)
122  trace.warning() << myImage(it) << " ";
123  trace.info() << endl;
124 
125 
126  trace.info() << "(" << nbok << "/" << nb << ") "
127  << "true == true" << std::endl;
128  trace.endBlock();
129 
130  return nbok == nb;
131 }
132 
134 {
135  unsigned int nbok = 0;
136  unsigned int nb = 0;
137 
138  trace.beginBlock ( "Test the use of a pure ITK method..." );
139 
140  typedef DGtal::int32_t Integer;
143  typedef Domain::Point Point;
144 
145 
147 
148  Point a ( 0, 0 );
149  Point b ( 9, 9);
150  Domain domain(a, b);
151 
152  Image myImage(domain);
153  trace.info() << myImage << std::endl;
154 
155  //We fill the image
156  Integer nbVal = 0;
157  for (Image::Iterator it = myImage.begin(), itend = myImage.end();
158  it != itend;
159  ++it)
160  myImage.setValue(it, nbVal++);
161 
162  trace.info() << "Input image=";
163  for (Image::ConstIterator it = myImage.begin(), itend = myImage.end();
164  it != itend;
165  ++it)
166  trace.warning() << myImage(it) << " ";
167  trace.info() << endl;
168 
169 
170  // We define a cropFilter
171  typedef itk::ExtractImageFilter< Image::ITKImage, Image::ITKImage > CropFilter;
172 
173  // Crop filter region
174  Image::ITKImage::SizeType size;
175  size[0] = 5;
176  size[1] = 5;
177 
178  Image::ITKImage::IndexType index;
179  index[0] = 2;
180  index[1] = 2;
181 
182  Image::ITKImage::RegionType regionToExtract(index,size);
183 
184  // Crop filter process
185  CropFilter::Pointer cropFilter = CropFilter::New();
186  cropFilter->SetInput( myImage.getITKImagePointer() );
187  cropFilter->SetExtractionRegion( regionToExtract );
188  cropFilter->Update();
189 
190  // Pointer to the filter output
191  Image::ITKImagePointer handleOut = cropFilter->GetOutput();
192  Image myImageOut ( handleOut );
193 
194 
195  trace.info() << "Output image=";
196 
197  for (Image::ConstIterator it = myImageOut.begin(), itend = myImageOut.end();
198  it != itend;
199  ++it)
200  {
201  nbok += (it.Value() == (it.GetIndex()[1]*10 + it.GetIndex()[0]));
202  nb++;
203  trace.warning() << it.Value() << "(" << (it.GetIndex()[1]*10 + it.GetIndex()[0]) << ")" << " ";
204  }
205  trace.info() << endl;
206 
207  trace.info() << "(" << nbok << "/" << nb << ") " << "true == true" << std::endl;
208  trace.endBlock();
209 
210  return nbok == 25 && nb == 25;
211 }
212 
214 // Standard services - public :
215 
216 int main( int argc, char** argv )
217 {
218  trace.beginBlock ( "Testing class ITKImage" );
219  trace.info() << "Args:";
220  for ( int i = 0; i < argc; ++i )
221  trace.info() << " " << argv[ i ];
222  trace.info() << endl;
223 
224  bool res = testITKImage() && testITKMethod();
225  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
226  trace.endBlock();
227  return res ? 0 : 1;
228 }
229 // //
void beginBlock(const std::string &keyword="")
HyperRectDomain< Space > Domain
const Domain domain(Point(1, 2), Point(6, 5))
unsigned int index(DGtal::uint32_t n, unsigned int b)
Definition: testBits.cpp:44
Trace trace
Definition: Common.h:137
SpaceND< 2, int > Space2Type
Aim: SpaceND is a utility class that defines the fundamental structure of a Digital Space in ND...
Definition: SpaceND.h:95
Aim: Parallelepidec region of a digital space, model of a &#39;CDomain&#39;.
STL namespace.
double endBlock()
void setValue(const Point &aPoint, const Value &aValue)
Definition: Image.h:247
bool testITKImage()
std::ostream & emphase()
int main(int argc, char **argv)
DGtal is the top-level namespace which contains all DGtal functions and types.
MyPointD Point
Definition: testClone2.cpp:383
std::ostream & warning()
std::ostream & info()
ImageContainerBySTLVector< Domain, Value > Image
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
bool testITKMethod()
Aim: implements a model of CImageContainer using a ITK Image.