DGtal  1.1.0
Functions
testITKImage.cpp File Reference
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/kernel/SpaceND.h"
#include "DGtal/kernel/domains/HyperRectDomain.h"
#include "DGtal/images/ImageContainerByITKImage.h"
#include <boost/foreach.hpp>
#include <itkExtractImageFilter.h>
Include dependency graph for testITKImage.cpp:

Go to the source code of this file.

Functions

bool testITKImage ()
 
bool testITKMethod ()
 
bool testITKImageWithMetadata ()
 
bool testITKImageWithShiftDomain ()
 
int main (int argc, char **argv)
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR a PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
David Coeurjolly (david.nosp@m..coe.nosp@m.urjol.nosp@m.ly@l.nosp@m.iris..nosp@m.cnrs.nosp@m..fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2010/12/09

Functions for testing class ITKImage.

This file is part of the DGtal library.

Definition in file testITKImage.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 408 of file testITKImage.cpp.

409 {
410  trace.beginBlock ( "Testing class ITKImage" );
411  trace.info() << "Args:";
412  for ( int i = 0; i < argc; ++i )
413  trace.info() << " " << argv[ i ];
414  trace.info() << endl;
415 
416  bool res = testITKImage() && testITKMethod() &&
418  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
419  trace.endBlock();
420  return res ? 0 : 1;
421 }

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), DGtal::Trace::info(), testITKImage(), testITKImageWithMetadata(), testITKImageWithShiftDomain(), testITKMethod(), and DGtal::trace.

◆ testITKImage()

bool testITKImage ( )

Example of a test. To be completed.

Definition at line 67 of file testITKImage.cpp.

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 lowerBound ( t );
85  Point upperBound ( t2 );
86  Point c ( t3 );
87  Integer val;
88 
89  Image myImage ( Domain(lowerBound, upperBound) );
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 }

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::Image< TImageContainer >::setValue(), DGtal::trace, and DGtal::Trace::warning().

Referenced by main().

◆ testITKImageWithMetadata()

bool testITKImageWithMetadata ( )

Definition at line 213 of file testITKImage.cpp.

214 {
215  trace.beginBlock ( "ITK Image With Metadata init..." );
216 
217  unsigned int nbok = 0;
218  unsigned int nb = 0;
219 
220  typedef DGtal::int32_t Integer;
221  typedef SpaceND<3, Integer > Space3Type;
223  typedef Domain::Point Point;
224 
226 
227  // REMEMBER: Origin in ITK is the physical position of the index {0,0,...}
228  // even when that zero-index is not included in the LargestPossibleRegion of the image.
229  const Integer t[ ] = { 1, 1, 1};
230  const Integer t2[ ] = { 5, 5, 5};
231  const Integer t3[ ] = { 2, 2, 2};
232  Point lowerBound ( t );
233  Point upperBound ( t2 );
234  Point c ( t3 );
235  Integer val;
236 
237  Image myImage ( Domain(lowerBound, upperBound) );
238  // Fill the image
239  int nbVal = 0;
240  for (Image::Iterator it = myImage.begin(), itend = myImage.end();
241  it != itend; ++it) {
242  myImage.setValue(it, nbVal++);
243  }
244  // Change the default metadata (physical properties) of an itk_image.
245  auto itk_image = myImage.getITKImagePointer();
246  Image::ITKImage::PointType origin;
247  origin.Fill(10.0);
248  Image::ITKImage::SpacingType spacing;
249  spacing.Fill(2.0);
250  Image::ITKImage::DirectionType direction;
251  direction.SetIdentity();
252  itk_image->SetOrigin(origin);
253  itk_image->SetSpacing(spacing);
254  itk_image->SetDirection(direction);
255 
256  // Check that the value of index points is not affected by a change of metadata.
257  val = myImage.operator()(lowerBound);
258  nbok += (val == 0); nb++;
259  trace.info() << "Index: " << lowerBound << ". Value: " << val << ". Expected: " << 0 << std::endl;
260  val = myImage.operator()(upperBound);
261  nbok += (val == 124); nb++;
262  trace.info() << "Index: " << upperBound << ". Value: " << val << ". Expected: " << 124 << std::endl;
263  val = myImage.operator()(c);
264  nbok += (val == 31); nb++;
265  trace.info() << "Index: " << c << ". Value: " << val << ". Expected: " << 31 << std::endl;
266 
267  Image::PhysicalPoint physical_point;
268  Image::PhysicalPoint expected_physical_point;
269  Image::Point index_point;
270 
271  // when shiftDomain is zero, index points (ITK) and domain points (DGtal) are equal
272  index_point = myImage.getIndexFromDomainPoint(lowerBound);
273  nbok += (index_point == lowerBound); nb++;
274  physical_point = myImage.getPhysicalPointFromDomainPoint(index_point);
275  expected_physical_point = myImage.getLowerBoundAsPhysicalPoint();
276  nbok += (physical_point[0] == 12.0); nb++;
277  nbok += (physical_point == expected_physical_point); nb++;
278  trace.info() << "Index: " << index_point << ". PhysicalPoint: " << physical_point << ". Expected: " << expected_physical_point << std::endl;
279 
280  index_point = myImage.getIndexFromDomainPoint(upperBound);
281  nbok += (index_point == upperBound); nb++;
282  physical_point = myImage.getPhysicalPointFromDomainPoint(index_point);
283  expected_physical_point = myImage.getUpperBoundAsPhysicalPoint();
284  nbok += (physical_point[0] == 20.0); nb++;
285  nbok += (physical_point == expected_physical_point); nb++;
286  trace.info() << "Index: " << index_point << ". PhysicalPoint: " << physical_point << ". Expected: " << expected_physical_point << std::endl;
287 
288  auto index_back = myImage.getDomainPointFromPhysicalPoint(physical_point);
289  nbok += (index_back == upperBound); nb++;
290  trace.info() << "PhysicalPoint: " << physical_point << ". Index (back): " << index_back << ". Expected: " << upperBound << std::endl;
291 
292  trace.endBlock();
293 
294  return nbok == 10 && nb == 10;
295 }

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::Image< TImageContainer >::setValue(), and DGtal::trace.

