findNearest method

(Mat, Mat, Mat) findNearest(
  1. Mat samples,
  2. int k
)

Finds the k nearest neighbors of each row of samples and predicts their responses.

Returns the predicted responses, the responses of the neighbors (one row of k values per sample) and the distances to them.

Implementation

(Mat results, Mat neighborResponses, Mat dist) findNearest(Mat samples, int k) {
  final p = calloc<ffi.Float>();
  final results = Mat.empty();
  final neighborResponses = Mat.empty();
  final dist = Mat.empty();
  try {
    cvRun(
      () => cml.cv_ml_KNearest_findNearest(
        ref,
        samples.ref,
        k,
        results.ref,
        neighborResponses.ref,
        dist.ref,
        p,
        ffi.nullptr,
      ),
    );
    return (results, neighborResponses, dist);
  } catch (_) {
    results.dispose();
    neighborResponses.dispose();
    dist.dispose();
    rethrow;
  } finally {
    calloc.free(p);
  }
}