首先拆分图片得到数据

#include "stdafx.h"
#include <iostream>
#include "opencv2/opencv.hpp" using namespace std;
using namespace cv; int mainss(int argc, const char** argv)
{
char ad[128] = { 0 };
int filename = 0, filenum = 0;
Mat img = imread("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的小图块 for (int i = 0; i < m; i++)
{
int offsetRow = i*b; //行上的偏移量
if (i % 5 == 0 && i != 0)
{
filename++;
filenum = 0;
}
for (int j = 0; j < n; j++)
{
int offsetCol = j*b; //列上的偏移量
sprintf_s(ad, "C:\\Users\\dongufang\\Documents\\Visual Studio 2015\\Projects\\opencvtest\\opencvtest\\data\\%d\\%d.jpg", filename, filenum++);
//截取20*20的小块
Mat tmp;
gray(Range(offsetRow, offsetRow + b), Range(offsetCol, offsetCol + b)).copyTo(tmp);
imwrite(ad, tmp);
}
}
return 0;
}

然后knn

#include "stdafx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/ml/ml.hpp>
using namespace std;
using namespace cv;
using namespace ml; char ad[128] = { 0 }; int main()
{
Mat traindata, trainlabel;
int k = 5, testnum = 0, truenum = 0;
//读取训练数据 4000张
for (int i = 0; i < 10; i++)
{
for (int j = 0; j<400; j++)
{
sprintf_s(ad, "C:\\Users\\dongufang\\Documents\\Visual Studio 2015\\Projects\\opencvtest\\opencvtest\\data\\%d\\%d.jpg", i, j);
Mat srcimage = imread(ad);
srcimage = srcimage.reshape(1, 1);
traindata.push_back(srcimage);
trainlabel.push_back(i);
}
}
traindata.convertTo(traindata, CV_32F);
Ptr<KNearest> knn = KNearest::create();
knn->setDefaultK(k);
knn->setIsClassifier(true); Ptr<TrainData> tdata = TrainData::create(traindata,ROW_SAMPLE, trainlabel);
knn->train(tdata); cv::Mat nearests(1, k, CV_32F);
//读取测试数据 1000张
for (int i = 0; i < 10; i++)
{
for (int j = 400; j<500; j++)
{
testnum++;
sprintf_s(ad, "C:\\Users\\dongufang\\Documents\\Visual Studio 2015\\Projects\\opencvtest\\opencvtest\\data\\%d\\%d.jpg", i, j);
Mat testdata = imread(ad);
testdata = testdata.reshape(1, 1);
testdata.convertTo(testdata, CV_32F);
Mat result;
int response = knn->predict(testdata, result);
if (response == i)
{
truenum++;
}
cout << "result:" << response << endl;
}
}
cout << "测试总数" << testnum << endl;
cout << "正确分类数" << truenum << endl;
cout << "准确率:" << (float)truenum / testnum * 100 << "%" << endl;
return 0;
}

opencv-识别手写数字的更多相关文章

  1. 学习OpenCV——SVM 手写数字检测

    转自http://blog.csdn.net/firefight/article/details/6452188 是MNIST手写数字图片库:http://code.google.com/p/supp ...

  2. 使用神经网络来识别手写数字【译】(三)- 用Python代码实现

    实现我们分类数字的网络 好,让我们使用随机梯度下降和 MNIST训练数据来写一个程序来学习怎样识别手写数字. 我们用Python (2.7) 来实现.只有 74 行代码!我们需要的第一个东西是 MNI ...

  3. 学习笔记TF024:TensorFlow实现Softmax Regression(回归)识别手写数字

    TensorFlow实现Softmax Regression(回归)识别手写数字.MNIST(Mixed National Institute of Standards and Technology ...

  4. TensorFlow实战之Softmax Regression识别手写数字

         关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2018年02月21日 23:10:04所撰写内容(http://blog.c ...

  5. 一文全解:利用谷歌深度学习框架Tensorflow识别手写数字图片(初学者篇)

    笔记整理者:王小草 笔记整理时间2017年2月24日 原文地址 http://blog.csdn.net/sinat_33761963/article/details/56837466?fps=1&a ...

  6. python手写神经网络实现识别手写数字

    写在开头:这个实验和matlab手写神经网络实现识别手写数字一样. 实验说明 一直想自己写一个神经网络来实现手写数字的识别,而不是套用别人的框架.恰巧前几天,有幸从同学那拿到5000张已经贴好标签的手 ...

  7. 3 TensorFlow入门之识别手写数字

    ------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...

  8. 用BP人工神经网络识别手写数字

    http://wenku.baidu.com/link?url=HQ-5tZCXBQ3uwPZQECHkMCtursKIpglboBHq416N-q2WZupkNNH3Gv4vtEHyPULezDb5 ...

  9. python机器学习使用PCA降维识别手写数字

    PCA降维识别手写数字 关注公众号"轻松学编程"了解更多. PCA 用于数据降维,减少运算时间,避免过拟合. PCA(n_components=150,whiten=True) n ...

  10. KNN 算法-实战篇-如何识别手写数字

    公号:码农充电站pro 主页:https://codeshellme.github.io 上篇文章介绍了KNN 算法的原理,今天来介绍如何使用KNN 算法识别手写数字? 1,手写数字数据集 手写数字数 ...

随机推荐

  1. NX二次开发-UFUN工程图表格注释检索默认单元格首选项UF_TABNOT_ask_default_cell_prefs

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

  2. NX二次开发-遍历当前part所有component,把装配子部件设置成工作部件

    NX11+VS2013 #include <uf.h> #include <uf_disp.h> #include <uf_modl.h> #include < ...

  3. 从[id setValue: forKey:]了解KVC

    <Objective-C基础教程> P224页有详细介绍 下边是apple官网的简单介绍 和一个应用的例子. KVC就是Key-value coding,大意是允许通过一个Key来读写一个 ...

  4. Oracle 生成sys_guid

    select sys_guid() from dual;select sys_guid() from dual connect by rownum<100

  5. 拾遗:Perl 在 Shell 脚本编程中的应用

    Perl 对我用途,仅是作为 Shell 脚本中的文本处理器:在较大的软件工程里,更多的是使用 C.go 等编译型语言. Perl 是一种历史比较悠久的动态编程语言,在各种类 Unix 系统中得到了应 ...

  6. vue多文件上传进度条 进度不更新问题

    转自 hhttp://www.cnblogs.com/muge10/p/6767493.html 感谢这位兄弟的文章,之前因为这个问题 ,我连续在sgmentflow上提问过多次,完全没人能回答.谢谢 ...

  7. 深夜Python - 第1夜 - for 迷 in 迷思

    深夜Python - 第1夜 - for 迷 in 迷思 在一个月黑风高的夜晚,我悄悄打开编辑器,进入程序的世界.刚刚学会Python的我,由于一段时间的过度装B,被委托优化一段程序,我信心十足地接下 ...

  8. Petrozavodsk Summer-2016. Warsaw U Contest, XVI Open Cup Onsite.

    Petrozavodsk Summer-2016. Warsaw U Contest, XVI Open Cup Onsite. Problem A. Gambling Problem B. Colo ...

  9. 【72. 编辑距离】【困难】【线性DP】

    给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1: 输 ...

  10. 【牛客网多校第一场】A

    题目链接:https://www.nowcoder.com/acm/contest/139/A 题意:大概就是给你0,1,2让你填矩阵问有多少种填法满足 a(i,j)<=a(i+1,j)以及a( ...