资源简介
印刷体数字识别openc程序 已测试效果不错
代码片段和文件信息
#include “opencv2/ml/ml.hpp“
#include “opencv2/highgui/highgui.hpp“
#include “opencv2/imgproc/imgproc.hpp“
#include
#include
using namespace cv;
using namespace std;
const int train_samples = 1;
const int classes = 10;
const int sizex = 20;
const int sizey = 30;
const int ImageSize = sizex * sizey;
char pathToImages[] = “./images“;
void PreProcessImage(Mat *inImageMat *outImageint sizex int sizey);
void LearnFromImages(CvMat* trainData CvMat* trainClasses);
void RunSelfTest(KNearest& knn2);
void AnalyseImage(KNearest knearest);
/** @function main */
int main(int argc char** argv)
{
CvMat* trainData = cvCreateMat(classes * train_samplesImageSize CV_32FC1);
CvMat* trainClasses = cvCreateMat(classes * train_samples 1 CV_32FC1);
namedWindow(“single“ CV_WINDOW_AUTOSIZE);
namedWindow(“all“CV_WINDOW_AUTOSIZE);
LearnFromImages(trainData trainClasses);
KNearest knearest(trainData trainClasses);
RunSelfTest(knearest);
cout << “losgehts\n“;
AnalyseImage(knearest);
return 0;
}
void PreProcessImage(Mat *inImageMat *outImageint sizex int sizey)
{
Mat grayImageblurredImagethresholdImagecontourImageregionOfInterest;
vector > contours;
cvtColor(*inImagegrayImage COLOR_BGR2GRAY);
GaussianBlur(grayImage blurredImage Size(5 5) 2 2);
adaptiveThreshold(blurredImage thresholdImage 255 1 1 11 2);
thresholdImage.copyTo(contourImage);
findContours(contourImage contours RETR_LIST CHAIN_APPROX_SIMPLE);
int idx = 0;
size_t area = 0;
for (size_t i = 0; i < contours.size(); i++)
{
if (area < contours[i].size() )
{
idx = i;
area = contours[i].size();
}
}
Rect rec = boundingRect(contours[idx]);
regionOfInterest = thresholdImage(rec);
resize(regionOfInterest*outImage Size(sizex sizey));
}
void LearnFromImages(CvMat* trainData CvMat* trainClasses)
{
Mat img;
char file[255];
for (int i = 0; i < classes; i++)
{
sprintf(file “%s/%d.png“ pathToImages i);
img = imread(file 1);
if (!img.data)
{
cout << “File “ << file << “ not found\n“;
exit(1);
}
Mat outfile;
PreProcessImage(&img &outfile sizex sizey);
for (int n = 0; n < ImageSize; n++)
{
trainData->data.fl[i * ImageSize + n] = outfile.data[n];
}
trainClasses->data.fl[i] = i;
}
}
void RunSelfTest(KNearest& knn2)
{
Mat img;
CvMat* sample2 = cvCreateMat(1 ImageSize CV_32FC1);
// SelfTest
char file[255];
int z = 0;
while (z++ < 10)
{
int iSecret = rand() % 10;
//cout << iSecret;
sprintf(file “%s/%d.png“ pathToImages iSecret);
img = imread(file 1);
Mat stagedImage;
PreProcessImage(&img &stagedImage sizex sizey);
for (int n = 0; n < ImageSize; n++)
{
sample2->data.fl[n] = stagedImage.data[n];
}
float detectedClass = knn2.find_nearest(sample2 1);
if (iSecret != (int) ((detectedClass)))
{
cout << “Falsch. Ist “ << iSecret << “ aber geraten ist “
<< (int) ((detectedClass));
exit(1);
}
cout << “Richtig “
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1699 2015-03-14 09:09 digit_recognition\images\0.png
文件 1012 2015-03-14 09:09 digit_recognition\images\1.png
文件 1569 2015-03-14 09:09 digit_recognition\images\2.png
文件 1554 2015-03-14 09:10 digit_recognition\images\3.png
文件 1393 2015-03-14 09:10 digit_recognition\images\4.png
文件 1597 2015-03-14 09:10 digit_recognition\images\5.png
文件 1532 2015-03-14 09:10 digit_recognition\images\6.png
文件 1189 2015-03-14 09:10 digit_recognition\images\7.png
文件 147997 2015-03-14 09:02 digit_recognition\images\buchstaben.png
文件 1595 2015-03-14 09:10 digit_recognition\images\8.png
文件 1645 2015-03-14 09:10 digit_recognition\images\9.png
文件 4272 2015-03-14 09:19 digit_recognition\digit_recog.cpp
文件 1446 2015-03-14 09:07 digit_recognition\Makefile~
文件 165 2015-03-14 09:24 digit_recognition\Makefile
目录 0 2015-03-14 09:12 digit_recognition\images\
目录 0 2015-03-14 09:24 digit_recognition\
- 上一篇:V5游戏程序多开器适合游戏多开.rar
- 下一篇:opengl的glut最全包
评论
共有 条评论