Orfeo Toolbox  3.16
KMeansImageClassificationExample.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 
19 // Software Guide : BeginLatex
20 //
21 // The K-Means classification proposed by ITK for images is limited to
22 // scalar images and is not streamed. In this example, we show
23 // how the use of the \doxygen{otb}{KMeansImageClassificationFilter}
24 // allows for a simple implementation of a K-Means classification
25 // application. We will start by including the appropirate header file.
26 //
27 // Software Guide : EndLatex
28 
29 // Software Guide : BeginCodeSnippet
31 // Software Guide : EndCodeSnippet
32 #include "otbVectorImage.h"
33 #include "otbImage.h"
34 #include "otbImageFileReader.h"
35 #include "otbImageFileWriter.h"
36 
37 int main(int argc, char * argv[])
38 {
39  const char * infname = argv[1];
40  const char * outfname = argv[2];
41  const unsigned int nbClasses = atoi(argv[3]);
42 
43 // Software Guide : BeginLatex
44 //
45 // We will assume double precision input images and will also define
46 // the type for the labeled pixels.
47 //
48 // Software Guide : EndLatex
49 
50 // Software Guide : BeginCodeSnippet
51  const unsigned int Dimension = 2;
52  typedef double PixelType;
53  typedef unsigned short LabeledPixelType;
54 // Software Guide : EndCodeSnippet
55 // Software Guide : BeginLatex
56 //
57 // Our classifier will be genric enough to be able to process images
58 // with any number of bands. We read the images as
59 // \doxygen{otb}{VectorImage}s. The labeled image will be a scalar image.
60 //
61 // Software Guide : EndLatex
62 
63 // Software Guide : BeginCodeSnippet
65  typedef otb::Image<LabeledPixelType, Dimension> LabeledImageType;
66 // Software Guide : EndCodeSnippet
67 // Software Guide : BeginLatex
68 //
69 // We can now define the type for the classifier filter, which is
70 // templated over its input and output image types.
71 //
72 // Software Guide : EndLatex
73 
74 // Software Guide : BeginCodeSnippet
76  ClassificationFilterType;
77  typedef ClassificationFilterType::KMeansParametersType KMeansParametersType;
78 // Software Guide : EndCodeSnippet
79 // Software Guide : BeginLatex
80 //
81 // And finally, we define the reader and the writer. Since the images
82 // to classify can be very big, we will use a streamed writer which
83 // will trigger the streaming ability of the classifier.
84 //
85 // Software Guide : EndLatex
86 
87 // Software Guide : BeginCodeSnippet
88  typedef otb::ImageFileReader<ImageType> ReaderType;
89  typedef otb::ImageFileWriter<LabeledImageType> WriterType;
90 // Software Guide : EndCodeSnippet
91 // Software Guide : BeginLatex
92 //
93 // We instantiate the classifier and the reader objects and we set
94 // their parameters. Please note the call of the
95 // \code{GenerateOutputInformation()} method on the reader in order to
96 // have available the information about the input image (size, number
97 // of bands, etc.) without needing to actually read the image.
98 //
99 // Software Guide : EndLatex
100 
101 // Software Guide : BeginCodeSnippet
102  ClassificationFilterType::Pointer filter = ClassificationFilterType::New();
103 
104  ReaderType::Pointer reader = ReaderType::New();
105  reader->SetFileName(infname);
106  reader->GenerateOutputInformation();
107 // Software Guide : EndCodeSnippet
108 // Software Guide : BeginLatex
109 //
110 // The classifier needs as input the centroids of
111 // the classes. We declare the parameter vector, and we read the
112 // centroids from the arguments of the program.
113 //
114 // Software Guide : EndLatex
115 
116 // Software Guide : BeginCodeSnippet
117  const unsigned int sampleSize =
118  ClassificationFilterType::MaxSampleDimension;
119  const unsigned int parameterSize = nbClasses * sampleSize;
120  KMeansParametersType parameters;
121 
122  parameters.SetSize(parameterSize);
123  parameters.Fill(0);
124 
125  for (unsigned int i = 0; i < nbClasses; ++i)
126  {
127  for (unsigned int j = 0; j <
128  reader->GetOutput()->GetNumberOfComponentsPerPixel(); ++j)
129  {
130  parameters[i * sampleSize + j] =
131  atof(argv[4 + i *
132  reader->GetOutput()->GetNumberOfComponentsPerPixel()
133  + j]);
134  }
135  }
136 
137  std::cout << "Parameters: " << parameters << std::endl;
138 // Software Guide : EndCodeSnippet
139 // Software Guide : BeginLatex
140 //
141 // We set the parameters for the classifier, we plug the pipeline and
142 // trigger its execution by updating the output of the writer.
143 //
144 // Software Guide : EndLatex
145 
146 // Software Guide : BeginCodeSnippet
147  filter->SetCentroids(parameters);
148  filter->SetInput(reader->GetOutput());
149 
150  WriterType::Pointer writer = WriterType::New();
151  writer->SetInput(filter->GetOutput());
152  writer->SetFileName(outfname);
153  writer->Update();
154 // Software Guide : EndCodeSnippet
155  return EXIT_SUCCESS;
156 }

Generated at Sun Feb 3 2013 00:15:36 for Orfeo Toolbox with doxygen 1.8.1.1