opencv-识别手写数字
2024-08-25 22:14:48
首先拆分图片得到数据
#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;
}
最新文章
- 如果你的SharePoint出现了,状态服务问题,InfoPath无法正常使用
- QT error: cannot find -lGL
- java 选择排序
- Android-开发工具
- 29、shiro框架入门
- XidianOJ 1120 Gold of Orz Pandas
- cmd实用命令
- trunc函数
- Android 异常处理最佳实践
- 公钥password学中的素数以及对称加密
- 竞争激烈的洗衣O2O
- C#基础 运算符
- fillder---断言,更改提交数据
- Python模块定义和使用
- Python:每日一题003
- 在Mac上配置全局的Git忽略文件
- CSS图片下面产生间隙的6种解决方案
- CCConfiguration::sharedConfiguration()->;loadConfigFile cocos2d-x 中文乱码问题及国际化解决方案
- RedHat 6.4 安装WAS 7.0 启动失败com.ibm.websphere.ssl.SSLException
- kafka学习之-java api测试
热门文章
- delphi windows操作
- [转]C++的Json解析库:jsoncpp和boost
- PAT_A1090#Highest Price in Supply Chain
- 安装mongo
- 第一章:Lambda表达式入门概念
- sql 递归显示所有父节点
- Codeforces Round #525 D - Ehab and another another xor problem /// 构造
- docker删除常见命令
- 一些CSS3新技术
- mac NTFS 关于错误-36,rm Input/output error