資源簡介
這是基于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? {
cout?< }
cout?< 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? {
uchar?*data_match?=?match.ptr(i);
uchar?*data_temp?=?temp.ptr(i);
for?(int?j?=?0;?j? {
if?(*data_match?==?*data_temp)
{
total++;
}
*data_match++;
*data_temp++;
}
}
//讀入的圖片二值化后與原圖二值化相同像素為0
//讀入的圖片二值化與程序運行過程中二值化的產生圖片剛好相反
//所以相同的像素越少圖片相似度越高
//在這里match二值化后與temp二值化圖相反即黑白正好顛倒了
if?(total? {
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\li
?????文件??????15010??2017-10-24?21:13??數字識別4整理\數字識別2\Debug\數字識別2.tlog\li
?????文件????????898??2017-10-24?21:13??數字識別4整理\數字識別2\Debug\數字識別2.tlog\li
?????文件????????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個文件信息
- 上一篇:unity多人聯網
- 下一篇:w8h3fi.exe
評論
共有 條評論