首先拆分图片得到数据

#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;
}

最新文章

  1. 如果你的SharePoint出现了,状态服务问题,InfoPath无法正常使用
  2. QT error: cannot find -lGL
  3. java 选择排序
  4. Android-开发工具
  5. 29、shiro框架入门
  6. XidianOJ 1120 Gold of Orz Pandas
  7. cmd实用命令
  8. trunc函数
  9. Android 异常处理最佳实践
  10. 公钥password学中的素数以及对称加密
  11. 竞争激烈的洗衣O2O
  12. C#基础 运算符
  13. fillder---断言,更改提交数据
  14. Python模块定义和使用
  15. Python:每日一题003
  16. 在Mac上配置全局的Git忽略文件
  17. CSS图片下面产生间隙的6种解决方案
  18. CCConfiguration::sharedConfiguration()-&gt;loadConfigFile cocos2d-x 中文乱码问题及国际化解决方案
  19. RedHat 6.4 安装WAS 7.0 启动失败com.ibm.websphere.ssl.SSLException
  20. kafka学习之-java api测试

热门文章

  1. delphi windows操作
  2. [转]C++的Json解析库:jsoncpp和boost
  3. PAT_A1090#Highest Price in Supply Chain
  4. 安装mongo
  5. 第一章:Lambda表达式入门概念
  6. sql 递归显示所有父节点
  7. Codeforces Round #525 D - Ehab and another another xor problem /// 构造
  8. docker删除常见命令
  9. 一些CSS3新技术
  10. mac NTFS 关于错误-36,rm Input/output error