Skip to content

K-nearest-neighbour classification

Algorithm atlasMachine learning

Predict a sample from the labels or values of nearby training examples.

Try it on your image ↓

VISUAL WALKTHROUGHMachine learning
REFERENCE INPUTLabelled training observations

The white point is an unlabelled query in the same feature space.

RESULTAggregate the neighbour labels

The query receives the mint class, with 5 of five votes.

STEP 03 / 03

Aggregate their labels or numeric responses.

Distances and votes are computed from the displayed two-dimensional sample set.

Try it on an image

YOUR IMAGE · REAL OPENCV

Experiment at pixel level

Open full lab ↗

Train a colour classifier from the rectangle (foreground) and image border (background). Classify pixels by their nearest training colours.

The engine loads on your first run. Your images stay in this browser.

Input448 × 320
OutputWaiting for a result

Scroll over either image to zoom at the pointer. Use the scrollbars to pan both views over the same relative area. Zoom is relative to the input; pixel coordinates belong to each image. Warps can change scene correspondence.

Pixel inspector RGBA · native values · matched scale · 9 × 9 output pixels
Hover to inspect. Click to pin a pixel.
Input
Select a pixel

Output
Select a pixel

Sample models and licenses

When to use it

Build a simple classifier for small descriptor sets or establish a baseline before fitting a more complex model.

How it works

  1. 01Store training feature vectors and responses.
  2. 02Find the closest K vectors to a query.
  3. 03Aggregate their labels or numeric responses.

query → K nearest examples → vote or average

What to tune

K trades local sensitivity against smoothing; isClassifier selects classification or regression behavior.

Where it breaks down

Prediction depends strongly on feature scaling and distance. Query cost and memory grow with the stored training data.

TypeScript API

Open an entry for its exact overloads, parameter descriptions, result ownership and pinned upstream source.

All of these calls execute on the CPU. Native objects need explicit disposal. See matrices and ownership and build compatibility.