/Modules/Filtering/DistanceMap/test/itkDanielssonDistanceMapImageFilterTest.cxx

https://github.com/crtc/ITK-PBNRR-GITHUB · C++ · 305 lines · 215 code · 59 blank · 31 comment · 26 complexity · ec6b75b95727ccf3b4c6c47a27b10ffa MD5 · raw file

  1. /*=========================================================================
  2. *
  3. * Copyright Insight Software Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0.txt
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. *=========================================================================*/
  18. #if defined(_MSC_VER)
  19. #pragma warning ( disable : 4786 )
  20. #endif
  21. #include "itkImage.h"
  22. #include "itkImageSliceConstIteratorWithIndex.h"
  23. #include "itkDanielssonDistanceMapImageFilter.h"
  24. int itkDanielssonDistanceMapImageFilterTest(int, char* [] )
  25. {
  26. std::cout << "Test ITK Danielsson Distance Map" << std::endl << std::endl;
  27. std::cout << "Compute the distance map of a 9x9 image" << std::endl;
  28. std::cout << "with a point at (4,4) (value=1)" << std::endl << std::endl;
  29. std::cout << "with a point at (1,6) (value=2)" << std::endl << std::endl;
  30. typedef itk::Image<float, 2> myImageType2D1;
  31. typedef itk::Image<float, 2> myImageType2D2;
  32. /* Allocate the 2D image */
  33. myImageType2D1::SizeType size2D = {{9,9}};
  34. myImageType2D1::IndexType index2D = {{0,0}};
  35. myImageType2D1::RegionType region2D;
  36. region2D.SetSize( size2D );
  37. region2D.SetIndex( index2D );
  38. myImageType2D1::Pointer inputImage2D = myImageType2D1::New();
  39. inputImage2D->SetLargestPossibleRegion( region2D );
  40. inputImage2D->SetBufferedRegion( region2D );
  41. inputImage2D->SetRequestedRegion( region2D );
  42. inputImage2D->Allocate();
  43. /* Set pixel (4,4) with the value 1
  44. * and pixel (1,6) with the value 2
  45. * The Danielsson Distance is performed for each pixel with a value > 0
  46. * The ClosestPoints computation is based on the value of the pixel.
  47. */
  48. typedef itk::ImageRegionIteratorWithIndex<myImageType2D1> myIteratorType2D1;
  49. typedef itk::ImageRegionIteratorWithIndex<myImageType2D2> myIteratorType2D2;
  50. myIteratorType2D1 it2D1(inputImage2D,region2D);
  51. // Set the image to 0
  52. while( !it2D1.IsAtEnd() )
  53. {
  54. it2D1.Set( 0 );
  55. ++it2D1;
  56. }
  57. index2D[0] = 4;
  58. index2D[1] = 4;
  59. inputImage2D->SetPixel( index2D, 1);
  60. index2D[0] = 1;
  61. index2D[1] = 6;
  62. inputImage2D->SetPixel( index2D, 2);
  63. /* Create Danielsson Distance Map filter */
  64. typedef itk::DanielssonDistanceMapImageFilter<
  65. myImageType2D1,
  66. myImageType2D2 > myFilterType2D;
  67. myFilterType2D::Pointer filter2D = myFilterType2D::New();
  68. filter2D->SetInput( inputImage2D );
  69. myImageType2D2::Pointer outputDistance2D = filter2D->GetOutput();
  70. myImageType2D1::Pointer outputVoronoi2D = filter2D->GetVoronoiMap();
  71. myFilterType2D::VectorImageType::Pointer
  72. outputComponents = filter2D->GetVectorDistanceMap();
  73. filter2D->Update();
  74. /* Show Distance map */
  75. itk::ImageSliceConstIteratorWithIndex<myImageType2D2> it2D2(
  76. outputDistance2D,
  77. outputDistance2D->GetRequestedRegion() );
  78. it2D2.GoToBegin();
  79. it2D2.SetFirstDirection ( 0 );
  80. it2D2.SetSecondDirection( 1 );
  81. while( !it2D2.IsAtEnd() )
  82. {
  83. while( !it2D2.IsAtEndOfSlice() )
  84. {
  85. while( !it2D2.IsAtEndOfLine() )
  86. {
  87. std::cout.width(5);
  88. std::cout << it2D2.Get() << "\t";
  89. ++it2D2;
  90. }
  91. std::cout << std::endl;
  92. it2D2.NextLine();
  93. }
  94. it2D2.NextSlice();
  95. }
  96. /* Show Closest Points map */
  97. std::cout << std::endl << std::endl;
  98. std::cout << "Voronoi Map Image 2D" << std::endl << std::endl;
  99. itk::ImageSliceConstIteratorWithIndex< myImageType2D2 > it2D3(
  100. outputVoronoi2D,
  101. outputVoronoi2D->GetRequestedRegion() );
  102. it2D3.SetFirstDirection( 0 );
  103. it2D3.SetSecondDirection( 1 );
  104. while( !it2D3.IsAtEnd() )
  105. {
  106. while( !it2D3.IsAtEndOfSlice() )
  107. {
  108. while( !it2D3.IsAtEndOfLine() )
  109. {
  110. std::cout.width(5);
  111. std::cout << it2D3.Get() << "\t";
  112. ++it2D3;
  113. }
  114. std::cout << std::endl;
  115. it2D3.NextLine();
  116. }
  117. it2D3.NextSlice();
  118. }
  119. /* Show VectorsComponents Points map */
  120. std::cout << std::endl << std::endl;
  121. std::cout << "Components Map Image 2D" << std::endl << std::endl;
  122. itk::ImageSliceConstIteratorWithIndex< myFilterType2D::VectorImageType > it2D4(
  123. outputComponents,
  124. outputComponents->GetRequestedRegion() );
  125. it2D4.SetFirstDirection( 0 );
  126. it2D4.SetSecondDirection( 1 );
  127. while( !it2D4.IsAtEnd() )
  128. {
  129. while( !it2D4.IsAtEndOfSlice() )
  130. {
  131. while( !it2D4.IsAtEndOfLine() )
  132. {
  133. std::cout << "[";
  134. for (unsigned int i=0;i<2;i++)
  135. {
  136. std::cout << it2D4.Get()[i];
  137. if(i==0)
  138. {
  139. std::cout << ",";
  140. }
  141. }
  142. std::cout << "]";
  143. std::cout << "\t";
  144. ++it2D4;
  145. }
  146. std::cout << std::endl;
  147. it2D4.NextLine();
  148. }
  149. it2D4.NextSlice();
  150. }
  151. /* Test Squared Distance functionality */
  152. myImageType2D2::IndexType index;
  153. index[0] = 0;
  154. index[1] = 0;
  155. const double distance1 = outputDistance2D->GetPixel( index );
  156. filter2D->SquaredDistanceOn();
  157. if( filter2D->GetSquaredDistance() != true )
  158. {
  159. std::cerr << "filter2D->GetSquaredDistance() != true" <<std::endl;
  160. return EXIT_FAILURE;
  161. }
  162. filter2D->SetSquaredDistance( true );
  163. filter2D->Update();
  164. const double distance2 = outputDistance2D->GetPixel( index );
  165. const myImageType2D2::PixelType epsilon = 1e-5;
  166. if( vcl_fabs( distance2 - distance1 * distance1 ) > epsilon )
  167. {
  168. std::cerr << "Error in use of the SetSquaredDistance() method" << std::endl;
  169. return EXIT_FAILURE;
  170. }
  171. /* Show Squared Distance map */
  172. std::cout << "Squared Distance Map " << std::endl;
  173. it2D2.GoToBegin();
  174. it2D2.SetFirstDirection ( 0 );
  175. it2D2.SetSecondDirection( 1 );
  176. while( !it2D2.IsAtEnd() )
  177. {
  178. while( !it2D2.IsAtEndOfSlice() )
  179. {
  180. while( !it2D2.IsAtEndOfLine() )
  181. {
  182. std::cout.width(5);
  183. std::cout << it2D2.Get() << "\t";
  184. ++it2D2;
  185. }
  186. std::cout << std::endl;
  187. it2D2.NextLine();
  188. }
  189. it2D2.NextSlice();
  190. }
  191. /* Test for images with anisotropic spacing */
  192. myImageType2D1::SpacingType anisotropicSpacing;
  193. anisotropicSpacing[0] = 1.0;
  194. anisotropicSpacing[1] = 5.0;
  195. inputImage2D->SetSpacing( anisotropicSpacing );
  196. inputImage2D->FillBuffer( 0 );
  197. index2D[0] = 4;
  198. index2D[1] = 4;
  199. inputImage2D->SetPixel( index2D, 1);
  200. filter2D->SetInput( inputImage2D );
  201. filter2D->SetInputIsBinary(true);
  202. if( filter2D->GetInputIsBinary() != true )
  203. {
  204. std::cerr << "filter2D->GetInputIsBinary() != true" <<std::endl;
  205. return EXIT_FAILURE;
  206. }
  207. filter2D->UseImageSpacingOn();
  208. if( filter2D->GetUseImageSpacing() != true )
  209. {
  210. std::cerr << "filter2D->GetUseImageSpacing() != true" << std::endl;
  211. return EXIT_FAILURE;
  212. }
  213. filter2D->SetUseImageSpacing(true);
  214. filter2D->Update();
  215. index2D[1] = 5;
  216. myImageType2D2::PixelType expectedValue =
  217. static_cast<myImageType2D2::PixelType>(anisotropicSpacing[1]);
  218. expectedValue *= expectedValue;
  219. myImageType2D2::PixelType pixelValue =
  220. filter2D->GetOutput()->GetPixel( index2D );
  221. if ( vcl_fabs( expectedValue - pixelValue ) > epsilon )
  222. {
  223. std::cerr << "Error when image spacing is anisotropic." << std::endl;
  224. std::cerr << "Pixel value was " << pixelValue << ", expected " << expectedValue << std::endl;
  225. return EXIT_FAILURE;
  226. }
  227. it2D2.GoToBegin();
  228. it2D2.SetFirstDirection ( 0 );
  229. it2D2.SetSecondDirection( 1 );
  230. while( !it2D2.IsAtEnd() )
  231. {
  232. while( !it2D2.IsAtEndOfSlice() )
  233. {
  234. while( !it2D2.IsAtEndOfLine() )
  235. {
  236. std::cout.width(5);
  237. std::cout << it2D2.Get() << "\t";
  238. ++it2D2;
  239. }
  240. std::cout << std::endl;
  241. it2D2.NextLine();
  242. }
  243. it2D2.NextSlice();
  244. }
  245. return EXIT_SUCCESS;
  246. }