diff --git a/Examples/Classification/CMakeLists.txt b/Examples/Classification/CMakeLists.txt index 06fefda5a43df6f26855b18b286847635ad9b662..e2bcff04b1ca7e8d8ff9acf45716ed02df30c57b 100644 --- a/Examples/Classification/CMakeLists.txt +++ b/Examples/Classification/CMakeLists.txt @@ -7,6 +7,9 @@ TARGET_LINK_LIBRARIES(KdTreeBasedKMeansClustering OTBCommon ITKCommon ITKStatist ADD_EXECUTABLE(ScalarImageKmeansClassifier ScalarImageKmeansClassifier.cxx ) TARGET_LINK_LIBRARIES(ScalarImageKmeansClassifier OTBIO OTBCommon ITKIO ITKCommon ) +ADD_EXECUTABLE(KMeansImageClassificationExample KMeansImageClassificationExample.cxx ) +TARGET_LINK_LIBRARIES(KMeansImageClassificationExample OTBCommon OTBLearning ITKCommon ITKStatistics) + ADD_EXECUTABLE(ScalarImageKmeansModelEstimator ScalarImageKmeansModelEstimator.cxx ) TARGET_LINK_LIBRARIES(ScalarImageKmeansModelEstimator OTBIO OTBCommon ITKIO ITKCommon ) @@ -19,6 +22,11 @@ TARGET_LINK_LIBRARIES(ExpectationMaximizationMixtureModelEstimator OTBIO OTBComm ADD_EXECUTABLE(ScalarImageMarkovRandomField1 ScalarImageMarkovRandomField1.cxx ) TARGET_LINK_LIBRARIES(ScalarImageMarkovRandomField1 OTBIO OTBCommon ITKIO ITKCommon ) +ADD_EXECUTABLE(SOMImageClassificationExample SOMImageClassificationExample.cxx ) +TARGET_LINK_LIBRARIES(SOMImageClassificationExample OTBCommon OTBLearning ITKCommon ITKStatistics) + +ADD_EXECUTABLE(SVMImageClassificationExample SVMImageClassificationExample.cxx ) +TARGET_LINK_LIBRARIES(SVMImageClassificationExample OTBCommon OTBLearning ITKCommon ITKStatistics) #ADD_EXECUTABLE( BayesianClassifierInitializer BayesianClassifierInitializer.cxx ) #TARGET_LINK_LIBRARIES(BayesianClassifierInitializer ITKCommon ITKStatistics ITKIO) diff --git a/Examples/Classification/KMeansImageClassificationExample.cxx b/Examples/Classification/KMeansImageClassificationExample.cxx new file mode 100644 index 0000000000000000000000000000000000000000..2481814bb4c23937446edb80328a86d1c32a7f15 --- /dev/null +++ b/Examples/Classification/KMeansImageClassificationExample.cxx @@ -0,0 +1,75 @@ +/*========================================================================= + +Program: ORFEO Toolbox +Language: C++ +Date: $Date$ +Version: $Revision$ + + +Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. +See OTBCopyright.txt for details. + + +This software is distributed WITHOUT ANY WARRANTY; without even +the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "otbKMeansImageClassificationFilter.h" +#include "otbVectorImage.h" +#include "otbImage.h" +#include "otbImageFileReader.h" +#include "otbStreamingImageFileWriter.h" + +int main(int argc, char * argv[]) +{ + const char * infname = argv[1]; + const char * outfname = argv[2]; + const unsigned int nbClasses = atoi(argv[3]); + + const unsigned int Dimension = 2; + typedef double PixelType; + typedef unsigned short LabeledPixelType; + + typedef otb::VectorImage<PixelType,Dimension> ImageType; + typedef otb::Image<LabeledPixelType,Dimension> LabeledImageType; + typedef otb::KMeansImageClassificationFilter<ImageType,LabeledImageType> ClassificationFilterType; + typedef ClassificationFilterType::KMeansParametersType KMeansParametersType; + typedef otb::ImageFileReader<ImageType> ReaderType; + typedef otb::StreamingImageFileWriter<LabeledImageType> WriterType; + + + // Instantiating object + ClassificationFilterType::Pointer filter = ClassificationFilterType::New(); + + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName(infname); + reader->GenerateOutputInformation(); + + const unsigned int sampleSize = ClassificationFilterType::MaxSampleDimension; + const unsigned int parameterSize = nbClasses * sampleSize; + KMeansParametersType parameters; + + parameters.SetSize(parameterSize); + parameters.Fill(0); + + for(unsigned int i = 0; i<nbClasses;++i) + { + for(unsigned int j = 0; j < reader->GetOutput()->GetNumberOfComponentsPerPixel();++j) + { + parameters[i*sampleSize+j]=atof(argv[4+i*reader->GetOutput()->GetNumberOfComponentsPerPixel()+j]); + } + } + + std::cout<<"Parameters: "<<parameters<<std::endl; + + filter->SetCentroids(parameters); + filter->SetInput(reader->GetOutput()); + + WriterType::Pointer writer = WriterType::New(); + writer->SetInput(filter->GetOutput()); + writer->SetFileName(outfname); + writer->Update(); + + return EXIT_SUCCESS; +} diff --git a/Examples/Classification/SOMImageClassificationExample.cxx b/Examples/Classification/SOMImageClassificationExample.cxx new file mode 100644 index 0000000000000000000000000000000000000000..2e3e95d983c68660fcc946d43f54c52c5248c507 --- /dev/null +++ b/Examples/Classification/SOMImageClassificationExample.cxx @@ -0,0 +1,63 @@ +/*========================================================================= + +Program: ORFEO Toolbox +Language: C++ +Date: $Date$ +Version: $Revision$ + + +Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. +See OTBCopyright.txt for details. + + +This software is distributed WITHOUT ANY WARRANTY; without even +the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "otbSOMImageClassificationFilter.h" +#include "otbVectorImage.h" +#include "otbImage.h" +#include "otbSOMMap.h" +#include "otbImageFileReader.h" +#include "otbStreamingImageFileWriter.h" + +int main(int argc, char * argv[]) +{ + const char * infname = argv[1]; + const char * somfname = argv[2]; + const char * outfname = argv[3]; + + const unsigned int Dimension = 2; + typedef double PixelType; + typedef unsigned short LabeledPixelType; + + typedef otb::VectorImage<PixelType,Dimension> ImageType; + typedef otb::Image<LabeledPixelType,Dimension> LabeledImageType; + typedef otb::SOMMap<ImageType::PixelType> SOMMapType; + typedef otb::SOMImageClassificationFilter<ImageType,LabeledImageType,SOMMapType> ClassificationFilterType; + typedef otb::ImageFileReader<ImageType> ReaderType; + typedef otb::ImageFileReader<SOMMapType> SOMReaderType; + typedef otb::StreamingImageFileWriter<LabeledImageType> WriterType; + + + // Instantiating object + ClassificationFilterType::Pointer filter = ClassificationFilterType::New(); + + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName(infname); + + SOMReaderType::Pointer somreader = SOMReaderType::New(); + somreader->SetFileName(somfname); + somreader->Update(); + + filter->SetMap(somreader->GetOutput()); + filter->SetInput(reader->GetOutput()); + + WriterType::Pointer writer = WriterType::New(); + writer->SetInput(filter->GetOutput()); + writer->SetFileName(outfname); + writer->Update(); + + return EXIT_SUCCESS; +} diff --git a/Examples/Classification/SVMImageClassificationExample.cxx b/Examples/Classification/SVMImageClassificationExample.cxx new file mode 100644 index 0000000000000000000000000000000000000000..78b4e5b6adcdf08d3a6b96aad3e7b92c83e8dc60 --- /dev/null +++ b/Examples/Classification/SVMImageClassificationExample.cxx @@ -0,0 +1,59 @@ +/*========================================================================= + +Program: ORFEO Toolbox +Language: C++ +Date: $Date$ +Version: $Revision$ + + +Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. +See OTBCopyright.txt for details. + + +This software is distributed WITHOUT ANY WARRANTY; without even +the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "otbSVMImageClassificationFilter.h" +#include "otbVectorImage.h" +#include "otbImage.h" +#include "otbImageFileReader.h" +#include "otbStreamingImageFileWriter.h" + +int main(int argc, char * argv[]) +{ + const char * infname = argv[1]; + const char * modelfname = argv[2]; + const char * outfname = argv[3]; + + const unsigned int Dimension = 2; + typedef double PixelType; + typedef unsigned short LabeledPixelType; + + typedef otb::VectorImage<PixelType,Dimension> ImageType; + typedef otb::Image<LabeledPixelType,Dimension> LabeledImageType; + typedef otb::SVMImageClassificationFilter<ImageType,LabeledImageType> ClassificationFilterType; + typedef ClassificationFilterType::ModelType ModelType; + typedef otb::ImageFileReader<ImageType> ReaderType; + typedef otb::StreamingImageFileWriter<LabeledImageType> WriterType; + + // Instantiating object + ClassificationFilterType::Pointer filter = ClassificationFilterType::New(); + + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName(infname); + + ModelType::Pointer model = ModelType::New(); + model->LoadModel(modelfname); + + filter->SetModel(model); + filter->SetInput(reader->GetOutput()); + + WriterType::Pointer writer = WriterType::New(); + writer->SetInput(filter->GetOutput()); + writer->SetFileName(outfname); + writer->Update(); + + return EXIT_SUCCESS; +}