OTB  9.0.0
Orfeo Toolbox
otbOpenCVUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbOpenCVUtils_h
22 #define otbOpenCVUtils_h
23 
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
30 #else
31 #include <opencv2/core/core.hpp>
32 #include <opencv2/core/core_c.h>
33 #endif
34 
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
40 #else
41 #include <opencv2/ml/ml.hpp>
42 #endif
43 
44 #include "otb_opencv_api.h"
45 
46 #include "OTBSupervisedExport.h"
47 
48 #include "itkListSample.h"
49 
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"
56 
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
61 
62 #define CV_VAR_NUMERICAL cv::ml::VAR_NUMERICAL
63 #define CV_VAR_CATEGORICAL cv::ml::VAR_CATEGORICAL
64 
65 
66 namespace otb
67 {
68 template <class T>
69 void SampleToMat(const T& sample, cv::Mat& output)
70 {
71  output.create(1, sample.Size(), CV_32FC1);
72 
73  // Loop on sample size
74  for (unsigned int i = 0; i < sample.Size(); ++i)
75  {
76  output.at<float>(0, i) = sample[i];
77  }
78 }
79 
80 
86 template <class T>
87 void ListSampleToMat(const T* listSample, cv::Mat& output)
88 {
89  // Sample index
90  unsigned int sampleIdx = 0;
91 
92  // Check for valid listSample
93  if (listSample != nullptr && listSample->Size() > 0)
94  {
95  // Retrieve samples count
96  unsigned int sampleCount = listSample->Size();
97 
98  // Build an iterator
99  typename T::ConstIterator sampleIt = listSample->Begin();
100 
101  // Retrieve samples size alike
102  const unsigned int sampleSize = listSample->GetMeasurementVectorSize();
103 
104  // Allocate CvMat
105  output.create(sampleCount, sampleSize, CV_32FC1);
106 
107  // Fill the cv matrix
108  for (; sampleIt != listSample->End(); ++sampleIt, ++sampleIdx)
109  {
110  // Retrieve sample
111  typename T::MeasurementVectorType sample = sampleIt.GetMeasurementVector();
112 
113  // Loop on sample size
114  for (unsigned int i = 0; i < sampleSize; ++i)
115  {
116  output.at<float>(sampleIdx, i) = sample[i];
117  }
118  }
119  }
120 }
121 
122 template <typename T>
123 void ListSampleToMat(typename T::Pointer listSample, cv::Mat& output)
124 {
125  return ListSampleToMat(listSample.GetPointer(), output);
126 }
127 
128 template <typename T>
129 void ListSampleToMat(typename T::ConstPointer listSample, cv::Mat& output)
130 {
131  return ListSampleToMat(listSample.GetPointer(), output);
132 }
133 
134 template <typename T>
135 typename T::Pointer MatToListSample(const cv::Mat& cvmat)
136 {
137  // Build output type
138  typename T::Pointer output = T::New();
139 
140  // Get samples count
141  unsigned sampleCount = cvmat.rows;
142 
143  // Get samples size
144  unsigned int sampleSize = cvmat.cols;
145 
146  // Loop on samples
147  for (unsigned int i = 0; i < sampleCount; ++i)
148  {
149  typename T::MeasurementVectorType sample;
150  itk::NumericTraits<typename T::MeasurementVectorType>::SetLength(sample, sampleSize);
151 
152  unsigned int realSampleSize = sample.Size();
153 
154  for (unsigned int j = 0; j < realSampleSize; ++j)
155  {
156  // Don't forget to cast
157  sample[j] = static_cast<typename T::MeasurementVectorType::ValueType>(cvmat.at<float>(i, j));
158  }
159  // PushBack the new sample
160  output->PushBack(sample);
161  }
162  // return the output
163  return output;
164 }
165 }
166 
167 #endif
otb::SampleToMat
void SampleToMat(const T &sample, cv::Mat &output)
Definition: otbOpenCVUtils.h:69
otb::ListSampleToMat
void ListSampleToMat(const T *listSample, cv::Mat &output)
Definition: otbOpenCVUtils.h:87
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MatToListSample
T::Pointer MatToListSample(const cv::Mat &cvmat)
Definition: otbOpenCVUtils.h:135