Referenced by main().

◆ testITKImageWithShiftDomain()

bool testITKImageWithShiftDomain ( )

Definition at line 297 of file testITKImage.cpp.

298 {
299  trace.beginBlock ( "ITK Image With ShiftDomain init..." );
300 
301  unsigned int nbok = 0;
302  unsigned int nb = 0;
303 
304  typedef DGtal::int32_t Integer;
305  typedef SpaceND<3, Integer > Space3Type;
307  typedef Domain::Point Point;
308 
310 
311  const Integer t[ ] = { 1, 1, 1};
312  const Integer t2[ ] = { 5, 5, 5};
313  const Integer t3[ ] = { 2, 2, 2};
314  Point lowerBound ( t );
315  Point upperBound ( t2 );
316  Point c ( t3 );
317  Integer val;
318 
319  Image myImage ( Domain(lowerBound, upperBound) );
320 
321  // Fill the image
322  int nbVal = 0;
323  for (Image::Iterator it = myImage.begin(), itend = myImage.end();
324  it != itend; ++it) {
325  myImage.setValue(it, nbVal++);
326  }
327 
328  // Change the default metadata (physical properties) of an itk_image.
329  auto itk_image = myImage.getITKImagePointer();
330  Image::ITKImage::PointType origin;
331  origin.Fill(10.0);
332  Image::ITKImage::SpacingType spacing;
333  spacing.Fill(2.0);
334  Image::ITKImage::DirectionType direction;
335  direction.SetIdentity();
336  itk_image->SetOrigin(origin);
337  itk_image->SetSpacing(spacing);
338  itk_image->SetDirection(direction);
339 
340 
341  // Apply a domainShift
342  const Integer sd[ ] = { -20, -20, -20};
343  Point domainShift(sd);
344  myImage.updateDomain(domainShift);
345  Point new_lowerBound = myImage.domain().lowerBound();
346  Point new_upperBound = myImage.domain().upperBound();
347  nbok += ( new_lowerBound == lowerBound + domainShift); nb++;
348  trace.info() << "lowerBound: " << new_lowerBound << ". Expected: " << lowerBound + domainShift << std::endl;
349  nbok += ( new_upperBound == upperBound + domainShift); nb++;
350  trace.info() << "upperBound: " << new_upperBound << ". Expected: " << upperBound + domainShift << std::endl;
351 
352  // Check that the shifted domain points to the correct indices of the image.
353  val = myImage.operator()(new_lowerBound);
354  // It should have the same value than lowerBound had before applying the domainShift
355  nbok += (val == 0); nb++;
356  trace.info() << "Index: " << new_lowerBound << ". Value: " << val << ". Expected: " << 0 << std::endl;
357  val = myImage.operator()(new_upperBound);
358  nbok += (val == 124); nb++;
359  trace.info() << "Index: " << new_upperBound << ". Value: " << val << ". Expected: " << 124 << std::endl;
360  val = myImage.operator()(myImage.getDomainPointFromIndex(c));
361  nbok += (val == 31); nb++;
362  trace.info() << "Index: " << c << ". Value: " << val << ". Expected: " << 31 << std::endl;
363 
364  Image::PhysicalPoint physical_point;
365  Image::PhysicalPoint expected_physical_point;
366  Image::Point index_point;
367  Image::Point domain_point;
368 
369  index_point = lowerBound;
370  domain_point = new_lowerBound;
371  physical_point = myImage.getPhysicalPointFromDomainPoint(domain_point);
372  expected_physical_point = Image::PhysicalPoint(12.0, 12.0, 12.0);
373  nbok += (physical_point == expected_physical_point); nb++;
374  trace.info() << "Domain: " << domain_point <<
375  ". Index: " << index_point <<
376  ". PhysicalPoint: " << physical_point <<
377  ". Expected: " << expected_physical_point << std::endl;
378 
379  index_point = myImage.getIndexFromDomainPoint(new_lowerBound);
380  nbok += ( index_point == lowerBound ); nb++;
381  trace.info() << "index_point: " << index_point << ". Expected: " << lowerBound << std::endl;
382  physical_point = myImage.getPhysicalPointFromDomainPoint(new_lowerBound);
383  expected_physical_point = myImage.getLowerBoundAsPhysicalPoint();
384  nbok += (physical_point[0] == 12.0); nb++;
385  nbok += (physical_point == expected_physical_point); nb++;
386  trace.info() << "Domain: " << new_lowerBound <<
387  ". Index: " << index_point <<
388  ". PhysicalPoint: " << physical_point <<
389  ". Expected: " << expected_physical_point << std::endl;
390 
391  index_point = upperBound;
392  domain_point = new_upperBound;
393  physical_point = myImage.getPhysicalPointFromDomainPoint(domain_point);
394  expected_physical_point = myImage.getUpperBoundAsPhysicalPoint();
395  nbok += (physical_point[0] == 20.0); nb++;
396  nbok += (physical_point == expected_physical_point); nb++;
397  trace.info() << "Domain: " << new_lowerBound <<
398  ". Index: " << index_point <<
399  ". PhysicalPoint: " << physical_point <<
400  ". Expected: " << expected_physical_point << std::endl;
401 
402  return nbok == 11 && nb == 11;
403 }

