KNearest class

The class implements K-Nearest Neighbors model. https://docs.opencv.org/4.x/dd/de1/classcv_1_1ml_1_1KNearest.html

Inheritance
Available extensions

Constructors

KNearest.create()
Creates an empty model. Use one of the train methods to train it, or KNearest.load.
factory
KNearest.fromPointer(KNearestPtr ptr, [bool attach = true])
factory
KNearest.load(String filepath)
Loads a model saved with save from filepath. Throws CvException when the file does not hold a valid model.
factory
KNearest.loadFromString(String strModel, {String objname = ''})
Loads a model from the content of a file saved with save (e.g. a Flutter asset).
factory

Properties

algorithmType ↔ int
Algorithm type, one of BRUTE_FORCE, KDTREE.
getter/setter pair
defaultK ↔ int
Default number of neighbors to use in predict method.
getter/setter pair
defaultName → String
The name used when saving the model, opencv_ml_knn, opencv_ml_knn_kd with KDTREE.
no setter
emax ↔ int
Parameter for KDTree implementation.
getter/setter pair
hashCode → int
The hash code for this object.
no setterinherited
isClassifier ↔ bool
Whether classification or regression model should be trained.
getter/setter pair
isDisposed → bool
Whether this object has been disposed.
no setterinherited
isEmpty → bool
no setter
isTrained → bool
no setter
props → List<Object?>
no setterinherited
ptr ↔ Pointer<KNearest>
getter/setter pairinherited
ref → KNearest
A view of the native memory behind ptr, not a copy.
no setteroverride
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
varCount → int
Number of variables in training samples.
no setter

Methods

calcError(TrainData data, bool test) → (double, Mat)
Computes the error on the training (test false) or test (test true) part of data.
calcErrorAsync(TrainData data, bool test) → Future<(double, Mat)>

Available on KNearest, provided by the KNearestAsync extension

clear() → void
Calls OpenCV's clear, which does not reset this model: it stays trained. Create a new model to start over.
dispose() → void
Releases the underlying native resources. Idempotent: calling it more than once is safe, but the object must not be used after dispose.
inherited
findNearest(Mat samples, int k) → (Mat, Mat, Mat)
Finds the k nearest neighbors of each row of samples and predicts their responses.
findNearestAsync(Mat samples, int k) → Future<(Mat, Mat, Mat)>

Available on KNearest, provided by the KNearestAsync extension

freeNative() → void
Frees the native resources owned by this object. Subclasses implement the concrete free logic here; callers must use dispose instead.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
predict(Mat samples, {int flags = 0}) → Mat
Predicts the response for each row of samples, one row of the returned Mat per sample.
predictAsync(Mat samples, {int flags = 0}) → Future<Mat>

Available on KNearest, provided by the KNearestAsync extension

save(String filename) → void
Saves the model to filename (.xml, .yml or .json).
toString() → String
A string representation of this object.
inherited
train(Mat samples, int layout, Mat responses) → bool
Trains the model on samples (CV_32F, one sample per row for ROW_SAMPLE).
trainAsync(Mat samples, int layout, Mat responses) → Future<bool>

Available on KNearest, provided by the KNearestAsync extension

trainWithData(TrainData data, {int flags = 0}) → bool
Trains the model on data, flags may be StatModel.UPDATE_MODEL or model-specific flags.
trainWithDataAsync(TrainData data, {int flags = 0}) → Future<bool>

Available on KNearest, provided by the KNearestAsync extension

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Properties

finalizer → NativeFinalizer
final

Constants

BRUTE_FORCE → const int
KDTREE → const int