Package org.opencv.ml

## Class KNearest

- java.lang.Object
- - org.opencv.core.Algorithm
  - - org.opencv.ml.StatModel
    - - org.opencv.ml.KNearest

-     public class KNearest
      extends StatModel

  The class implements K-Nearest Neighbors model SEE: REF: ml_intro_knn

- - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static int` | `BRUTE_FORCE` |
    `static int` | `KDTREE` |

    - ### Fields inherited from class org.opencv.ml.StatModel

      `COMPRESSED_INPUT, PREPROCESSED_INPUT, RAW_OUTPUT, UPDATE_MODEL`

    - ### Fields inherited from class org.opencv.core.Algorithm

      `nativeObj`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor | Description
    --- | --- | ---
    `protected` | `KNearest​(long addr)` |

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `static KNearest` | `__fromPtr__​(long addr)` |
    `static KNearest` | `create()` | Creates the empty model The static method creates empty %KNearest classifier.
    `protected void` | `finalize()` |
    `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()` | SEE: setAlgorithmType
    `int` | `getDefaultK()` | SEE: setDefaultK
    `int` | `getEmax()` | SEE: setEmax
    `boolean` | `getIsClassifier()` | SEE: setIsClassifier
    `static KNearest` | `load​(java.lang.String filepath)` | Loads and creates a serialized knearest from a file Use KNearest::save to serialize and store an KNearest to disk.
    `void` | `setAlgorithmType​(int val)` | getAlgorithmType SEE: getAlgorithmType
    `void` | `setDefaultK​(int val)` | getDefaultK SEE: getDefaultK
    `void` | `setEmax​(int val)` | getEmax SEE: getEmax
    `void` | `setIsClassifier​(boolean val)` | getIsClassifier SEE: getIsClassifier

    - ### Methods inherited from class org.opencv.ml.StatModel

      `calcError, empty, getVarCount, isClassifier, isTrained, predict, predict, predict, train, train, train`

    - ### Methods inherited from class org.opencv.core.Algorithm

      `clear, getDefaultName, getNativeObjAddr, save`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### BRUTE_FORCE

          public static final int BRUTE_FORCE

      See Also:
        Constant Field Values

    - #### KDTREE

          public static final int KDTREE

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### KNearest

          protected KNearest​(long addr)

  - ### Method Detail

    - #### __fromPtr__

          public static KNearest __fromPtr__​(long addr)

    - #### create

          public static KNearest create()

      Creates the empty model The static method creates empty %KNearest classifier. It should be then trained using StatModel::train method.
      Returns:
        automatically generated

    - #### load

          public static KNearest load​(java.lang.String filepath)

      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:
        `filepath` - path to serialized KNearest
      Returns:
        automatically generated

    - #### getIsClassifier

          public boolean getIsClassifier()

      SEE: setIsClassifier
      Returns:
        automatically generated

    - #### findNearest

          public float findNearest​(Mat samples,
                                   int k,
                                   Mat results,
                                   Mat neighborResponses,
                                   Mat dist)

      Finds the neighbors and predicts responses for input vectors.
      Parameters:
        `samples` - Input samples stored by rows. It is a single-precision floating-point matrix of `&lt;number_of_samples&gt; * k` size.
        `k` - Number of used nearest neighbors. Should be greater than 1.
        `results` - Vector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with `&lt;number_of_samples&gt;` elements.
        `neighborResponses` - Optional output values for corresponding neighbors. It is a single- precision floating-point matrix of `&lt;number_of_samples&gt; * k` size.
        `dist` - Optional output distances from the input vectors to the corresponding neighbors. It is a single-precision floating-point matrix of `&lt;number_of_samples&gt; * 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.
      Returns:
        automatically generated

    - #### findNearest

          public float findNearest​(Mat samples,
                                   int k,
                                   Mat results,
                                   Mat neighborResponses)

      Finds the neighbors and predicts responses for input vectors.
      Parameters:
        `samples` - Input samples stored by rows. It is a single-precision floating-point matrix of `&lt;number_of_samples&gt; * k` size.
        `k` - Number of used nearest neighbors. Should be greater than 1.
        `results` - Vector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with `&lt;number_of_samples&gt;` elements.
        `neighborResponses` - Optional output values for corresponding neighbors. It is a single- precision floating-point matrix of `&lt;number_of_samples&gt; * k` size. is a single-precision floating-point matrix of `&lt;number_of_samples&gt; * 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.
      Returns:
        automatically generated

    - #### findNearest

          public float findNearest​(Mat samples,
                                   int k,
                                   Mat results)

      Finds the neighbors and predicts responses for input vectors.
      Parameters:
        `samples` - Input samples stored by rows. It is a single-precision floating-point matrix of `&lt;number_of_samples&gt; * k` size.
        `k` - Number of used nearest neighbors. Should be greater than 1.
        `results` - Vector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with `&lt;number_of_samples&gt;` elements. precision floating-point matrix of `&lt;number_of_samples&gt; * k` size. is a single-precision floating-point matrix of `&lt;number_of_samples&gt; * 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.
      Returns:
        automatically generated

    - #### getAlgorithmType

          public int getAlgorithmType()

      SEE: setAlgorithmType
      Returns:
        automatically generated

    - #### getDefaultK

          public int getDefaultK()

      SEE: setDefaultK
      Returns:
        automatically generated

    - #### getEmax

          public int getEmax()

      SEE: setEmax
      Returns:
        automatically generated

    - #### setAlgorithmType

          public void setAlgorithmType​(int val)

      getAlgorithmType SEE: getAlgorithmType
      Parameters:
        `val` - automatically generated

    - #### setDefaultK

          public void setDefaultK​(int val)

      getDefaultK SEE: getDefaultK
      Parameters:
        `val` - automatically generated

    - #### setEmax

          public void setEmax​(int val)

      getEmax SEE: getEmax
      Parameters:
        `val` - automatically generated

    - #### setIsClassifier

          public void setIsClassifier​(boolean val)

      getIsClassifier SEE: getIsClassifier
      Parameters:
        `val` - automatically generated

    - #### finalize

          protected void finalize()
                           throws java.lang.Throwable

      Overrides:
        `finalize` in class `StatModel`
      Throws:
        `java.lang.Throwable`