From 735349bc35069f9e9f62610864e98f8c9100f775 Mon Sep 17 00:00:00 2001
From: Christophe Palmann <christophe.palmann@c-s.fr>
Date: Thu, 19 Mar 2015 15:16:50 +0100
Subject: [PATCH] ENH: fixed TrainImagesClassifier app comp. when libsvm and
 opencv modules are not installed

---
 .../AppClassification/app/otbTrainBoost.cxx   |  2 +
 .../app/otbTrainDecisionTree.cxx              |  3 +-
 .../app/otbTrainGradientBoostedTree.cxx       |  3 +-
 .../app/otbTrainImagesClassifier.cxx          | 42 ++++++++++++++++++-
 .../app/otbTrainImagesClassifier.h            | 15 ++++++-
 .../AppClassification/app/otbTrainKNN.cxx     |  3 +-
 .../AppClassification/app/otbTrainLibSVM.cxx  |  3 +-
 .../app/otbTrainNeuralNetwork.cxx             |  3 +-
 .../app/otbTrainNormalBayes.cxx               |  3 +-
 .../app/otbTrainRandomForests.cxx             |  3 +-
 .../AppClassification/app/otbTrainSVM.cxx     |  3 +-
 11 files changed, 71 insertions(+), 12 deletions(-)

diff --git a/Modules/Applications/AppClassification/app/otbTrainBoost.cxx b/Modules/Applications/AppClassification/app/otbTrainBoost.cxx
index 26d28cb361..c107f8016e 100644
--- a/Modules/Applications/AppClassification/app/otbTrainBoost.cxx
+++ b/Modules/Applications/AppClassification/app/otbTrainBoost.cxx
@@ -22,6 +22,7 @@ namespace otb
 {
 namespace Wrapper
 {
+#ifdef OTB_USE_OPENCV
   void TrainImagesClassifier::InitBoostParams()
   {
     AddChoice("classifier.boost", "Boost classifier");
@@ -68,6 +69,7 @@ namespace Wrapper
     boostClassifier->Train();
     boostClassifier->Save(GetParameterString("io.out"));
   }
+#endif
 
 } //end namespace wrapper
 } //end namespace otb
