123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- using OpenCVForUnity.CoreModule;
- using OpenCVForUnity.UtilsModule;
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- namespace OpenCVForUnity.MlModule
- {
- // C++: class StatModel
- /**
- * Base class for statistical models in OpenCV ML.
- */
- public class StatModel : Algorithm
- {
- protected override void Dispose(bool disposing)
- {
- try
- {
- if (disposing)
- {
- }
- if (IsEnabledDispose)
- {
- if (nativeObj != IntPtr.Zero)
- ml_StatModel_delete(nativeObj);
- nativeObj = IntPtr.Zero;
- }
- }
- finally
- {
- base.Dispose(disposing);
- }
- }
- protected internal StatModel(IntPtr addr) : base(addr) { }
- // internal usage only
- public static new StatModel __fromPtr__(IntPtr addr) { return new StatModel(addr); }
- // C++: enum cv.ml.StatModel.Flags
- public const int UPDATE_MODEL = 1;
- public const int RAW_OUTPUT = 1;
- public const int COMPRESSED_INPUT = 2;
- public const int PREPROCESSED_INPUT = 4;
- //
- // C++: int cv::ml::StatModel::getVarCount()
- //
- /**
- * Returns the number of variables in training samples
- * return automatically generated
- */
- public int getVarCount()
- {
- ThrowIfDisposed();
- return ml_StatModel_getVarCount_10(nativeObj);
- }
- //
- // C++: bool cv::ml::StatModel::empty()
- //
- public override bool empty()
- {
- ThrowIfDisposed();
- return ml_StatModel_empty_10(nativeObj);
- }
- //
- // C++: bool cv::ml::StatModel::isTrained()
- //
- /**
- * Returns true if the model is trained
- * return automatically generated
- */
- public bool isTrained()
- {
- ThrowIfDisposed();
- return ml_StatModel_isTrained_10(nativeObj);
- }
- //
- // C++: bool cv::ml::StatModel::isClassifier()
- //
- /**
- * Returns true if the model is classifier
- * return automatically generated
- */
- public bool isClassifier()
- {
- ThrowIfDisposed();
- return ml_StatModel_isClassifier_10(nativeObj);
- }
- //
- // C++: bool cv::ml::StatModel::train(Ptr_TrainData trainData, int flags = 0)
- //
- /**
- * Trains the statistical model
- *
- * param trainData training data that can be loaded from file using TrainData::loadFromCSV or
- * created with TrainData::create.
- * param flags optional flags, depending on the model. Some of the models can be updated with the
- * new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).
- * return automatically generated
- */
- public bool train(TrainData trainData, int flags)
- {
- ThrowIfDisposed();
- if (trainData != null) trainData.ThrowIfDisposed();
- return ml_StatModel_train_10(nativeObj, trainData.getNativeObjAddr(), flags);
- }
- /**
- * Trains the statistical model
- *
- * param trainData training data that can be loaded from file using TrainData::loadFromCSV or
- * created with TrainData::create.
- * new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).
- * return automatically generated
- */
- public bool train(TrainData trainData)
- {
- ThrowIfDisposed();
- if (trainData != null) trainData.ThrowIfDisposed();
- return ml_StatModel_train_11(nativeObj, trainData.getNativeObjAddr());
- }
- //
- // C++: bool cv::ml::StatModel::train(Mat samples, int layout, Mat responses)
- //
- /**
- * Trains the statistical model
- *
- * param samples training samples
- * param layout See ml::SampleTypes.
- * param responses vector of responses associated with the training samples.
- * return automatically generated
- */
- public bool train(Mat samples, int layout, Mat responses)
- {
- ThrowIfDisposed();
- if (samples != null) samples.ThrowIfDisposed();
- if (responses != null) responses.ThrowIfDisposed();
- return ml_StatModel_train_12(nativeObj, samples.nativeObj, layout, responses.nativeObj);
- }
- //
- // C++: float cv::ml::StatModel::calcError(Ptr_TrainData data, bool test, Mat& resp)
- //
- /**
- * Computes error on the training or test dataset
- *
- * param data the training data
- * param test if true, the error is computed over the test subset of the data, otherwise it's
- * computed over the training subset of the data. Please note that if you loaded a completely
- * different dataset to evaluate already trained classifier, you will probably want not to set
- * the test subset at all with TrainData::setTrainTestSplitRatio and specify test=false, so
- * that the error is computed for the whole new set. Yes, this sounds a bit confusing.
- * param resp the optional output responses.
- *
- * The method uses StatModel::predict to compute the error. For regression models the error is
- * computed as RMS, for classifiers - as a percent of missclassified samples (0%-100%).
- * return automatically generated
- */
- public float calcError(TrainData data, bool test, Mat resp)
- {
- ThrowIfDisposed();
- if (data != null) data.ThrowIfDisposed();
- if (resp != null) resp.ThrowIfDisposed();
- return ml_StatModel_calcError_10(nativeObj, data.getNativeObjAddr(), test, resp.nativeObj);
- }
- //
- // C++: float cv::ml::StatModel::predict(Mat samples, Mat& results = Mat(), int flags = 0)
- //
- /**
- * Predicts response(s) for the provided sample(s)
- *
- * param samples The input samples, floating-point matrix
- * param results The optional output matrix of results.
- * param flags The optional flags, model-dependent. See cv::ml::StatModel::Flags.
- * return automatically generated
- */
- public virtual float predict(Mat samples, Mat results, int flags)
- {
- ThrowIfDisposed();
- if (samples != null) samples.ThrowIfDisposed();
- if (results != null) results.ThrowIfDisposed();
- return ml_StatModel_predict_10(nativeObj, samples.nativeObj, results.nativeObj, flags);
- }
- /**
- * Predicts response(s) for the provided sample(s)
- *
- * param samples The input samples, floating-point matrix
- * param results The optional output matrix of results.
- * return automatically generated
- */
- public virtual float predict(Mat samples, Mat results)
- {
- ThrowIfDisposed();
- if (samples != null) samples.ThrowIfDisposed();
- if (results != null) results.ThrowIfDisposed();
- return ml_StatModel_predict_11(nativeObj, samples.nativeObj, results.nativeObj);
- }
- /**
- * Predicts response(s) for the provided sample(s)
- *
- * param samples The input samples, floating-point matrix
- * return automatically generated
- */
- public virtual float predict(Mat samples)
- {
- ThrowIfDisposed();
- if (samples != null) samples.ThrowIfDisposed();
- return ml_StatModel_predict_12(nativeObj, samples.nativeObj);
- }
- #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
- const string LIBNAME = "__Internal";
- #else
- const string LIBNAME = "opencvforunity";
- #endif
- // C++: int cv::ml::StatModel::getVarCount()
- [DllImport(LIBNAME)]
- private static extern int ml_StatModel_getVarCount_10(IntPtr nativeObj);
- // C++: bool cv::ml::StatModel::empty()
- [DllImport(LIBNAME)]
- [return: MarshalAs(UnmanagedType.U1)]
- private static extern bool ml_StatModel_empty_10(IntPtr nativeObj);
- // C++: bool cv::ml::StatModel::isTrained()
- [DllImport(LIBNAME)]
- [return: MarshalAs(UnmanagedType.U1)]
- private static extern bool ml_StatModel_isTrained_10(IntPtr nativeObj);
- // C++: bool cv::ml::StatModel::isClassifier()
- [DllImport(LIBNAME)]
- [return: MarshalAs(UnmanagedType.U1)]
- private static extern bool ml_StatModel_isClassifier_10(IntPtr nativeObj);
- // C++: bool cv::ml::StatModel::train(Ptr_TrainData trainData, int flags = 0)
- [DllImport(LIBNAME)]
- [return: MarshalAs(UnmanagedType.U1)]
- private static extern bool ml_StatModel_train_10(IntPtr nativeObj, IntPtr trainData_nativeObj, int flags);
- [DllImport(LIBNAME)]
- [return: MarshalAs(UnmanagedType.U1)]
- private static extern bool ml_StatModel_train_11(IntPtr nativeObj, IntPtr trainData_nativeObj);
- // C++: bool cv::ml::StatModel::train(Mat samples, int layout, Mat responses)
- [DllImport(LIBNAME)]
- [return: MarshalAs(UnmanagedType.U1)]
- private static extern bool ml_StatModel_train_12(IntPtr nativeObj, IntPtr samples_nativeObj, int layout, IntPtr responses_nativeObj);
- // C++: float cv::ml::StatModel::calcError(Ptr_TrainData data, bool test, Mat& resp)
- [DllImport(LIBNAME)]
- private static extern float ml_StatModel_calcError_10(IntPtr nativeObj, IntPtr data_nativeObj, [MarshalAs(UnmanagedType.U1)] bool test, IntPtr resp_nativeObj);
- // C++: float cv::ml::StatModel::predict(Mat samples, Mat& results = Mat(), int flags = 0)
- [DllImport(LIBNAME)]
- private static extern float ml_StatModel_predict_10(IntPtr nativeObj, IntPtr samples_nativeObj, IntPtr results_nativeObj, int flags);
- [DllImport(LIBNAME)]
- private static extern float ml_StatModel_predict_11(IntPtr nativeObj, IntPtr samples_nativeObj, IntPtr results_nativeObj);
- [DllImport(LIBNAME)]
- private static extern float ml_StatModel_predict_12(IntPtr nativeObj, IntPtr samples_nativeObj);
- // native support for java finalize()
- [DllImport(LIBNAME)]
- private static extern void ml_StatModel_delete(IntPtr nativeObj);
- }
- }
|