This project implements a handwritten digit recognition system from scratch using template matching and K-Nearest Neighbors (KNN) classification. A self-prepared dataset was created by extracting 50 digit images (0–9) from a handwritten sheet, with 5 samples per class. Each digit is resized to a uniform 32×32 grayscale format for consistency. For classification, the Zero-mean Normalized Cross Correlation (ZNCC) metric is used to measure similarity between the input digit and stored templates. A generic zncc() function compares pixel-wise structure while compensating for lighting and contrast differences, ensuring robust matching. The classify_digit() function applies ZNCC to all stored templates, sorts them by similarity, and uses KNN (k=3) to predict the most likely digit class. The code supports easy testing with new digit images of the same size as the templates, bypassing the need for template sliding. This approach demonstrates a simplified yet effective alternative to deep learning for digit classification, useful in constrained environments. The project includes all components from preprocessing to prediction and shows strong accuracy for clean, centered handwritten digits.
sohaib0075/Template-Matching
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|