OpenCV for Unity 2.6.4
Enox Software / Please refer to OpenCV official document ( http://docs.opencv.org/4.10.0/index.html ) for the details of the argument of the method.
Loading...
Searching...
No Matches
OpenCVForUnity.MlModule.KNearest Class Reference

The class implements K-Nearest Neighbors model. More...

Public Member Functions

float findNearest (Mat samples, int k, Mat results)
 Finds the neighbors and predicts responses for input vectors.
 
float findNearest (Mat samples, int k, Mat results, Mat neighborResponses)
 Finds the neighbors and predicts responses for input vectors.
 
float findNearest (Mat samples, int k, Mat results, Mat neighborResponses, Mat dist)
 Finds the neighbors and predicts responses for input vectors.
 
int getAlgorithmType ()
 
int getDefaultK ()
 
int getEmax ()
 
bool getIsClassifier ()
 
void setAlgorithmType (int val)
 
void setDefaultK (int val)
 
void setEmax (int val)
 
void setIsClassifier (bool val)
 
- Public Member Functions inherited from OpenCVForUnity.MlModule.StatModel
float calcError (TrainData data, bool test, Mat resp)
 Computes error on the training or test dataset.
 
override bool empty ()
 Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read.
 
int getVarCount ()
 Returns the number of variables in training samples.
 
bool isClassifier ()
 Returns true if the model is classifier.
 
bool isTrained ()
 Returns true if the model is trained.
 
virtual float predict (Mat samples)
 Predicts response(s) for the provided sample(s)
 
virtual float predict (Mat samples, Mat results)
 Predicts response(s) for the provided sample(s)
 
virtual float predict (Mat samples, Mat results, int flags)
 Predicts response(s) for the provided sample(s)
 
bool train (Mat samples, int layout, Mat responses)
 Trains the statistical model.
 
bool train (TrainData trainData)
 Trains the statistical model.
 
bool train (TrainData trainData, int flags)
 Trains the statistical model.
 
- Public Member Functions inherited from OpenCVForUnity.CoreModule.Algorithm
virtual void clear ()
 Clears the algorithm state.
 
virtual string getDefaultName ()
 
IntPtr getNativeObjAddr ()
 
void save (string filename)
 
- Public Member Functions inherited from OpenCVForUnity.DisposableObject
void Dispose ()
 
void ThrowIfDisposed ()
 

Static Public Member Functions

static new KNearest __fromPtr__ (IntPtr addr)
 
static KNearest create ()
 Creates the empty model.
 
static KNearest load (string filepath)
 Loads and creates a serialized knearest from a file.
 
- Static Public Member Functions inherited from OpenCVForUnity.MlModule.StatModel
static new StatModel __fromPtr__ (IntPtr addr)
 
- Static Public Member Functions inherited from OpenCVForUnity.CoreModule.Algorithm
static Algorithm __fromPtr__ (IntPtr addr)
 
- Static Public Member Functions inherited from OpenCVForUnity.DisposableObject
static IntPtr ThrowIfNullIntPtr (IntPtr ptr)
 

Static Public Attributes

const int BRUTE_FORCE = 1
 
const int KDTREE = 2
 
- Static Public Attributes inherited from OpenCVForUnity.MlModule.StatModel
const int COMPRESSED_INPUT = 2
 
const int PREPROCESSED_INPUT = 4
 
const int RAW_OUTPUT = 1
 
const int UPDATE_MODEL = 1
 

Protected Member Functions

override void Dispose (bool disposing)
 
- Protected Member Functions inherited from OpenCVForUnity.MlModule.StatModel
override void Dispose (bool disposing)
 
- Protected Member Functions inherited from OpenCVForUnity.CoreModule.Algorithm
- Protected Member Functions inherited from OpenCVForUnity.DisposableOpenCVObject
 DisposableOpenCVObject ()
 
 DisposableOpenCVObject (bool isEnabledDispose)
 
 DisposableOpenCVObject (IntPtr ptr)
 
 DisposableOpenCVObject (IntPtr ptr, bool isEnabledDispose)
 
- Protected Member Functions inherited from OpenCVForUnity.DisposableObject
 DisposableObject ()
 
 DisposableObject (bool isEnabledDispose)
 

Additional Inherited Members

- Package Functions inherited from OpenCVForUnity.MlModule.StatModel
- Package Functions inherited from OpenCVForUnity.CoreModule.Algorithm
- Package Attributes inherited from OpenCVForUnity.DisposableOpenCVObject
- Properties inherited from OpenCVForUnity.DisposableObject
bool IsDisposed [get, protected set]
 
bool IsEnabledDispose [get, set]
 

Detailed Description

The class implements K-Nearest Neighbors model.

See also
ml_intro_knn

Member Function Documentation

◆ __fromPtr__()

static new KNearest OpenCVForUnity.MlModule.KNearest.__fromPtr__ ( IntPtr addr)
static

◆ create()

static KNearest OpenCVForUnity.MlModule.KNearest.create ( )
static

Creates the empty model.

The static method creates empty KNearest classifier. It should be then trained using StatModel.train method.

◆ Dispose()

override void OpenCVForUnity.MlModule.KNearest.Dispose ( bool disposing)
protectedvirtual

◆ findNearest() [1/3]

float OpenCVForUnity.MlModule.KNearest.findNearest ( Mat samples,
int k,
Mat results )

Finds the neighbors and predicts responses for input vectors.

Parameters
samplesInput samples stored by rows. It is a single-precision floating-point matrix of <number_of_samples> * k size.
kNumber of used nearest neighbors. Should be greater than 1.
resultsVector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with <number_of_samples> elements.
neighborResponsesOptional output values for corresponding neighbors. It is a single- precision floating-point matrix of <number_of_samples> * k size.
distOptional output distances from the input vectors to the corresponding neighbors. It is a single-precision floating-point matrix of <number_of_samples> * k size.

For each input vector (a row of the matrix samples), the method finds the k nearest neighbors. In case of regression, the predicted result is a mean value of the particular vector's neighbor responses. In case of classification, the class is determined by voting.

For each input vector, the neighbors are sorted by their distances to the vector.

In case of C++ interface you can use output pointers to empty matrices and the function will allocate memory itself.

If only a single input vector is passed, all output matrices are optional and the predicted value is returned by the method.

The function is parallelized with the TBB library.

◆ findNearest() [2/3]

float OpenCVForUnity.MlModule.KNearest.findNearest ( Mat samples,
int k,
Mat results,
Mat neighborResponses )

Finds the neighbors and predicts responses for input vectors.

Parameters
samplesInput samples stored by rows. It is a single-precision floating-point matrix of <number_of_samples> * k size.
kNumber of used nearest neighbors. Should be greater than 1.
resultsVector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with <number_of_samples> elements.
neighborResponsesOptional output values for corresponding neighbors. It is a single- precision floating-point matrix of <number_of_samples> * k size.
distOptional output distances from the input vectors to the corresponding neighbors. It is a single-precision floating-point matrix of <number_of_samples> * k size.

For each input vector (a row of the matrix samples), the method finds the k nearest neighbors. In case of regression, the predicted result is a mean value of the particular vector's neighbor responses. In case of classification, the class is determined by voting.

For each input vector, the neighbors are sorted by their distances to the vector.

In case of C++ interface you can use output pointers to empty matrices and the function will allocate memory itself.

If only a single input vector is passed, all output matrices are optional and the predicted value is returned by the method.

The function is parallelized with the TBB library.

◆ findNearest() [3/3]

float OpenCVForUnity.MlModule.KNearest.findNearest ( Mat samples,
int k,
Mat results,
Mat neighborResponses,
Mat dist )

Finds the neighbors and predicts responses for input vectors.

Parameters
samplesInput samples stored by rows. It is a single-precision floating-point matrix of <number_of_samples> * k size.
kNumber of used nearest neighbors. Should be greater than 1.
resultsVector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with <number_of_samples> elements.
neighborResponsesOptional output values for corresponding neighbors. It is a single- precision floating-point matrix of <number_of_samples> * k size.
distOptional output distances from the input vectors to the corresponding neighbors. It is a single-precision floating-point matrix of <number_of_samples> * k size.

For each input vector (a row of the matrix samples), the method finds the k nearest neighbors. In case of regression, the predicted result is a mean value of the particular vector's neighbor responses. In case of classification, the class is determined by voting.

For each input vector, the neighbors are sorted by their distances to the vector.

In case of C++ interface you can use output pointers to empty matrices and the function will allocate memory itself.

If only a single input vector is passed, all output matrices are optional and the predicted value is returned by the method.

The function is parallelized with the TBB library.

◆ getAlgorithmType()

int OpenCVForUnity.MlModule.KNearest.getAlgorithmType ( )

◆ getDefaultK()

int OpenCVForUnity.MlModule.KNearest.getDefaultK ( )
See also
setDefaultK

◆ getEmax()

int OpenCVForUnity.MlModule.KNearest.getEmax ( )
See also
setEmax

◆ getIsClassifier()

bool OpenCVForUnity.MlModule.KNearest.getIsClassifier ( )
See also
setIsClassifier

◆ load()

static KNearest OpenCVForUnity.MlModule.KNearest.load ( string filepath)
static

Loads and creates a serialized knearest from a file.

Use KNearest.save to serialize and store an KNearest to disk. Load the KNearest from this file again, by calling this function with the path to the file.

Parameters
filepathpath to serialized KNearest

◆ setAlgorithmType()

void OpenCVForUnity.MlModule.KNearest.setAlgorithmType ( int val)

◆ setDefaultK()

void OpenCVForUnity.MlModule.KNearest.setDefaultK ( int val)
See also
getDefaultK

◆ setEmax()

void OpenCVForUnity.MlModule.KNearest.setEmax ( int val)
See also
getEmax

◆ setIsClassifier()

void OpenCVForUnity.MlModule.KNearest.setIsClassifier ( bool val)
See also
getIsClassifier

Member Data Documentation

◆ BRUTE_FORCE

const int OpenCVForUnity.MlModule.KNearest.BRUTE_FORCE = 1
static

◆ KDTREE

const int OpenCVForUnity.MlModule.KNearest.KDTREE = 2
static

The documentation for this class was generated from the following file: