diff --git a/Applications/Classification/CMakeLists.txt b/Applications/Classification/CMakeLists.txt index de0764e1516a01a789ac24cee1a93a542f2f32bc..00a08a4fd218aafee08147e7e562df770fc6796e 100644 --- a/Applications/Classification/CMakeLists.txt +++ b/Applications/Classification/CMakeLists.txt @@ -38,4 +38,8 @@ IF(OTB_USE_OPENCV) OTB_CREATE_APPLICATION(NAME TrainMachineLearningImagesClassifier SOURCES otbTrainMachineLearningImagesClassifier.cxx LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning;OTBMachineLearning) + + OTB_CREATE_APPLICATION(NAME ImageClassifier + SOURCES otbImageClassifier.cxx + LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning;OTBMachineLearning) ENDIF() diff --git a/Applications/Classification/otbImageClassifier.cxx b/Applications/Classification/otbImageClassifier.cxx new file mode 100644 index 0000000000000000000000000000000000000000..504579e04d5f03a6ce29be0b7a386c81f1310322 --- /dev/null +++ b/Applications/Classification/otbImageClassifier.cxx @@ -0,0 +1,178 @@ +/*========================================================================= + + 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 "otbWrapperApplication.h" +#include "otbWrapperApplicationFactory.h" + +#include "itkVariableLengthVector.h" +#include "otbChangeLabelImageFilter.h" +#include "otbStandardWriterWatcher.h" +#include "otbStatisticsXMLFileReader.h" +#include "otbShiftScaleVectorImageFilter.h" +#include "otbImageClassificationFilter.h" +#include "otbMultiToMonoChannelExtractROI.h" +#include "otbImageToVectorImageCastFilter.h" +#include "otbMachineLearningModelFactory.h" + +namespace otb +{ +namespace Wrapper +{ + +class ImageClassifier : public Application +{ +public: + /** Standard class typedefs. */ + typedef ImageClassifier Self; + typedef Application Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; + + /** Standard macro */ + itkNewMacro(Self); + + itkTypeMacro(ImageClassifier, otb::Application); + + /** Filters typedef */ + typedef itk::VariableLengthVector<FloatVectorImageType::InternalPixelType> MeasurementType; + typedef otb::StatisticsXMLFileReader<MeasurementType> StatisticsReader; + typedef otb::ShiftScaleVectorImageFilter<FloatVectorImageType, FloatVectorImageType> RescalerType; + typedef otb::ImageClassificationFilter<FloatVectorImageType, UInt8ImageType> ClassificationFilterType; + typedef ClassificationFilterType::Pointer ClassificationFilterPointerType; + typedef ClassificationFilterType::ModelType ModelType; + typedef ModelType::Pointer ModelPointerType; + typedef ClassificationFilterType::ValueType ValueType; + typedef ClassificationFilterType::LabelType LabelType; + typedef otb::MachineLearningModelFactory<ValueType, LabelType> MachineLearningModelFactoryType; + +private: + void DoInit() + { + SetName("ImageClassifier"); + SetDescription("Performs a classification of the input image according to a model file."); + + // Documentation + SetDocName("Image Classification"); + SetDocLongDescription("This application performs an image classification based on a model file (*.txt extension) produced by the TrainImagesClassifier application. Pixels of the output image will contain the class label decided by the classifier. The input pixels can be optionnaly centered and reduced according to the statistics file produced by the ComputeImagesStatistics application. An optional input mask can be provided, in which case only input image pixels whose corresponding mask value is greater than 0 will be classified. The remaining of pixels will be given the label 0 in the output image."); + + SetDocLimitations("The input image must have the same type, order and number of bands than the images used to produce the statistics file and the SVM model file. If a statistics file was used during training by the TrainSVMImagesClassifier, it is mandatory to use the same statistics file for classification. If an input mask is used, its size must match the input image size."); + SetDocAuthors("OTB-Team"); + SetDocSeeAlso("TrainImagesClassifier, ValidateImagesClassifier, ComputeImagesStatistics"); + + AddDocTag(Tags::Learning); + + AddParameter(ParameterType_InputImage, "in", "Input Image"); + SetParameterDescription( "in", "The input image to classify."); + + AddParameter(ParameterType_InputImage, "mask", "Input Mask"); + SetParameterDescription( "mask", "The mask allows to restrict classification of the input image to the area where mask pixel values are greater than 0."); + MandatoryOff("mask"); + + AddParameter(ParameterType_InputFilename, "model", "Model file"); + SetParameterDescription("model", "A model file (*.txt extension, produced by TrainImagesClassifier application)."); + + AddParameter(ParameterType_InputFilename, "imstat", "Statistics file"); + SetParameterDescription("imstat", "A XML file containing mean and standard deviation to center and reduce samples before classification (produced by ComputeImagesStatistics application)."); + MandatoryOff("imstat"); + + AddParameter(ParameterType_OutputImage, "out", "Output Image"); + SetParameterDescription( "out", "Output image containing class labels"); + SetParameterOutputImagePixelType( "out", ImagePixelType_uint8); + + AddRAMParameter(); + + // Doc example parameter settings + SetDocExampleParameterValue("in", "QB_1_ortho.tif"); + SetDocExampleParameterValue("imstat", "EstimateImageStatisticsQB1.xml"); + SetDocExampleParameterValue("model", "clsvmModelQB1.svm"); + SetDocExampleParameterValue("out", "clLabeledImageQB1.tif"); + } + + void DoUpdateParameters() + { + // Nothing to do here : all parameters are independent + } + + void DoExecute() + { + // Load input image + FloatVectorImageType::Pointer inImage = GetParameterImage("in"); + inImage->UpdateOutputInformation(); + + // Load svm model + otbAppLogINFO("Loading model"); + m_Model = MachineLearningModelFactoryType::CreateMachineLearningModel(GetParameterString("model"), + MachineLearningModelFactoryType::ReadMode); + m_Model->Load(GetParameterString("model")); + otbAppLogINFO("Model loaded"); + + // Normalize input image (optional) + StatisticsReader::Pointer statisticsReader = StatisticsReader::New(); + MeasurementType meanMeasurementVector; + MeasurementType stddevMeasurementVector; + m_Rescaler = RescalerType::New(); + + // Classify + m_ClassificationFilter = ClassificationFilterType::New(); + m_ClassificationFilter->SetModel(m_Model); + + // Normalize input image if asked + if(IsParameterEnabled("imstat") ) + { + otbAppLogINFO("Input image normalization activated."); + // Load input image statistics + statisticsReader->SetFileName(GetParameterString("imstat")); + meanMeasurementVector = statisticsReader->GetStatisticVectorByName("mean"); + stddevMeasurementVector = statisticsReader->GetStatisticVectorByName("stddev"); + otbAppLogINFO( "mean used: " << meanMeasurementVector ); + otbAppLogINFO( "standard deviation used: " << stddevMeasurementVector ); + // Rescale vector image + m_Rescaler->SetScale(stddevMeasurementVector); + m_Rescaler->SetShift(meanMeasurementVector); + m_Rescaler->SetInput(inImage); + + m_ClassificationFilter->SetInput(m_Rescaler->GetOutput()); + } + else + { + otbAppLogINFO("Input image normalization deactivated."); + m_ClassificationFilter->SetInput(inImage); + } + + + if(IsParameterEnabled("mask")) + { + otbAppLogINFO("Using input mask"); + // Load mask image and cast into LabeledImageType + UInt8ImageType::Pointer inMask = GetParameterUInt8Image("mask"); + + m_ClassificationFilter->SetInputMask(inMask); + } + + SetParameterOutputImage<UInt8ImageType>("out", m_ClassificationFilter->GetOutput()); + } + + ClassificationFilterType::Pointer m_ClassificationFilter; + ModelPointerType m_Model; + RescalerType::Pointer m_Rescaler; +}; + + +} +} + +OTB_APPLICATION_EXPORT(otb::Wrapper::ImageClassifier) diff --git a/Applications/Classification/otbTrainMachineLearningImagesClassifier.cxx b/Applications/Classification/otbTrainMachineLearningImagesClassifier.cxx index dbc5feff3337b24a2490b37c07d3cd007d703eb1..e4ee0c4ceeca46965180aa258b49d61c8d590716 100644 --- a/Applications/Classification/otbTrainMachineLearningImagesClassifier.cxx +++ b/Applications/Classification/otbTrainMachineLearningImagesClassifier.cxx @@ -636,7 +636,7 @@ private: confMatCalc->SetReferenceLabels(validationLabeledListSample); confMatCalc->SetProducedLabels(predictedList); - confMatCalc->Update(); + confMatCalc->Compute(); otbAppLogINFO("SVM training performances"); LogConfusionMatrix(confMatCalc); diff --git a/Code/UtilitiesAdapters/OpenCV/otbMachineLearningModelFactory.h b/Code/UtilitiesAdapters/OpenCV/otbMachineLearningModelFactory.h index b1c44b141a47f9652f4508f1570c8c6a9d7d036e..2cb89dd5e4061d692d396a183f7f31a7fd564c08 100644 --- a/Code/UtilitiesAdapters/OpenCV/otbMachineLearningModelFactory.h +++ b/Code/UtilitiesAdapters/OpenCV/otbMachineLearningModelFactory.h @@ -48,7 +48,7 @@ public: typedef enum { ReadMode, WriteMode } FileModeType; /** Create the appropriate MachineLearningModel depending on the particulars of the file. */ - static MachineLearningModelTypePointer CreateMachineLearningModel(const char* path, FileModeType mode); + static MachineLearningModelTypePointer CreateMachineLearningModel(const std::string& path, FileModeType mode); /** Register Built-in factories */ static void RegisterBuiltInFactories(); diff --git a/Code/UtilitiesAdapters/OpenCV/otbMachineLearningModelFactory.txx b/Code/UtilitiesAdapters/OpenCV/otbMachineLearningModelFactory.txx index debaefde12c0102bf89547d42267bc513e5ee661..6c869d2cb7981c2435666b2b960d113b96b34b4f 100644 --- a/Code/UtilitiesAdapters/OpenCV/otbMachineLearningModelFactory.txx +++ b/Code/UtilitiesAdapters/OpenCV/otbMachineLearningModelFactory.txx @@ -31,7 +31,7 @@ namespace otb template <class TInputValue, class TOutputValue> typename MachineLearningModel<TInputValue,TOutputValue>::Pointer MachineLearningModelFactory<TInputValue,TOutputValue> -::CreateMachineLearningModel(const char* path, FileModeType mode) +::CreateMachineLearningModel(const std::string& path, FileModeType mode) { RegisterBuiltInFactories();