OpenCV 2.4.2

org.opencv.ml
Class CvKNearest

java.lang.Object
  extended by org.opencv.ml.CvStatModel
      extended by org.opencv.ml.CvKNearest

public class CvKNearest
extends CvStatModel

The class implements K-Nearest Neighbors model as described in the beginning of this section.

See Also:
org.opencv.ml.CvKNearest : public CvStatModel

Field Summary
 
Fields inherited from class org.opencv.ml.CvStatModel
nativeObj
 
Constructor Summary
  CvKNearest()
          Default and training constructors.
protected CvKNearest(long addr)
           
  CvKNearest(Mat trainData, Mat responses)
          Default and training constructors.
  CvKNearest(Mat trainData, Mat responses, Mat sampleIdx, boolean isRegression, int max_k)
          Default and training constructors.
 
Method Summary
protected  void finalize()
           
 float find_nearest(Mat samples, int k, Mat results, Mat neighborResponses, Mat dists)
          Finds the neighbors and predicts responses for input vectors.
 boolean train(Mat trainData, Mat responses)
          Trains the model.
 boolean train(Mat trainData, Mat responses, Mat sampleIdx, boolean isRegression, int maxK, boolean updateBase)
          Trains the model.
 
Methods inherited from class org.opencv.ml.CvStatModel
load, load, save, save
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CvKNearest

public CvKNearest()

Default and training constructors.

See "CvKNearest.train" for additional parameters descriptions.

See Also:
org.opencv.ml.CvKNearest.CvKNearest

CvKNearest

protected CvKNearest(long addr)

CvKNearest

public CvKNearest(Mat trainData,
                  Mat responses)

Default and training constructors.

See "CvKNearest.train" for additional parameters descriptions.

Parameters:
trainData - a trainData
responses - a responses
See Also:
org.opencv.ml.CvKNearest.CvKNearest

CvKNearest

public CvKNearest(Mat trainData,
                  Mat responses,
                  Mat sampleIdx,
                  boolean isRegression,
                  int max_k)

Default and training constructors.

See "CvKNearest.train" for additional parameters descriptions.

Parameters:
trainData - a trainData
responses - a responses
sampleIdx - a sampleIdx
isRegression - a isRegression
max_k - a max_k
See Also:
org.opencv.ml.CvKNearest.CvKNearest
Method Detail

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class CvStatModel
Throws:
java.lang.Throwable

find_nearest

public float find_nearest(Mat samples,
                          int k,
                          Mat results,
                          Mat neighborResponses,
                          Mat dists)

Finds the neighbors and predicts responses for input vectors.

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.

Parameters:
samples - Input samples stored by rows. It is a single-precision floating-point matrix of number_of_samples x number_of_features size.
k - Number of used nearest neighbors. It must satisfy constraint: k <= "CvKNearest.get_max_k".
results - Vector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with number_of_samples elements.
neighborResponses - Optional output values for corresponding neighbors. It is a single-precision floating-point matrix of number_of_samples x k size.
dists - a dists
See Also:
org.opencv.ml.CvKNearest.find_nearest

train

public boolean train(Mat trainData,
                     Mat responses)

Trains the model.

The method trains the K-Nearest model. It follows the conventions of the generic "CvStatModel.train" approach with the following limitations:

Parameters:
trainData - a trainData
responses - a responses
See Also:
org.opencv.ml.CvKNearest.train

train

public boolean train(Mat trainData,
                     Mat responses,
                     Mat sampleIdx,
                     boolean isRegression,
                     int maxK,
                     boolean updateBase)

Trains the model.

The method trains the K-Nearest model. It follows the conventions of the generic "CvStatModel.train" approach with the following limitations:

Parameters:
trainData - a trainData
responses - a responses
sampleIdx - a sampleIdx
isRegression - Type of the problem: true for regression and false for classification.
maxK - Number of maximum neighbors that may be passed to the method "CvKNearest.find_nearest".
updateBase - Specifies whether the model is trained from scratch (update_base=false), or it is updated using the new training data (update_base=true). In the latter case, the parameter maxK must not be larger than the original value.
See Also:
org.opencv.ml.CvKNearest.train

Official OpenCV 2.4 Documentation