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 
19 //  Software Guide : BeginLatex
20 //
21 //  This example illustrates how to convert an ITK Image into a PointSet.
22 //
23 //  Software Guide : EndLatex
24 
25 // Software Guide : BeginCodeSnippet
26 #include "itkImageFileReader.h"
27 #include "itkImageFileWriter.h"
28 
29 
30 #include "itkImage.h"
31 #include "itkPointSet.h"
32 #include "itkImageRegionConstIterator.h"
33 
34 
main(int argc,char * argv[])35 int main( int argc, char * argv[] )
36 {
37   // Verify the number of parameters in the command line
38   if( argc < 2 )
39     {
40     std::cerr << "Usage: " << std::endl;
41     std::cerr << argv[0] << " inputImageFile  " << std::endl;
42     return EXIT_FAILURE;
43     }
44 
45 
46   using PixelType = unsigned char;
47   constexpr unsigned int Dimension = 2;
48 
49   using ImageType = itk::Image< PixelType, Dimension >;
50   using PointSetType = itk::PointSet< PixelType, Dimension >;
51   using ReaderType = itk::ImageFileReader< ImageType >;
52 
53   ReaderType::Pointer reader = ReaderType::New();
54 
55   const char * inputFilename  = argv[1];
56   reader->SetFileName( inputFilename  );
57 
58   try
59     {
60     reader->Update();
61     }
62   catch( itk::ExceptionObject & err )
63     {
64     std::cout << "ExceptionObject caught !" << std::endl;
65     std::cout << err << std::endl;
66     return EXIT_FAILURE;
67     }
68 
69   PointSetType::Pointer  pointSet = PointSetType::New();
70 
71 
72   using IteratorType = itk::ImageRegionConstIterator< ImageType >;
73 
74   const ImageType * image = reader->GetOutput();
75 
76   IteratorType it( image, image->GetBufferedRegion() );
77 
78   it.GoToBegin();
79 
80 
81   using PointType = PointSetType::PointType;
82   PointType point;
83 
84   unsigned long pointId = 0;
85 
86   while( !it.IsAtEnd() )
87     {
88 
89     // Convert the pixel position into a Point
90     image->TransformIndexToPhysicalPoint( it.GetIndex() , point );
91     pointSet->SetPoint( pointId, point );
92 
93     // Transfer the pixel data to the value associated with the point.
94     pointSet->SetPointData( pointId, it.Get() );
95 
96     ++it;
97     ++pointId;
98     }
99 
100 
101   std::cout << "Number Of Points = ";
102   std::cout << pointSet->GetNumberOfPoints() << std::endl;
103 
104 
105   // Software Guide : EndCodeSnippet
106   return EXIT_SUCCESS;
107 }
108