This experiment code has been uploaded bread, please click to buy, or pay attention to [all code farming] public number, reply [nearest neighbor], limited time free access!

The overview

In this experiment, OpenCV, Python and other tools are used to identify and detect handwritten numbers using appropriate algorithms. Training sets and test sets can be provided for training, and handwritten fonts can be input for testing.

By completing this experiment independently, I can greatly improve my understanding of Python and OpenCV, and master the feature extraction technology of handwritten numbers and the nearest neighbor template matching method.

The environment that

System: Window 10

Software: PyCharm Community Edition 2020.2.2×64

Language: Python 3.6

Environment: Cv2, numpy, Matplotlib.pyplot, time

The data set

This dataset consists of 100 images, with numbers 0-9, 10 images for each number, 8 training sets, and 2 test sets. As follows:

Figure 1 The number zero Figure 2 number one
Figure 3 number two Figure 4 the number three

The terms defined

Training set: Model parameters can be trained to fit the model to the correct prediction function.

Test set: Tests are performed on trained models to assess the final model generalization ability.

Validation set: used to adjust overparameters and monitor whether the model has been fitted.

specifications

The experiment content,

\1. Learn to use the method of line and line scanning to obtain handwritten numbers up, down, left, right position of the subroutine.

\2. Design the feature extraction algorithm of handwritten digits, and write the corresponding feature extraction program.

\3. Write handwritten number recognition program based on nearest neighbor template matching.

Experimental results and analysis

Record the input numbers 0-9 10 each, the program after the operation of the corresponding recognition results, and in-depth analysis of the results.

\1. Record the effect drawing of feature extraction for each sample.

\2. Record the normalized eigenvalues of the sample.

\3. Calculate the correct recognition rate of each number. And the situation of wrong recognition is analyzed deeply.

The software design

I. System design drawing

Function listing

The function name Function Description Function interface Description
ImgToNp() Import the path of the image into the matrix. Input: None Output: image path matrix
outFeature() Process and label characteristic functions. Input: picture path matrix Output: training picture matrix set and tag set
star() Program according to the preset structure of the cycle test and output. Input: training picture matrix set and label set Output: training results
TestImgToNp() Enter the test picture and convert it to a matrix. Enter: test picture address. Output: Test picture matrix.
outfeatureImg() Output characteristic graph and characteristic value. Input: training picture matrix set Output: Save feature graphs and feature values as files. \

The code is introduced

(1) Mainapp.py code

Mainapp.py is the main program, which contains the functions listed in Table 1.

ImgToNp() is used to convert 80 sample images from 0-9 folders in img folder into matrices and save them in variables and return them.

4. Experimental results

After running mainapp.py, the output box looks like this:

\1.Image address to import matrix...... \2.Prepare to extract features...... \3.Features are stored in the feature matrix...... \4.Ready to label...... \5.Label to finish with...... \6.Whether to output characteristic graph and eigenvalue? y/n:Copy the code

When y is input, the program outputs the characteristic values and graphs of 80 samples, which are stored in “./fandimg/{0-9} “and”./fandimg/featureArray/{0-9} “respectively. The diagram below:

The program continues to run, and the output box outputs: 1. 3-9. BMP this picture is judged to be wrong, the system judges: 1. [[1.1.1.]] but the correct result is: 4 3.5-10. BMP is incorrectly judged by the system, 1 The three closest results are: [[5.1.1.]] But the correct result is: 5 4.6-10. 1 The three closest results are: [[1.1.1.]] but the correct result is: 6 5.7-9. BMP the three closest results are: [[1.1.]] but the correct result is: 7 6.8-9. 3 The three closest results are: [[8.9.3.]] but the correct result is: 8 7.8-10.bmp This figure is wrong, the system judged that: 9 The three closest results are: [[8.9.9.]] but the correct result is: 8 8.9-9. BMP is incorrectly judged by the system. The three closest results are: [[9.7.1.]], but the correct result is: 9Copy the code

f