This is the 23rd day of my participation in Gwen Challenge

OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.

This article shows you how to implement number recognition using OpenCV in Python3.

The basic principle of

1. Load the training image

OpenCV installation directory:

2. Image segmentation

The size of this image is 2000*1000 and there are 5K handwritten characters. It can be calculated that each number occupies about 400 pixels, so the image is divided into 20 *20 pieces.

# Split the image into5000Piece of transverse100The longitudinal50, is divided into20*20
cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)]
Copy the code

3. Gray processing

Grayscale the segmented picture, so that the original RGB multidimensional data into two-dimensional grayscale data, convenient processing.

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
Copy the code

4. Data matrix

The gray image is converted into a matrix and divided into 100 pieces horizontally and 50 pieces vertically.

# split np.hsplit(array, copies), split np.vsplit(array, copies) # make it a Numpy array of size (50.100.20.20)
x = np.array(cells)
Copy the code

5. Assign training set and test set

50 columns are allocated, with the first 50 as the training set and the last 50 as the test set.

Test_data train = x[:,: :50].reshape(-1.400).astype(np.float32)
test = x[:,50:100].reshape(-1.400).astype(np.float32)
Copy the code

6. Calibrate the training test set

It is convenient to determine the exact number in each block.

# create test, train tag k = np.arange(10)
train_labels = np.repeat(k,250)[:,np.newaxis]
test_labels = train_labels.copy()
Copy the code

7. Create KNN adjacencies

Training model.

Initialize KNN, train data, then use k =1KNN = cv2.ml.KNearest_create() KNN. train(train,cv2.ml.ROW_SAMPLE,train_labels) KNN = cv2.ml.Copy the code

8. Use test sets

Test model accuracy.

matches = result==test_labels
correct = np.count_nonzero(matches)
accuracy = correct*100.0/result.size
print(accuracy)
Copy the code

The implementation code

Training code

import numpy as np
import cv2

img = cv2.imread('F: \ \ yolo - 3.3 \ \ data \ \ opencv opencv \ \ sources \ \ samples \ \ data \ \ who PNG') gray = cv2.cvtcolor (img, cv2.color_bgr2gray) #5000Piece of transverse100The longitudinal50, is divided into20*20Cells = [np.hsplit(row, rows, rows, rows, rows, rows, rows, rows)100) for row in np.vsplit(gray,50)] # make it a Numpy array of size (50.100.20.20) x = np.array(cells) # train_data = x[:,: :50].reshape(-1.400).astype(np.float32)
test = x[:,50:100].reshape(-1.400).astype(np.float32) # create test, train tag k = np.arange(10)
train_labels = np.repeat(k,250)[:,np.newaxis] test_labels = train_allelages.copy () # initialize KNN, train data, and then use k =1Test data to test it. KNN = cv2 ml. KNearest_create (KNN). The "train" (" train ", cv2. Ml. ROW_SAMPLE, train_labels) ret, result, neighbours, dist = knn.findNearest(test,k=5)#test

matches = result==test_labels
correct = np.count_nonzero(matches)
accuracy = correct*100.0/result.size
print(accuracy)
Copy the code

The training output is:

Test input:

The test code

import numpy as np
importCv2 # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the input image processing part -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- test_img = cv2. Imread ('E:\\demo\\demo.png') # Zoom test image to20*20
height, width = test_img.shape[:2]
size = (int(width/25), int(height/25)) res = cv2.resize(test_img,size, Res_gray = Cv2.cvtcolor (res, cv2.color_bgr2gray) # Generate equal length pure white255The matrix test_array = Np.array (res_gray) g25 = Np.ones (test_array.shape) g25 = g25*2550 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01.400(np). Astype. Float32) print (input_test. Size) # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- input image processing is complete -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- img = cv2.imread('F: \ \ yolo - 3.3 \ \ data \ \ opencv opencv \ \ sources \ \ samples \ \ data \ \ who PNG') gray = cv2.cvtcolor (img, cv2.color_bgr2gray) #5000Piece of transverse100The longitudinal50, is divided into20*20Cells = [np.hsplit(row, rows, rows, rows, rows, rows, rows, rows)100) for row in np.vsplit(gray,50)] # make it a Numpy array of size (50.100.20.20) x = np.array(cells) # train_data = x[:,: :50].reshape(-1.400).astype(np.float32)
test = x[:,50:100].reshape(-1.400).astype(np.float32) # create test, train tag k = np.arange(10)
train_labels = np.repeat(k,250)[:,np.newaxis] test_labels = train_allelages.copy () # initialize KNN, train data, and then use k =1Test data to test it. KNN = cv2 ml. KNearest_create (KNN). The "train" (" train ", cv2. Ml. ROW_SAMPLE, train_labels) ret, result, neighbours, dist = knn.findNearest(input_test,k=5Print (type(result)) print(result)Copy the code

Test output:

A series of articles will follow this month,

Wonderful article, please pay attention.