In this video you will find an easy explanation of how the KNN algorythm works for handwritten digits recognition.
We use a sample of 2500 digits (250 of each type 0 to 9) to train the algorythm and we have another small sample to test it and see if the Knn algorythm can accurately read handwritten digits.

Source code:

import cv2
import numpy as np

digits = cv2.imread("digits.png", cv2.IMREAD_GRAYSCALE)
test_digits = cv2.imread("test_digits.png", cv2.IMREAD_GRAYSCALE)

rows = np.vsplit(digits, 50)
cells = []
for row in rows:
    row_cells = np.hsplit(row, 50)
    for cell in row_cells:
        cell = cell.flatten()
cells = np.array(cells, dtype=np.float32)

k = np.arange(10)
cells_labels = np.repeat(k, 250)

test_digits = np.vsplit(test_digits, 50)
test_cells = []
for d in test_digits:
    d = d.flatten()
test_cells = np.array(test_cells, dtype=np.float32)

knn =
knn.train(cells,, cells_labels)
ret, result, neighbours, dist = knn.findNearest(test_cells, k=3)




  1. digits.png
  2. test_digits.png