diff --git a/Modules/Applications/AppClassification/app/otbTrainDecisionTree.cxx b/Modules/Applications/AppClassification/app/otbTrainDecisionTree.cxx
index 604ada5134..9f94a7fc60 100644
--- a/Modules/Applications/AppClassification/app/otbTrainDecisionTree.cxx
+++ b/Modules/Applications/AppClassification/app/otbTrainDecisionTree.cxx
@@ -21,6 +21,7 @@ namespace otb
 {
 namespace Wrapper
 {
+#ifdef OTB_USE_OPENCV
 void TrainImagesClassifier::InitDecisionTreeParams()
 {
   AddChoice("classifier.dt", "Decision Tree classifier");
@@ -102,6 +103,6 @@ void TrainImagesClassifier::TrainDecisionTree(ListSampleType::Pointer trainingLi
   classifier->Train();
   classifier->Save(GetParameterString("io.out"));
 }
-
+#endif
 } //end namespace wrapper
 } //end namespace otb
diff --git a/Modules/Applications/AppClassification/app/otbTrainGradientBoostedTree.cxx b/Modules/Applications/AppClassification/app/otbTrainGradientBoostedTree.cxx
index ee3379542f..c703ad4c57 100644
--- a/Modules/Applications/AppClassification/app/otbTrainGradientBoostedTree.cxx
+++ b/Modules/Applications/AppClassification/app/otbTrainGradientBoostedTree.cxx
@@ -21,6 +21,7 @@ namespace otb
 {
 namespace Wrapper
 {
+#ifdef OTB_USE_OPENCV
 void TrainImagesClassifier::InitGradientBoostedTreeParams()
 {
   AddChoice("classifier.gbt", "Gradient Boosted Tree classifier");
@@ -80,6 +81,6 @@ void TrainImagesClassifier::TrainGradientBoostedTree(
   classifier->Train();
   classifier->Save(GetParameterString("io.out"));
 }
-
+#endif
 } //end namespace wrapper
 } //end namespace otb
diff --git a/Modules/Applications/AppClassification/app/otbTrainImagesClassifier.cxx b/Modules/Applications/AppClassification/app/otbTrainImagesClassifier.cxx
index ae99777833..ed76bb85e2 100644
--- a/Modules/Applications/AppClassification/app/otbTrainImagesClassifier.cxx
+++ b/Modules/Applications/AppClassification/app/otbTrainImagesClassifier.cxx
@@ -105,7 +105,9 @@ void TrainImagesClassifier::DoInit()
   SetParameterDescription("classifier", "Choice of the classifier to use for the training.");
 
   //Group LibSVM
+#ifdef OTB_USE_LIBSVM 
   InitLibSVMParams();
+#endif
 
 #ifdef OTB_USE_OPENCV
   InitSVMParams();
@@ -395,44 +397,80 @@ void TrainImagesClassifier::DoExecute()
   //--------------------------
   LabelListSampleType::Pointer predictedList = LabelListSampleType::New();
   const std::string classifierType = GetParameterString("classifier");
+
   if (classifierType == "libsvm")
     {
+	#ifdef OTB_USE_LIBSVM
     TrainLibSVM(trainingListSample, trainingLabeledListSample);
+    #elseif
+    otbAppLogFATAL("Module LIBSVM is not installed. You should consider turning OTB_USE_LIBSVM on during cmake configuration.");
+    #endif
     }
-#ifdef OTB_USE_OPENCV
   else if (classifierType == "svm")
     {
+	#ifdef OTB_USE_OPENCV
     TrainSVM(trainingListSample, trainingLabeledListSample);
+    #elseif
+    otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
+    #endif
     }
   else if (classifierType == "boost")
     {
+	#ifdef OTB_USE_OPENCV
     TrainBoost(trainingListSample, trainingLabeledListSample);
+    #elseif
+    otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
+    #endif
     }
   else if (classifierType == "dt")
     {
+	#ifdef OTB_USE_OPENCV
     TrainDecisionTree(trainingListSample, trainingLabeledListSample);
+    #elseif
+    otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
+    #endif
     }
   else if (classifierType == "gbt")
     {
+	#ifdef OTB_USE_OPENCV
     TrainGradientBoostedTree(trainingListSample, trainingLabeledListSample);
+    #elseif
+    otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
+    #endif
     }
   else if (classifierType == "ann")
     {
+	#ifdef OTB_USE_OPENCV
     TrainNeuralNetwork(trainingListSample, trainingLabeledListSample);
+    #elseif
+    otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
+    #endif
     }
   else if (classifierType == "bayes")
     {
+	#ifdef OTB_USE_OPENCV
     TrainNormalBayes(trainingListSample, trainingLabeledListSample);
+    #elseif
+    otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
+    #endif
     }
   else if (classifierType == "rf")
     {
+	#ifdef OTB_USE_OPENCV
     TrainRandomForests(trainingListSample, trainingLabeledListSample);
+    #elseif
+    otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
+    #endif
     }
   else if (classifierType == "knn")
     {
+	#ifdef OTB_USE_OPENCV
     TrainKNN(trainingListSample, trainingLabeledListSample);
+    #elseif
+    otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
+    #endif
     }
-#endif
+
 
   //--------------------------
   // Performances estimation
diff --git a/Modules/Applications/AppClassification/app/otbTrainImagesClassifier.h b/Modules/Applications/AppClassification/app/otbTrainImagesClassifier.h
index 253007b027..235613fd56 100644
--- a/Modules/Applications/AppClassification/app/otbTrainImagesClassifier.h
+++ b/Modules/Applications/AppClassification/app/otbTrainImagesClassifier.h
@@ -42,7 +42,9 @@
 # include "otbNeuralNetworkMachineLearningModel.h"
 #endif
 
+#ifdef OTB_USE_LIBSVM 
 #include "otbLibSVMMachineLearningModel.h"
+#endif
 
 // Statistic XML Reader
 #include "otbStatisticsXMLFileReader.h"
@@ -124,8 +126,11 @@ public:
   typedef otb::NeuralNetworkMachineLearningModel<InternalPixelType, ListSampleGeneratorType::ClassLabelType> NeuralNetworkType;
   typedef otb::NormalBayesMachineLearningModel<InternalPixelType, ListSampleGeneratorType::ClassLabelType> NormalBayesType;
 #endif
+
+#ifdef OTB_USE_LIBSVM 
   typedef otb::LibSVMMachineLearningModel<InternalPixelType, ListSampleGeneratorType::ClassLabelType> LibSVMType;
-  
+#endif
+ 
   // Estimate performance on validation sample
   typedef otb::ConfusionMatrixCalculator<LabelListSampleType, LabelListSampleType> ConfusionMatrixCalculatorType;
   typedef ConfusionMatrixCalculatorType::ConfusionMatrixType ConfusionMatrixType;
@@ -150,7 +155,10 @@ private:
 
   void LogConfusionMatrix(ConfusionMatrixCalculatorType* confMatCalc);
 
+#ifdef OTB_USE_LIBSVM 
   void InitLibSVMParams();
+#endif  
+  
 #ifdef OTB_USE_OPENCV
   void InitBoostParams();
   void InitSVMParams();
@@ -161,8 +169,11 @@ private:
   void InitRandomForestsParams();
   void InitKNNParams();
 #endif
-  
+
+#ifdef OTB_USE_LIBSVM 
   void TrainLibSVM(ListSampleType::Pointer trainingListSample, LabelListSampleType::Pointer trainingLabeledListSample);
+#endif 
+  
 #ifdef OTB_USE_OPENCV
   void TrainBoost(ListSampleType::Pointer trainingListSample, LabelListSampleType::Pointer trainingLabeledListSample);
   void TrainSVM(ListSampleType::Pointer trainingListSample, LabelListSampleType::Pointer trainingLabeledListSample);
diff --git a/Modules/Applications/AppClassification/app/otbTrainKNN.cxx b/Modules/Applications/AppClassification/app/otbTrainKNN.cxx
index bd4a97eda2..bc31cd4fce 100644
--- a/Modules/Applications/AppClassification/app/otbTrainKNN.cxx
+++ b/Modules/Applications/AppClassification/app/otbTrainKNN.cxx
@@ -22,6 +22,7 @@ namespace otb
 {
 namespace Wrapper
 {
+#ifdef OTB_USE_OPENCV
   void TrainImagesClassifier::InitKNNParams()
   {
     AddChoice("classifier.knn", "KNN classifier");
@@ -46,6 +47,6 @@ namespace Wrapper
     knnClassifier->Train();
     knnClassifier->Save(GetParameterString("io.out"));
   }
-
+#endif
 } //end namespace wrapper
 } //end namespace otb
diff --git a/Modules/Applications/AppClassification/app/otbTrainLibSVM.cxx b/Modules/Applications/AppClassification/app/otbTrainLibSVM.cxx
index 67293ca1e8..458e5b8487 100644
--- a/Modules/Applications/AppClassification/app/otbTrainLibSVM.cxx
+++ b/Modules/Applications/AppClassification/app/otbTrainLibSVM.cxx
@@ -22,6 +22,7 @@ namespace otb
 {
 namespace Wrapper
 {
+#ifdef OTB_USE_LIBSVM
   void TrainImagesClassifier::InitLibSVMParams()
   {
     AddChoice("classifier.libsvm", "LibSVM classifier");
@@ -78,6 +79,6 @@ namespace Wrapper
     libSVMClassifier->Train();
     libSVMClassifier->Save(GetParameterString("io.out"));
   }
-
+#endif
 } //end namespace wrapper
 } //end namespace otb
diff --git a/Modules/Applications/AppClassification/app/otbTrainNeuralNetwork.cxx b/Modules/Applications/AppClassification/app/otbTrainNeuralNetwork.cxx
index 6d9dc2bd2d..29b3657ad2 100644
--- a/Modules/Applications/AppClassification/app/otbTrainNeuralNetwork.cxx
+++ b/Modules/Applications/AppClassification/app/otbTrainNeuralNetwork.cxx
@@ -22,6 +22,7 @@ namespace otb
 {
 namespace Wrapper
 {
+#ifdef OTB_USE_OPENCV
 void TrainImagesClassifier::InitNeuralNetworkParams()
 {
   AddChoice("classifier.ann", "Artificial Neural Network classifier");
@@ -209,6 +210,6 @@ void TrainImagesClassifier::TrainNeuralNetwork(ListSampleType::Pointer trainingL
   classifier->Train();
   classifier->Save(GetParameterString("io.out"));
 }
-
+#endif
 } //end namespace wrapper
 } //end namespace otb
diff --git a/Modules/Applications/AppClassification/app/otbTrainNormalBayes.cxx b/Modules/Applications/AppClassification/app/otbTrainNormalBayes.cxx
index 65f6129d0a..d33e6eaf9c 100644
--- a/Modules/Applications/AppClassification/app/otbTrainNormalBayes.cxx
+++ b/Modules/Applications/AppClassification/app/otbTrainNormalBayes.cxx
@@ -22,6 +22,7 @@ namespace otb
 {
 namespace Wrapper
 {
+#ifdef OTB_USE_OPENCV
   void TrainImagesClassifier::InitNormalBayesParams()
   {
     AddChoice("classifier.bayes", "Normal Bayes classifier");
@@ -39,6 +40,6 @@ namespace Wrapper
     classifier->Train();
     classifier->Save(GetParameterString("io.out"));
   }
-
+#endif
 } //end namespace wrapper
 } //end namespace otb
diff --git a/Modules/Applications/AppClassification/app/otbTrainRandomForests.cxx b/Modules/Applications/AppClassification/app/otbTrainRandomForests.cxx
index daa742e746..1798b9d9b5 100644
--- a/Modules/Applications/AppClassification/app/otbTrainRandomForests.cxx
+++ b/Modules/Applications/AppClassification/app/otbTrainRandomForests.cxx
@@ -21,6 +21,7 @@ namespace otb
 {
 namespace Wrapper
 {
+#ifdef OTB_USE_OPENCV
 void TrainImagesClassifier::InitRandomForestsParams()
 {
   AddChoice("classifier.rf", "Random forests classifier");
@@ -111,6 +112,6 @@ void TrainImagesClassifier::TrainRandomForests(ListSampleType::Pointer trainingL
   classifier->Train();
   classifier->Save(GetParameterString("io.out"));
 }
-
+#endif
 } //end namespace wrapper
 } //end namespace otb
diff --git a/Modules/Applications/AppClassification/app/otbTrainSVM.cxx b/Modules/Applications/AppClassification/app/otbTrainSVM.cxx
index 90ad92c205..a41e103746 100644
--- a/Modules/Applications/AppClassification/app/otbTrainSVM.cxx
+++ b/Modules/Applications/AppClassification/app/otbTrainSVM.cxx
@@ -22,6 +22,7 @@ namespace otb
 {
 namespace Wrapper
 {
+#ifdef OTB_USE_OPENCV
   void TrainImagesClassifier::InitSVMParams()
   {
     AddChoice("classifier.svm", "SVM classifier (OpenCV)");
@@ -75,7 +76,6 @@ namespace Wrapper
                             "because the samples are not identically processed within OpenCV.");
   }
 
-
   void TrainImagesClassifier::TrainSVM(ListSampleType::Pointer trainingListSample, LabelListSampleType::Pointer trainingLabeledListSample)
   {
     SVMType::Pointer SVMClassifier = SVMType::New();
@@ -152,6 +152,7 @@ namespace Wrapper
     SetParameterFloat("classifier.svm.gamma", static_cast<float> (SVMClassifier->GetOutputGamma()));
     SetParameterFloat("classifier.svm.degree", static_cast<float> (SVMClassifier->GetOutputDegree()));
   }
+#endif
 
 } //end namespace wrapper
 } //end namespace otb
-- 
GitLab