21 #ifndef otbNormalBayesMachineLearningModel_hxx
22 #define otbNormalBayesMachineLearningModel_hxx
32 template <
class TInputValue,
class TOutputValue>
34 : m_NormalBayesModel(cv::ml::NormalBayesClassifier::create())
39 template <
class TInputValue,
class TOutputValue>
44 otb::ListSampleToMat<InputListSampleType>(this->GetInputListSample(), samples);
48 otb::ListSampleToMat<TargetListSampleType>(this->GetTargetListSample(), labels);
50 cv::Mat var_type = cv::Mat(this->GetInputListSample()->GetMeasurementVectorSize() + 1, 1, CV_8U);
52 var_type.at<uchar>(this->GetInputListSample()->GetMeasurementVectorSize(), 0) =
CV_VAR_CATEGORICAL;
54 m_NormalBayesModel->train(cv::ml::TrainData::create(samples, cv::ml::ROW_SAMPLE, labels, cv::noArray(), cv::noArray(), cv::noArray(), var_type));
57 template <
class TInputValue,
class TOutputValue>
66 otb::SampleToMat<InputSampleType>(input, sample);
68 cv::Mat missing = cv::Mat(1, input.Size(), CV_8U);
70 double result = m_NormalBayesModel->predict(sample);
72 target[0] =
static_cast<TOutputValue
>(result);
74 if (quality !=
nullptr)
76 if (!this->HasConfidenceIndex())
78 itkExceptionMacro(
"Confidence index not available for this classifier !");
81 if (proba !=
nullptr && !this->m_ProbaIndex)
82 itkExceptionMacro(
"Probability per class not available for this classifier !");
87 template <
class TInputValue,
class TOutputValue>
90 cv::FileStorage fs(filename, cv::FileStorage::WRITE);
91 fs << (name.empty() ? m_NormalBayesModel->getDefaultName() : cv::String(name)) <<
"{";
92 m_NormalBayesModel->write(fs);
97 template <
class TInputValue,
class TOutputValue>
100 cv::FileStorage fs(filename, cv::FileStorage::READ);
101 m_NormalBayesModel->read(name.empty() ? fs.getFirstTopLevelNode() : fs[name]);
104 template <
class TInputValue,
class TOutputValue>
112 std::cerr <<
"Could not read file " << file << std::endl;
119 std::getline(ifs, line);
121 if (line.find(
CV_TYPE_NAME_ML_NBAYES) != std::string::npos || line.find(m_NormalBayesModel->getDefaultName()) != std::string::npos)
130 template <
class TInputValue,
class TOutputValue>
136 template <
class TInputValue,
class TOutputValue>
140 Superclass::PrintSelf(os, indent);