References DGtal::Trace::beginBlock(), DGtal::Image< TImageContainer >::domain(), DGtal::Trace::info(), DGtal::Image< TImageContainer >::setValue(), and DGtal::trace.

Referenced by main().

◆ testITKMethod()

bool testITKMethod ( )

Definition at line 133 of file testITKImage.cpp.

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 lowerBound ( 0, 0 );
149  Point upperBound ( 9, 9);
150  Domain domain(lowerBound, upperBound);
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 }

References DGtal::Trace::beginBlock(), domain, DGtal::Trace::endBlock(), index(), DGtal::Trace::info(), DGtal::Image< TImageContainer >::setValue(), DGtal::trace, and DGtal::Trace::warning().

Referenced by main().

DGtal::HyperRectDomain
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
Definition: HyperRectDomain.h:100
DGtal::Trace::endBlock
double endBlock()
DGtal::ImageContainerByITKImage
Aim: implements a model of CImageContainer using a ITK Image.
Definition: ImageContainerByITKImage.h:94
DGtal::int32_t
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::trace
Trace trace
Definition: Common.h:150
DGtal::ImageContainerBySTLVector< Domain, Value >::ConstIterator
std::vector< Value >::const_iterator ConstIterator
Definition: ImageContainerBySTLVector.h:263
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::SpaceND
Definition: SpaceND.h:96
DGtal::Trace::info
std::ostream & info()
DGtal::Z2i::Integer
DGtal::int32_t Integer
Definition: StdDefs.h:74
Image
ImageContainerBySTLVector< Domain, Value > Image
Definition: testSimpleRandomAccessRangeFromPoint.cpp:45
Domain
HyperRectDomain< Space > Domain
Definition: testSimpleRandomAccessRangeFromPoint.cpp:44
index
unsigned int index(DGtal::uint32_t n, unsigned int b)
Definition: testBits.cpp:44
DGtal::ImageContainerBySTLVector< Domain, Value >::Iterator
std::vector< Value >::iterator Iterator
Definition: ImageContainerBySTLVector.h:262
DGtal::Image
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
domain
Domain domain
Definition: testProjection.cpp:88
DGtal::PointVector< dim, Integer >
testITKMethod
bool testITKMethod()
Definition: testITKImage.cpp:133
Space2Type
SpaceND< 2, int > Space2Type
Definition: testFreemanChain.cpp:51
testITKImageWithShiftDomain
bool testITKImageWithShiftDomain()
Definition: testITKImage.cpp:297
DGtal::Trace::warning
std::ostream & warning()
testITKImage
bool testITKImage()
Definition: testITKImage.cpp:67
Point
MyPointD Point
Definition: testClone2.cpp:383
testITKImageWithMetadata
bool testITKImageWithMetadata()
Definition: testITKImage.cpp:213