计算机系统应用教程网站

网站首页 > 技术文章 正文

「opencv」神经网络识别字母+数字

btikc 2024-09-08 12:08:30 技术文章 16 ℃ 0 评论

附件从原文下载即可。


  1. //opencv2.4.9 + vs2012 + 64位
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <opencv2/opencv.hpp>
  6. using namespace cv;
  7. using namespace std;
  8. char* WcharToChar(const wchar_t* wp)
  9. {
  10. char *m_char;
  11. int len= WideCharToMultiByte(CP_ACP,0,wp,wcslen(wp),NULL,0,NULL,NULL);
  12. m_char=new char[len+1];
  13. WideCharToMultiByte(CP_ACP,0,wp,wcslen(wp),m_char,len,NULL,NULL);
  14. m_char[len]='\0';
  15. return m_char;
  16. }
  17. wchar_t* CharToWchar(const char* c)
  18. {
  19. wchar_t *m_wchar;
  20. int len = MultiByteToWideChar(CP_ACP,0,c,strlen(c),NULL,0);
  21. m_wchar=new wchar_t[len+1];
  22. MultiByteToWideChar(CP_ACP,0,c,strlen(c),m_wchar,len);
  23. m_wchar[len]='\0';
  24. return m_wchar;
  25. }
  26. wchar_t* StringToWchar(const string& s)
  27. {
  28. const char* p=s.c_str();
  29. return CharToWchar(p);
  30. }
  31. int main()
  32. {
  33. const string fileform = "*.png";
  34. const string perfileReadPath = "charSamples";
  35. const int sample_mun_perclass = 20;//训练字符每类数量
  36. const int class_mun = 10+26;//训练字符类数 0-9 A-Z 除了I、O
  37. const int image_cols = 8;
  38. const int image_rows = 16;
  39. string fileReadName,
  40. fileReadPath;
  41. char temp[256];
  42. float trainingData[class_mun*sample_mun_perclass][image_rows*image_cols] = {{0}};//每一行一个训练样本
  43. float labels[class_mun*sample_mun_perclass][class_mun]={{0}};//训练样本标签
  44. for(int i = 0; i <= class_mun - 1; i++)//不同类
  45. {
  46. //读取每个类文件夹下所有图像
  47. int j = 0;//每一类读取图像个数计数
  48. if (i <= 9)//0-9
  49. {
  50. sprintf(temp, "%d", i);
  51. //printf("%d\n", i);
  52. }
  53. else//A-Z
  54. {
  55. sprintf(temp, "%c", i + 55);
  56. //printf("%c\n", i+55);
  57. }
  58. fileReadPath = perfileReadPath + "/" + temp + "/" + fileform;
  59. cout<<"文件夹"<<temp<<endl;
  60. HANDLE hFile;
  61. LPCTSTR lpFileName = StringToWchar(fileReadPath);//指定搜索目录和文件类型,如搜索d盘的音频文件可以是"D:\\*.mp3"
  62. WIN32_FIND_DATA pNextInfo; //搜索得到的文件信息将储存在pNextInfo中;
  63. hFile = FindFirstFile(lpFileName,&pNextInfo);//请注意是 &pNextInfo , 不是 pNextInfo;
  64. if(hFile == INVALID_HANDLE_VALUE)
  65. {
  66. continue;//搜索失败
  67. }
  68. //do-while循环读取
  69. do
  70. {
  71. if(pNextInfo.cFileName[0] == '.')//过滤.和..
  72. continue;
  73. j++;//读取一张图
  74. //wcout<<pNextInfo.cFileName<<endl;
  75. //printf("%s\n",WcharToChar(pNextInfo.cFileName));
  76. //对读入的图片进行处理
  77. Mat srcImage = imread( perfileReadPath + "/" + temp + "/" + WcharToChar(pNextInfo.cFileName),CV_LOAD_IMAGE_GRAYSCALE);
  78. Mat resizeImage;
  79. Mat trainImage;
  80. Mat result;
  81. resize(srcImage,resizeImage,Size(image_cols,image_rows),(0,0),(0,0),CV_INTER_AREA);//使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现
  82. threshold(resizeImage,trainImage,0,255,CV_THRESH_BINARY|CV_THRESH_OTSU);
  83. for(int k = 0; k<image_rows*image_cols; ++k)
  84. {
  85. trainingData[i*sample_mun_perclass+(j-1)][k] = (float)trainImage.data[k];
  86. //trainingData[i*sample_mun_perclass+(j-1)][k] = (float)trainImage.at<unsigned char>((int)k/8,(int)k%8);//(float)train_image.data[k];
  87. //cout<<trainingData[i*sample_mun_perclass+(j-1)][k] <<" "<< (float)trainImage.at<unsigned char>(k/8,k%8)<<endl;
  88. }
  89. }while (FindNextFile(hFile,&pNextInfo) && j<sample_mun_perclass);//如果设置读入的图片数量,则以设置的为准,如果图片不够,则读取文件夹下所有图片
  90. }
  91. // Set up training data Mat
  92. Mat trainingDataMat(class_mun*sample_mun_perclass, image_rows*image_cols, CV_32FC1, trainingData);
  93. cout<<"trainingDataMat——OK!"<<endl;
  94. // Set up label data
  95. for(int i = 0;i <= class_mun-1; ++i)
  96. {
  97. for(int j = 0;j <= sample_mun_perclass - 1; ++j)
  98. {
  99. for(int k = 0;k < class_mun; ++k)
  100. {
  101. if(k == i)
  102. if (k == 18)
  103. {
  104. labels[i*sample_mun_perclass + j][1] = 1;
  105. }
  106. else if(k == 24)
  107. {
  108. labels[i*sample_mun_perclass + j][0] = 1;
  109. }
  110. else
  111. {
  112. labels[i*sample_mun_perclass + j][k] = 1;
  113. }
  114. else
  115. labels[i*sample_mun_perclass + j][k] = 0;
  116. }
  117. }
  118. }
  119. Mat labelsMat(class_mun*sample_mun_perclass, class_mun, CV_32FC1,labels);
  120. cout<<"labelsMat:"<<endl;
  121. ofstream outfile("out.txt");
  122. outfile<<labelsMat;
  123. //cout<<labelsMat<<endl;
  124. cout<<"labelsMat——OK!"<<endl;
  125. //训练代码
  126. cout<<"training start...."<<endl;
  127. CvANN_MLP bp;
  128. // Set up BPNetwork's parameters
  129. CvANN_MLP_TrainParams params;
  130. params.train_method=CvANN_MLP_TrainParams::BACKPROP;
  131. params.bp_dw_scale=0.001;
  132. params.bp_moment_scale=0.1;
  133. params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,10000,0.0001); //设置结束条件
  134. //params.train_method=CvANN_MLP_TrainParams::RPROP;
  135. //params.rp_dw0 = 0.1;
  136. //params.rp_dw_plus = 1.2;
  137. //params.rp_dw_minus = 0.5;
  138. //params.rp_dw_min = FLT_EPSILON;
  139. //params.rp_dw_max = 50.;
  140. //Setup the BPNetwork
  141. Mat layerSizes=(Mat_<int>(1,5) << image_rows*image_cols,128,128,128,class_mun);
  142. bp.create(layerSizes,CvANN_MLP::SIGMOID_SYM,1.0,1.0);//CvANN_MLP::SIGMOID_SYM
  143. //CvANN_MLP::GAUSSIAN
  144. //CvANN_MLP::IDENTITY
  145. cout<<"training...."<<endl;
  146. bp.train(trainingDataMat, labelsMat, Mat(),Mat(), params);
  147. bp.save("../bpcharModel.xml"); //save classifier
  148. cout<<"training finish...bpModel1.xml saved "<<endl;
  149. //测试神经网络
  150. cout<<"测试:"<<endl;
  151. Mat test_image = imread("test4.png",CV_LOAD_IMAGE_GRAYSCALE);
  152. Mat test_temp;
  153. resize(test_image,test_temp,Size(image_cols,image_rows),(0,0),(0,0),CV_INTER_AREA);//使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现
  154. threshold(test_temp,test_temp,0,255,CV_THRESH_BINARY|CV_THRESH_OTSU);
  155. Mat_<float>sampleMat(1,image_rows*image_cols);
  156. for(int i = 0; i<image_rows*image_cols; ++i)
  157. {
  158. sampleMat.at<float>(0,i) = (float)test_temp.at<uchar>(i/8,i%8);
  159. }
  160. Mat responseMat;
  161. bp.predict(sampleMat,responseMat);
  162. Point maxLoc;
  163. double maxVal = 0;
  164. minMaxLoc(responseMat,NULL,&maxVal,NULL,&maxLoc);
  165. if (maxLoc.x <= 9)//0-9
  166. {
  167. sprintf(temp, "%d", maxLoc.x);
  168. //printf("%d\n", i);
  169. }
  170. else//A-Z
  171. {
  172. sprintf(temp, "%c", maxLoc.x + 55);
  173. //printf("%c\n", i+55);
  174. }
  175. cout<<"识别结果:"<<temp<<"相似度:"<<maxVal*100<<"%"<<endl;
  176. imshow("test_image",test_image);
  177. waitKey(0);
  178. return 0;
  179. }

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表