OpenCV & C++实现KNN手写字体识别

1目标

OpenCV & C++实现KNN手写字体识别

2代码

头文件:Knn.h

#include<opencv2\ml\ml.hpp>
#include<highgui\highgui.hpp>
#include<iostream>

#ifndef  _DIANBIAONUM_   
#define  _DIANBIAONUM_

#endif

源文件:Knn.cpp

#include "opencv2\opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::ml;

int main()
{
    Ptr<KNearest> model;
    fstream file;
    string fileName = "num_knn_pixel.yml";
    file.open(fileName.c_str(), ios::in);

    if (file)
    {
		//训练结果存在,加载训练结果
        model = Algorithm::load<KNearest>("num_knn_pixel.yml");
    }
    else
    {	//训练结果不存在,重新训练
        Mat img = imread("D:/Program Files/opencv/sources/samples/data/digits.png");
        Mat gray;
        cvtColor(img, gray, CV_BGR2GRAY);
        int b = 20;
        int m = gray.rows / b;   //原图为1000*2000
        int n = gray.cols / b;   //裁剪为5000个20*20的小图块
        Mat data, labels;   //特征矩阵
        for (int i = 0; i < n; i++)
        {
            int offsetCol = i*b; //列上的偏移量
            for (int j = 0; j < m; j++)
            {
                int offsetRow = j*b;  //行上的偏移量
                                      //截取20*20的小块
                Mat tmp;
                gray(Range(offsetRow, offsetRow + b), Range(offsetCol, offsetCol + b)).copyTo(tmp);
                //reshape  0:通道不变  其他数字,表示要设置的通道数
                //reshape  表示矩阵行数,如果设置为0,则表示保持原有行数不变,如果设置为其他数字,表示要设置的行数
                data.push_back(tmp.reshape(0, 1));  //序列化后放入特征矩阵
                labels.push_back((int)j / 5);  //对应的标注
            }

        }
        data.convertTo(data, CV_32F); //uchar型转换为cv_32f
        int samplesNum = data.rows;
        int trainNum = 3000;
        Mat trainData, trainLabels;
        trainData = data(Range(0, trainNum), Range::all());   //前3000个样本为训练数据
        trainLabels = labels(Range(0, trainNum), Range::all());

        //使用KNN算法
        int K = 5;
        Ptr<TrainData> tData = TrainData::create(trainData, ROW_SAMPLE, trainLabels);
        model = KNearest::create();
        model->setDefaultK(K);
        model->setIsClassifier(true);
        model->train(tData);

        //预测分类
        double train_hr = 0, test_hr = 0;
        Mat response;
        // compute prediction error on train and test data
        for (int i = 0; i < samplesNum; i++)
        {
            Mat sample = data.row(i);
            float r = model->predict(sample);   //对所有行进行预测
                                                //预测结果与原结果相比,相等为1,不等为0
            r = std::abs(r - labels.at<int>(i)) <= FLT_EPSILON ? 1.f : 0.f;

            if (i < trainNum)
                train_hr += r;  //累积正确数
            else
                test_hr += r;
        }

        test_hr /= samplesNum - trainNum;
        train_hr = trainNum > 0 ? train_hr / trainNum : 1.;

        printf("accuracy: train = %.1f%%, test = %.1f%%\n",
            train_hr*100., test_hr*100.);
		//保存训练结果
        model->save("./num_knn_pixel.yml");
    }
    ===============================预测部分===============================
    //预测分类
    Mat img = imread("6.png");
    cvtColor(img, img, COLOR_BGR2GRAY);
    //threshold(src, src, 0, 255, CV_THRESH_OTSU);
    imshow("原图像", img);
    resize(img, img, Size(20, 20));
    Mat test;
    test.push_back(img.reshape(0, 1));
    test.convertTo(test, CV_32F);
    int result = model->predict(test);

    cout << "我猜你写的是:" << result << endl;
    while (char(waitKey(1)) != 'q') {}

    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值