手写数字识别(三)
数字分割、识别
1. 数字分割
一个比较简单的方法是通过统计图像的灰度进行划分。先统计每一行的灰度值,对于遇到的空白区间作为划分行,然后以同样方法划分列,以此确定分区。但是很显然这样的方法对于数字密集或者是轻微的倾斜都会有问题。
我写的是通过bfs来划分,首先是通过膨胀处理,然后对于所有未标记的黑点走一次bfs,把同一个联通块看做是一个数字,并且在bfs的同时打上标记,以此划分为一个box,包含唯一的标记以及box长宽等。同时注意到数字 “5” 的一横可能会与本体分离,对于这种情况考虑所有宽比长大数倍的box(因为正常数字都是长比宽大),找一个最近的box进行合并,如果最近的box距离过远,那么判断这个box是一个废弃的噪音。通过这个方法可以很好地分割出数字
效果:
2. 数字识别
最基本的就是SVM,但是普通SVM效果并不好,需要使用hog做特征工程才能使得效果更好。
一个比较方便的方法是使用 Keras 构建神经网络,对 MNIST数据集进行训练。网上也有很多教程,这里用的是CNN。
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# reshape to be [samples][pixels][width][height]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
X_train = X_train / 255
X_test = X_test / 255
X_train = numpy.round(X_train)*1.0
X_test = numpy.round(X_test)*1.0
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
def baseline_model():
# create model
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(1, 28, 28), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
# build the model
model = baseline_model()
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=1, batch_size=200)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))
model.save('cnn_model.h5')
由于前期是使用 C++ 做的分割,后面用python识别,在Ubuntu下采取的是先把数字切割下来保存起来,再通过c++调用python运行识别
char command[1000];
FILE *fp;
/*将要执行的命令写入buf*/
string python_order = "python digit_rec.py "+file_name;
snprintf(command, sizeof(command), python_order.c_str());
/*执行预先设定的命令,并读出该命令的标准输出*/
fp = popen(command, "r");
if(NULL == fp) {
perror("popen执行失败!");
exit(1);
}
printf("DONE");
另外数字图像保存是默认 28*28 的(训练集的原因),对图像保存是找到对应的box,采用比box稍大的正方形矩阵,把box的内容(即数字)复制到矩形中,由于box中的黑色像素打了标记的,就算两个box有重叠区域也不会有关系。再把这个矩形resize为 28*28 的进行保存
3. 最终结果
包含原图的四个角点以及识别后的数字