21 #ifndef otbOpenCVUtils_h
22 #define otbOpenCVUtils_h
24 #if defined(__GNUC__) || defined(__clang__)
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wcast-align"
27 #include <opencv2/core/core.hpp>
28 #include <opencv2/core/core_c.h>
29 #pragma GCC diagnostic pop
31 #include <opencv2/core/core.hpp>
32 #include <opencv2/core/core_c.h>
35 #if defined(__GNUC__) || defined(__clang__)
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
38 #include <opencv2/ml/ml.hpp>
39 #pragma GCC diagnostic pop
41 #include <opencv2/ml/ml.hpp>
44 #include "otb_opencv_api.h"
46 #include "OTBSupervisedExport.h"
48 #include "itkListSample.h"
50 #define CV_TYPE_NAME_ML_SVM "opencv-ml-svm"
51 #define CV_TYPE_NAME_ML_RTREES "opencv-ml-random-trees"
52 #define CV_TYPE_NAME_ML_BOOSTING "opencv-ml-boost-tree"
53 #define CV_TYPE_NAME_ML_ANN_MLP "opencv-ml-ann-mlp"
54 #define CV_TYPE_NAME_ML_NBAYES "opencv-ml-bayesian"
55 #define CV_TYPE_NAME_ML_TREE "opencv-ml-tree"
57 #define CvSVM cv::ml::SVM
58 #define CvANN_MLP_TrainParams cv::ml::ANN_MLP
59 #define CvANN_MLP cv::ml::ANN_MLP
60 #define CvBoost cv::ml::Boost
62 #define CV_VAR_NUMERICAL cv::ml::VAR_NUMERICAL
63 #define CV_VAR_CATEGORICAL cv::ml::VAR_CATEGORICAL
71 output.create(1, sample.Size(), CV_32FC1);
74 for (
unsigned int i = 0; i < sample.Size(); ++i)
76 output.at<
float>(0, i) = sample[i];
90 unsigned int sampleIdx = 0;
93 if (listSample !=
nullptr && listSample->Size() > 0)
96 unsigned int sampleCount = listSample->Size();
99 typename T::ConstIterator sampleIt = listSample->Begin();
102 const unsigned int sampleSize = listSample->GetMeasurementVectorSize();
105 output.create(sampleCount, sampleSize, CV_32FC1);
108 for (; sampleIt != listSample->End(); ++sampleIt, ++sampleIdx)
111 typename T::MeasurementVectorType sample = sampleIt.GetMeasurementVector();
114 for (
unsigned int i = 0; i < sampleSize; ++i)
116 output.at<
float>(sampleIdx, i) = sample[i];
122 template <
typename T>
128 template <
typename T>
134 template <
typename T>
138 typename T::Pointer output = T::New();
141 unsigned sampleCount = cvmat.rows;
144 unsigned int sampleSize = cvmat.cols;
147 for (
unsigned int i = 0; i < sampleCount; ++i)
149 typename T::MeasurementVectorType sample;
150 itk::NumericTraits<typename T::MeasurementVectorType>::SetLength(sample, sampleSize);
152 unsigned int realSampleSize = sample.Size();
154 for (
unsigned int j = 0; j < realSampleSize; ++j)
157 sample[j] =
static_cast<typename T::MeasurementVectorType::ValueType
>(cvmat.at<
float>(i, j));
160 output->PushBack(sample);