Commit d3bd4b1f authored by Guillaume Pasero's avatar Guillaume Pasero
Browse files

MRG: Merge branch 'clean_deprecated' into develop

Conflicts:
	Modules/Applications/AppClassification/app/otbTrainVectorClassifier.cxx
	Modules/Applications/AppClassification/test/CMakeLists.txt
	Modules/Learning/Supervised/otb-module.cmake
parents d1646e07 be9e56be
......@@ -45,7 +45,7 @@
#include "otbImageFileReader.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkRecursiveGaussianImageFilter.h"
#include "otbWarpImageFilter.h"
#include "itkWarpImageFilter.h"
#include "itkMeanReciprocalSquareDifferenceImageToImageMetric.h"
// Software Guide : BeginCodeSnippet
......@@ -255,7 +255,7 @@ int main(int argc, char** argv)
dfWriter->SetFileName(argv[4]);
dfWriter->Update();
typedef otb::WarpImageFilter<InputImageType, InputImageType,
typedef itk::WarpImageFilter<InputImageType, InputImageType,
DisplacementFieldType> WarperType;
WarperType::Pointer warper = WarperType::New();
......
......@@ -42,7 +42,7 @@
// Software Guide : BeginCodeSnippet
#include "otbNCCRegistrationFilter.h"
#include "itkRecursiveGaussianImageFilter.h"
#include "otbWarpImageFilter.h"
#include "itkWarpImageFilter.h"
// Software Guide : EndCodeSnippet
#include "otbImageOfVectorsToMonoChannelExtractROI.h"
......@@ -211,7 +211,7 @@ int main(int argc, char** argv)
dfWriter->SetFileName(argv[4]);
dfWriter->Update();
typedef otb::WarpImageFilter<MovingImageType, MovingImageType,
typedef itk::WarpImageFilter<MovingImageType, MovingImageType,
DisplacementFieldType> WarperType;
WarperType::Pointer warper = WarperType::New();
......
......@@ -47,7 +47,7 @@
#include "itkWindowedSincInterpolateImageFunction.h"
#include "itkGradientDescentOptimizer.h"
#include "otbBSplinesInterpolateDisplacementFieldGenerator.h"
#include "otbWarpImageFilter.h"
#include "itkWarpImageFilter.h"
// Software Guide : EndCodeSnippet
#include "otbImageFileReader.h"
......@@ -414,7 +414,7 @@ int main(int argc, char* argv[])
// Software Guide : BeginCodeSnippet
typedef otb::WarpImageFilter<ImageType, ImageType,
typedef itk::WarpImageFilter<ImageType, ImageType,
DisplacementFieldType> ImageWarperType;
// Software Guide : EndCodeSnippet
......
......@@ -35,7 +35,7 @@
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "otbAssymmetricFusionOfLineDetectorImageFilter.h"
#include "otbAsymmetricFusionOfLineDetectorImageFilter.h"
// Software Guide : EndCodeSnippet
#include "otbImage.h"
......@@ -85,7 +85,7 @@ int main(int argc, char * argv[])
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::AssymmetricFusionOfLineDetectorImageFilter<InternalImageType,
typedef otb::AsymmetricFusionOfLineDetectorImageFilter<InternalImageType,
InternalImageType>
FilterType;
// Software Guide : EndCodeSnippet
......
......@@ -33,23 +33,8 @@ add_executable(SOMExample SOMExample.cxx)
target_link_libraries(SOMExample ${OTB_LIBRARIES})
if(OTBLibSVM_LOADED)
add_executable(SVMImageClassificationExample SVMImageClassificationExample.cxx)
target_link_libraries(SVMImageClassificationExample ${OTB_LIBRARIES})
add_executable(SVMImageEstimatorClassificationMultiExample SVMImageEstimatorClassificationMultiExample.cxx)
target_link_libraries(SVMImageEstimatorClassificationMultiExample ${OTB_LIBRARIES})
add_executable(SVMImageModelEstimatorExample SVMImageModelEstimatorExample.cxx)
target_link_libraries(SVMImageModelEstimatorExample ${OTB_LIBRARIES})
add_executable(SVMPointSetClassificationExample SVMPointSetClassificationExample.cxx)
target_link_libraries(SVMPointSetClassificationExample ${OTB_LIBRARIES})
add_executable(SVMPointSetExample SVMPointSetExample.cxx)
target_link_libraries(SVMPointSetExample ${OTB_LIBRARIES})
add_executable(SVMPointSetModelEstimatorExample SVMPointSetModelEstimatorExample.cxx)
target_link_libraries(SVMPointSetModelEstimatorExample ${OTB_LIBRARIES})
endif()
if(OTBOpenCV_LOADED)
......
/*
* Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fstream>
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "otbImage.h"
// Software Guide : BeginCommandLineArgs
// INPUTS: {ROI_QB_MUL_1.png}
// OUTPUTS: {ROI_QB_MUL_1_SVN_CLASS.png}
// ${OTB_DATA_ROOT}/Examples/svm_image_model.svm
// Software Guide : EndCommandLineArgs
// Software Guide : BeginLatex
// This example illustrates the use of the
// \doxygen{otb}{SVMClassifier} class for performing SVM
// classification on images.
// In this example, we will use an SVM model estimated in the example
// of section \ref{sec:LearningWithImages}
// to separate between water and non-water pixels by using the RGB
// values only. The images used for this example are shown in
// figure~\ref{fig:SVMROIS}.
// The first thing to do is include the header file for the
// class. Since the \doxygen{otb}{SVMClassifier} takes
// \doxygen{itk}{ListSample}s as input, the class
// \doxygen{itk}{PointSetToListAdaptor} is needed.
//
//
// Software Guide : EndLatex
#include "itkImageToListSampleAdaptor.h"
// Software Guide : BeginCodeSnippet
#include "otbSVMClassifier.h"
// Software Guide : EndCodeSnippet
int main(int argc, char* argv[])
{
if (argc != 4)
{
std::cout << "Usage : " << argv[0] << " inputImage outputImage modelFile "
<< std::endl;
return EXIT_FAILURE;
}
const char * imageFilename = argv[1];
const char * modelFilename = argv[3];
const char * outputFilename = argv[2];
// Software Guide : BeginLatex
//
// In the framework of supervised learning and classification, we will
// always use feature vectors for the characterization of the
// classes. On the other hand, the class labels are scalar
// values. Here, we start by defining the type of the features as the
// \code{PixelType}, which will be used to define the feature
// \code{VectorType}. We also declare the type for the labels.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef double PixelType;
typedef int LabelPixelType;
// Software Guide : EndCodeSnippet
const unsigned int Dimension = 2;
// Software Guide : BeginLatex
//
// We can now proceed to define the image type used for storing the
// features. We also define the reader.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::Image<itk::FixedArray<PixelType, 3>,
Dimension> InputImageType;
typedef otb::ImageFileReader<InputImageType> ReaderType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now read the image by calling the \code{Update} method of the reader.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(imageFilename);
reader->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The image has now to be transformed to a sample which
// is compatible with the classification framework. We will use a
// \doxygen{itk}{Statistics::ImageToListSampleAdaptor} for this
// task. This class is templated over the image type used for
// storing the measures.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::Statistics::ImageToListSampleAdaptor<InputImageType> SampleType;
SampleType::Pointer sample = SampleType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// After instantiation, we can set the image as an imput of our
// sample adaptor.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
sample->SetImage(reader->GetOutput());
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Now, we need to declare the SVM model which is to be used by the
// classifier. The SVM model is templated over the type of value used
// for the measures and the type of pixel used for the labels.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::SVMModel<PixelType, LabelPixelType> ModelType;
ModelType::Pointer model = ModelType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// After instantiation, we can load a model saved to a file (see
// section \ref{sec:LearningWithImages} for an example of model
// estimation and storage to a file.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
model->LoadModel(modelFilename);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We have now all the elements to create a classifier. The classifier
// is templated over the sample type (the type of the data to be
// classified) and the label type (the type of the output of the classifier).
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::SVMClassifier<SampleType, LabelPixelType> ClassifierType;
ClassifierType::Pointer classifier = ClassifierType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We set the classifier parameters : number of classes, SVM model,
// the sample data. And we trigger the classification process by
// calling the \code{Update} method.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
int numberOfClasses = model->GetNumberOfClasses();
classifier->SetNumberOfClasses(numberOfClasses);
classifier->SetModel(model);
classifier->SetInput(sample.GetPointer());
classifier->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// After the classification step, we usually want to get the
// results. The classifier gives an output under the form of a sample
// list. This list supports the classical STL iterators. Therefore, we
// will create an output image and fill it up with the results of the
// classification. The pixel type of the output image is the same as
// the one used for the labels.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef ClassifierType::ClassLabelType OutputPixelType;
typedef otb::Image<OutputPixelType, Dimension> OutputImageType;
OutputImageType::Pointer outputImage = OutputImageType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We allocate the memory for the output image using the information
// from the input image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::Index<Dimension> myIndexType;
typedef itk::Size<Dimension> mySizeType;
typedef itk::ImageRegion<Dimension> myRegionType;
mySizeType size;
size[0] = reader->GetOutput()->GetRequestedRegion().GetSize()[0];
size[1] = reader->GetOutput()->GetRequestedRegion().GetSize()[1];
myIndexType start;
start[0] = 0;
start[1] = 0;
myRegionType region;
region.SetIndex(start);
region.SetSize(size);
outputImage->SetRegions(region);
outputImage->Allocate();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now declare the iterators on the list that we get at the
// output of the classifier as well as the iterator to fill the output image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ClassifierType::OutputType* membershipSample =
classifier->GetOutput();
ClassifierType::OutputType::ConstIterator m_iter =
membershipSample->Begin();
ClassifierType::OutputType::ConstIterator m_last =
membershipSample->End();
typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
OutputIteratorType outIt(outputImage,
outputImage->GetBufferedRegion());
outIt.GoToBegin();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We will iterate through the list, get the labels and assign pixel
// values to the output image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
while (m_iter != m_last && !outIt.IsAtEnd())
{
outIt.Set(m_iter.GetClassLabel());
++m_iter;
++outIt;
}
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Only for visualization purposes, we choose to rescale the image of
// classes before saving it to a file. We will use the
// \doxygen{itk}{RescaleIntensityImageFilter} for this purpose.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::Image<unsigned char, Dimension> FileImageType;
typedef itk::RescaleIntensityImageFilter<OutputImageType,
FileImageType> RescalerType;
RescalerType::Pointer rescaler = RescalerType::New();
rescaler->SetOutputMinimum(itk::NumericTraits<unsigned char>::min());
rescaler->SetOutputMaximum(itk::NumericTraits<unsigned char>::max());
rescaler->SetInput(outputImage);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now create an image file writer and save the image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::ImageFileWriter<FileImageType> WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFilename);
writer->SetInput(rescaler->GetOutput());
writer->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
// Figure \ref{fig:SVMCLASS} shows the result of the SVM classification.
// \begin{figure}
// \center
// \includegraphics[width=0.45\textwidth]{ROI_QB_MUL_1.eps}
// \includegraphics[width=0.45\textwidth]{ROI_QB_MUL_1_SVN_CLASS.eps}
// \itkcaption[SVM Image Classification]{Result of the SVM
// classification . Left: RGB image. Right: image of classes.}
// \label{fig:SVMCLASS}
// \end{figure}
// Software Guide : EndLatex
return EXIT_SUCCESS;
}
......@@ -52,15 +52,16 @@
#include <iostream>
// Software Guide : BeginCodeSnippet
#include "otbSVMImageModelEstimator.h"
#include "itkImageToListSampleAdaptor.h"
#include "otbSVMClassifier.h"
#include "otbLibSVMMachineLearningModel.h"
#include "itkImageToListSampleFilter.h"
#include "otbImageClassificationFilter.h"
// Software Guide : EndCodeSnippet
#include "otbImageFileWriter.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkScalarToRGBPixelFunctor.h"
#include "itkBinaryThresholdImageFilter.h"
#include "otbImageFileReader.h"
......@@ -92,13 +93,13 @@ int main(int itkNotUsed(argc), char *argv[])
// Software Guide : BeginLatex
//
// The \doxygen{otb}{SVMImageModelEstimator} class is templated over
// the input (features) and the training (labels) images.
// The \doxygen{otb}{LibSVMMachineLearningModel} class is templated over
// the input (features) and the training (labels) values.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::SVMImageModelEstimator<InputImageType,
TrainingImageType> EstimatorType;
typedef otb::LibSVMMachineLearningModel<InputPixelType,
InputPixelType> ModelType;
// Software Guide : EndCodeSnippet
......@@ -128,107 +129,67 @@ int main(int itkNotUsed(argc), char *argv[])
inputReader->SetFileName(inputImageFileName);
trainingReader->SetFileName(trainingImageFileName);
inputReader->Update();
trainingReader->Update();
//~ inputReader->Update();
//~ trainingReader->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now instantiate the model estimator and set its parameters.
// The input data is contained in images. Only label values greater than 0
// shall be used, so we create two iterators to fill the input and target
// ListSamples.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
EstimatorType::Pointer svmEstimator = EstimatorType::New();
svmEstimator->SetInputImage(inputReader->GetOutput());
svmEstimator->SetTrainingImage(trainingReader->GetOutput());
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The model estimation procedure is triggered by calling the
// estimator's \code{Update} method.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
svmEstimator->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now proceed to the image classification. We start by
// declaring the type of the image to be classified. ITK's
// classification framework needs the type of the pixel to be of
// fixed type, so we declare the following types.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::Image<itk::FixedArray<InputPixelType, 3>,
Dimension> ClassifyImageType;
typedef itk::BinaryThresholdImageFilter<TrainingImageType,TrainingImageType> ThresholdFilterType;
ThresholdFilterType::Pointer thresholder = ThresholdFilterType::New();
thresholder->SetInput(trainingReader->GetOutput());
thresholder->SetLowerThreshold(1);
thresholder->SetOutsideValue(0);
thresholder->SetInsideValue(1);
typedef otb::ImageFileReader<ClassifyImageType> ClassifyReaderType;
// Software Guide : EndCodeSnippet
typedef itk::Statistics::ImageToListSampleFilter<InputImageType,TrainingImageType> ImageToListSample;
typedef itk::Statistics::ImageToListSampleFilter<TrainingImageType,TrainingImageType> ImageToTargetListSample;
// Software Guide : BeginLatex
//
// We can now read the image by calling the \code{Update} method of the reader.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ClassifyReaderType::Pointer cReader = ClassifyReaderType::New();
ImageToListSample::Pointer imToList = ImageToListSample::New();
imToList->SetInput(inputReader->GetOutput());
imToList->SetMaskImage(thresholder->GetOutput());
imToList->SetMaskValue(1);
imToList->Update();
cReader->SetFileName(inputImageFileName);
ImageToTargetListSample::Pointer imToTargetList = ImageToTargetListSample::New();
imToTargetList->SetInput(trainingReader->GetOutput());
imToTargetList->SetMaskImage(thresholder->GetOutput());
imToTargetList->SetMaskValue(1);
imToTargetList->Update();
cReader->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The image has now to be transformed to a sample which
// is compatible with the classification framework. We will use a
// \doxygen{itk}{Statistics::ImageToListSampleAdaptor} for this
// task. This class is templated over the image type used for
// storing the measures.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::Statistics::ImageToListSampleAdaptor<ClassifyImageType> SampleType;
SampleType::Pointer sample = SampleType::New();
// Software Guide : EndCodeSnippet
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
// Software Guide : BeginLatex
//
// After instantiation, we can set the image as an imput of our
// sample adaptor.
// We can now instantiate the model and set its parameters.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ModelType::Pointer svmModel = ModelType::New();
svmModel->SetInputListSample(const_cast<ModelType::InputListSampleType*>(imToList->GetOutput()));
svmModel->SetTargetListSample(const_cast<ModelType::TargetListSampleType*>(imToTargetList->GetOutput()));
// Software Guide : BeginCodeSnippet
sample->SetImage(cReader->GetOutput());
// Software Guide : EndCodeSnippet
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
// Software Guide : BeginLatex
//
// Now, we need to declare the SVM model which is to be used by the
// classifier. The SVM model is templated over the type of value used
// for the measures and the type of pixel used for the labels. The
// model is obtained from the model estimator by calling the
// \code{GetModel} method.
// The model training procedure is triggered by calling the
// model's \code{Train} method.