• 大小: 699KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-19
  • 语言: 其他
  • 标签: opencv数字  

资源简介

这是基于opencv2.4.10+VS2013写的数字字符识别 对于记事本里宋体常规的数字能够完美识别(没有干扰的情况下)

资源截图

代码片段和文件信息

#include
#include
#include

#include 
#include 
#include 
using namespace std;
using namespace cv;

const int NUM = 10000;
#define WINDOW_NAME “二值图“
int g_nThreshold = 200;
static void on_Threshold(int void*);
void getNumber();//分割字符
char getNumChar(Mat&);//识别单个字符图片

Mat g_srcImage gray image_threshold;
Mat open;

char result_num[NUM];//txt文件中的数字
char result_true[NUM];//识别后的数字

/*读取txt文件*/
void readTxt(string file)
{
ifstream infile;
infile.open(file.data());   //将文件流对象与文件连接起来 
assert(infile.is_open());   //若失败则输出错误消息并终止程序运行 
int i = 0;
char c;
while (!infile.eof())
{
infile >> c;
result_true[i] = c;
result_true[i + 1] = ‘\0‘;
i++;
}
infile.close();
}
int main()
{
readTxt(“123.txt“);
cout << “文件值为:“;
for (unsigned int i = 0; i < strlen(result_true)-1; i++)//读取txt输出数字
{
cout << result_true[i];
}
cout << endl;
g_srcImage = imread(“1.png“);
//放大原图,图片太小数字可能会连在一起
resize(g_srcImage g_srcImage Size(g_srcImage.cols * 2 g_srcImage.rows * 2) 0 0 INTER_LINEAR);
cvtColor(g_srcImage gray COLOR_BGR2GRAY);
namedWindow(WINDOW_NAME WINDOW_AUTOSIZE);
createTrackbar(“阈值“ WINDOW_NAME &g_nThreshold 255 on_Threshold);
on_Threshold(g_nThreshold 0);
waitKey(0);
return 0;
}

/*阈值操作*/
static void on_Threshold(int void*)
{
threshold(gray image_threshold g_nThreshold 255 1);
imshow(WINDOW_NAME image_threshold);
Mat element = getStructuringElement(MORPH_RECT Size(3 3));
morphologyEx(image_threshold open MORPH_OPEN element);//开运算,防止图片连在一起
getNumber();
}

/*单字符识别*/
char getNumChar(Mat &temp)
{
int min = 10000;
int min_i = 0;
int total = 0;
for (int n = 0; n < 10; n++)//n对应图片n.jpg
{
total = 0;
char a[100];
sprintf(a “number/%d.jpg“ n);//读取number模板文件夹图片,名字对应数字
string name = a;
Mat match = imread(name); 
cvtColor(match match COLOR_BGR2GRAY);
threshold(match match 254 255 1);
resize(temp temp match.size() 0 0 INTER_LINEAR);//转换图片大小和模板图片一样
for (int i = 0; i < match.rows; i++)//遍历所有像素与n.jpg对比
{
uchar *data_match = match.ptr(i);
uchar *data_temp = temp.ptr(i);
for (int j = 0; j < match.cols; j++)
{
if (*data_match == *data_temp)
{
total++;
}
*data_match++;
*data_temp++;
}
}
//读入的图片二值化后与原图二值化相同像素为0
//读入的图片二值化与程序运行过程中二值化的产生图片刚好相反
//所以相同的像素越少图片相似度越高
//在这里match二值化后与temp二值化图相反即黑白正好颠倒了
if (total < min)
{
min = total;
min_i = n;
}
}
char a = min_i+‘0‘;
return a;
}

/*字符分割*/
void getNumber()
{
Mat image_threshold_clone = image_threshold.clone();
vector>contours;
vectorhierarchy;
findContours(image_threshold_clone contours hierarchy
CV_RETR_EXTERNAL CV_CHAIN_APPROX_SIMPLE);//找出图片外轮廓,不要连内轮廓也找出来
int contour_size = contours.size();
int average_height = 0;
int count

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     126464  2017-10-24 21:13  数字识别4整理\Debug\数字识别2.exe

     文件     761836  2017-10-24 21:13  数字识别4整理\Debug\数字识别2.ilk

     文件    2223104  2017-10-24 21:13  数字识别4整理\Debug\数字识别2.pdb

     文件       1738  2017-10-24 21:08  数字识别4整理\数字识别2\1.png

     文件         36  2017-10-24 21:08  数字识别4整理\数字识别2\123.txt

     文件     422706  2017-10-24 21:07  数字识别4整理\数字识别2\Debug\main.obj

     文件     543744  2017-10-24 21:07  数字识别4整理\数字识别2\Debug\vc120.idb

     文件     782336  2017-10-24 21:07  数字识别4整理\数字识别2\Debug\vc120.pdb

     文件       1935  2017-10-24 21:13  数字识别4整理\数字识别2\Debug\数字识别2.log

     文件       3762  2017-10-24 21:07  数字识别4整理\数字识别2\Debug\数字识别2.tlog\cl.command.1.tlog

     文件      39928  2017-10-24 21:07  数字识别4整理\数字识别2\Debug\数字识别2.tlog\CL.read.1.tlog

     文件        556  2017-10-24 21:08  数字识别4整理\数字识别2\Debug\数字识别2.tlog\CL.write.1.tlog

     文件      16918  2017-10-24 21:13  数字识别4整理\数字识别2\Debug\数字识别2.tlog\link.command.1.tlog

     文件      15010  2017-10-24 21:13  数字识别4整理\数字识别2\Debug\数字识别2.tlog\link.read.1.tlog

     文件        898  2017-10-24 21:13  数字识别4整理\数字识别2\Debug\数字识别2.tlog\link.write.1.tlog

     文件        174  2017-10-24 21:13  数字识别4整理\数字识别2\Debug\数字识别2.tlog\数字识别2.lastbuildstate

     文件       5105  2017-10-14 22:12  数字识别4整理\数字识别2\main.cpp

     文件       1286  2017-10-14 17:12  数字识别4整理\数字识别2\number\0.jpg

     文件        861  2017-10-14 17:12  数字识别4整理\数字识别2\number\1.jpg

     文件       1333  2017-10-14 17:12  数字识别4整理\数字识别2\number\2.jpg

     文件       1363  2017-10-14 17:12  数字识别4整理\数字识别2\number\3.jpg

     文件       1199  2017-10-14 17:12  数字识别4整理\数字识别2\number\4.jpg

     文件       1220  2017-10-14 17:12  数字识别4整理\数字识别2\number\5.jpg

     文件       1378  2017-10-14 17:12  数字识别4整理\数字识别2\number\6.jpg

     文件       1029  2017-10-14 17:12  数字识别4整理\数字识别2\number\7.jpg

     文件       1455  2017-10-14 17:12  数字识别4整理\数字识别2\number\8.jpg

     文件       1386  2017-10-14 17:12  数字识别4整理\数字识别2\number\9.jpg

     文件       4090  2017-10-14 12:50  数字识别4整理\数字识别2\数字识别2.vcxproj

     文件        945  2017-10-14 12:50  数字识别4整理\数字识别2\数字识别2.vcxproj.filters

     文件        985  2017-10-14 12:50  数字识别4整理\数字识别2.sln

............此处省略10个文件信息

评论

共有 条评论

相关资源