识别简单的数字 字母的手写识别框架

本文分享了一个人工智能教程,专注于手写数字和字母的识别框架,包括代码实现和识别过程。通过学习,读者可以了解如何进行手写字符的识别。
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
  1. // patternDlg.cpp : implementation file    
  2. //    
  3.    
  4. #include "stdafx.h"    
  5. #include "pattern.h"    
  6. #include "patternDlg.h"    
  7. #include "Afxwin.h"    
  8. #include "Afxdlgs.h"    
  9. #include "winuser.h"    
  10. #include "Store.h"    
  11.  #include <STDIO.H>    
  12. //#define   _MBCS    
  13.    
  14. #ifdef _DEBUG    
  15. #define new DEBUG_NEW    
  16. #undef THIS_FILE    
  17. static char THIS_FILE[] = __FILE__;   
  18. #endif    
  19.    
  20. #include<WINDOWS.H>    
  21. #include<MATH.H>    
  22.    
  23. #define Startx 30    
  24. #define Starty 190    
  25. #define wid  192    
  26. #define lon 192    
  27. #define yu 3 //2 area    
  28. #define yu1 5 //dou dong    
  29.    
  30. #define yu2  4     
  31. int num,Time;   
  32.    
  33. int xmax,ymax,xmin,ymin; //随时纪录书写过程中的书写范围    
  34.    
  35. //存储书写笔迹的坐标序列,按笔划存储,每个笔划最多300个点    
  36. //每个字最多15个笔划    
  37. struct    
  38. {   
  39.     int x;   
  40.     int y;   
  41. }store[15][300];   
  42.    
  43. //备份的序列    
  44. struct    
  45. {   
  46.     int x;   
  47.     int y;   
  48. }storeback[15][300];   
  49.    
  50. struct   
  51. {      
  52.     int total;//总的笔划数    
  53.     char zifu[2];//代表字符    
  54.     int dian[15];//每个笔划的点数    
  55.     struct{   
  56.         int relate;//相对前点特征    
  57.         int region;//全局位置特征    
  58.         int arc;//过渡带特征    
  59.     }detail[15][20];//15个笔划上的特征点,每个笔划20个特征点    
  60. }tez[2002];   
  61.    
  62. struct   
  63. {   
  64.     int total;   
  65.     int dian[15];   
  66.     struct   
  67.     {   
  68.         int relate;   
  69.         int region;   
  70.         int arc;   
  71.     }detail[15][20];   
  72. }test;//意义同上,为测试点的结构    
  73.    
  74. int queue[50];   
  75. int  queue1[40];   
  76. int zong;   
  77.    
  78.   
  79. // CAboutDlg dialog used for App About    
  80.    
  81. class CAboutDlg : public CDialog   
  82. {   
  83. public:   
  84.     CAboutDlg();   
  85.    
  86. // Dialog Data    
  87.     //{{AFX_DATA(CAboutDlg)    
  88.     enum { IDD = IDD_ABOUTBOX };   
  89.     //}}AFX_DATA    
  90.    
  91.     // ClassWizard generated virtual function overrides    
  92.     //{{AFX_VIRTUAL(CAboutDlg)    
  93.     protected:   
  94.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support    
  95.     //}}AFX_VIRTUAL    
  96.    
  97. // Implementation    
  98. protected:   
  99.     //{{AFX_MSG(CAboutDlg)    
  100.     //}}AFX_MSG    
  101.     DECLARE_MESSAGE_MAP()   
  102. };   
  103.    
  104. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)   
  105. {   
  106.     //{{AFX_DATA_INIT(CAboutDlg)    
  107.     //}}AFX_DATA_INIT    
  108. }   
  109.    
  110. void CAboutDlg::DoDataExchange(CDataExchange* pDX)   
  111. {   
  112.     CDialog::DoDataExchange(pDX);   
  113.     //{{AFX_DATA_MAP(CAboutDlg)    
  114.     //}}AFX_DATA_MAP    
  115. }   
  116.    
  117. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)   
  118.     //{{AFX_MSG_MAP(CAboutDlg)    
  119.         // No message handlers    
  120.     //}}AFX_MSG_MAP    
  121. END_MESSAGE_MAP()   
  122.    
  123.   
  124. // CPatternDlg dialog    
  125.    
  126. CPatternDlg::CPatternDlg(CWnd* pParent /*=NULL*/)   
  127.     : CDialog(CPatternDlg::IDD, pParent)   
  128. {   
  129.     //{{AFX_DATA_INIT(CPatternDlg)    
  130.     //}}AFX_DATA_INIT    
  131.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32    
  132.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);   
  133. }   
  134.    
  135. void CPatternDlg::DoDataExchange(CDataExchange* pDX)   
  136. {   
  137.     CDialog::DoDataExchange(pDX);   
  138.     //{{AFX_DATA_MAP(CPatternDlg)    
  139.     DDX_Control(pDX, IDC_EDIT2, m_edit2);   
  140.     DDX_Control(pDX, IDC_EDIT1, m_edit1);   
  141.     DDX_Control(pDX, IDC_BUTTON4, m_escape);   
  142.     DDX_Control(pDX, IDC_BUTTON3, m_clear);   
  143.     DDX_Control(pDX, IDC_BUTTON1, m_recog);   
  144.     DDX_Control(pDX, IDC_BUTTON2, m_study);   
  145.     //}}AFX_DATA_MAP    
  146. }   
  147.    
  148. BEGIN_MESSAGE_MAP(CPatternDlg, CDialog)   
  149.     //{{AFX_MSG_MAP(CPatternDlg)    
  150.     ON_WM_SYSCOMMAND()   
  151.     ON_WM_PAINT()   
  152.     ON_WM_QUERYDRAGICON()   
  153.     ON_BN_CLICKED(IDC_BUTTON4, OnEsp)   
  154.     ON_BN_CLICKED(IDC_BUTTON1, OnRecog)   
  155.     ON_WM_MOUSEMOVE()   
  156.     ON_BN_CLICKED(IDC_BUTTON3, OnClear)   
  157.     ON_BN_CLICKED(IDC_BUTTON2, OnStudy)   
  158.     ON_WM_LBUTTONUP()   
  159.     ON_WM_LBUTTONDOWN()   
  160.     ON_COMMAND(ID_EXIT_MENU, OnExitMenu)   
  161.     ON_COMMAND(ID_ABOUT_MENU, OnAboutMenu)   
  162.     ON_COMMAND(ID_SAVE_MENU, OnSaveMenu1)   
  163.     ON_COMMAND(ID_OPEN_MENU, OnOpenMenu)   
  164.     //}}AFX_MSG_MAP    
  165. END_MESSAGE_MAP()   
  166.    
  167.   
  168. // CPatternDlg message handlers    
  169.    
  170. BOOL CPatternDlg::OnInitDialog()   
  171. {   
  172.     CFile cf;   
  173.        
  174.     CDialog::OnInitDialog();   
  175.    
  176.     mouseDown=0;   
  177.        
  178.     int i,j,k;   
  179.    
  180.     //将每个笔划上坐标初始化为空    
  181.     for(i=0;i<15;i++)   
  182.     {   
  183.         for(j=0;j<300;j++)   
  184.         {   
  185.             store[i][j].x=-1;   
  186.             store[i][j].y=-1;   
  187.         }   
  188.     }   
  189.    
  190.     for(i=0;i<15;i++)   
  191.     {   
  192.         for(j=0;j<300;j++)   
  193.         {   
  194.             storeback[i][j].x=-1;   
  195.             storeback[i][j].y=-1;   
  196.         }   
  197.     }   
  198.    
  199.     for(i=0;i<10;i++)   
  200.     {   
  201.         tez[i].total=-1;   
  202.    
  203.         for(j=0;j<15;j++)    
  204.             tez[i].dian[j]=-1;   
  205.    
  206.         for(j=0;j<15;j++)   
  207.         {   
  208.             for(k=0;k<20;k++)   
  209.             {   
  210.                 tez[i].detail[j][k].relate=0;   
  211.                 tez[i].detail[j][k].arc=0;   
  212.                 tez[i].detail[j][k].region=0;   
  213.             }   
  214.         }   
  215.     }   
  216.    
  217.     Time=0;   
  218.     num=0;   
  219.     zong=-1;//tez下标    
  220.     xmax=0;   
  221.     ymax=0;   
  222.     xmin=31;   
  223.     ymin=31;   
  224.    
  225.     for(i=0;i<50;i++)   
  226.         queue[i]=-1;   
  227.     for(i=0;i<40;i++)   
  228.         queue1[i]=-1;   
  229.    
  230.     test.total=-1;   
  231.     for(j=0;j<15;j++)    
  232.         test.dian[j]=-1;   
  233.    
  234.     for(j=0;j<15;j++)   
  235.     {   
  236.         for(k=0;k<20;k++)   
  237.         {   
  238.             test.detail[j][k].relate=0;   
  239.             test.detail[j][k].arc=0;   
  240.             test.detail[j][k].region=0;   
  241.         }   
  242.     }   
  243.    
  244.     // Add "About..." menu item to system menu.    
  245.     // IDM_ABOUTBOX must be in the system command range.    
  246.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);   
  247.     ASSERT(IDM_ABOUTBOX < 0xF000);   
  248.    
  249.     CMenu* pSysMenu = GetSystemMenu(FALSE);   
  250.     if (pSysMenu != NULL)   
  251.     {   
  252.         CString strAboutMenu;   
  253.         strAboutMenu.LoadString(IDS_ABOUTBOX);   
  254.         if (!strAboutMenu.IsEmpty())   
  255.         {   
  256.             pSysMenu->AppendMenu(MF_SEPARATOR);   
  257.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);   
  258.         }   
  259.     }   
  260.    
  261.     // Set the icon for this dialog.  The framework does this automatically    
  262.     //  when the application's main window is not a dialog    
  263.     SetIcon(m_hIcon, TRUE);         // Set big icon    
  264.     SetIcon(m_hIcon, FALSE);        // Set small icon    
  265.        
  266.     // TODO: Add extra initialization here    
  267.     if(cf.Open("mydata.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite)==NULL){   
  268.         AfxMessageBox("打开文件失败,/n您最好退出程序");}   
  269.    
  270.     return TRUE;  // return TRUE  unless you set the focus to a control    
  271. }   
  272.    
  273. void CPatternDlg::OnSysCommand(UINT nID, LPARAM lParam)   
  274. {   
  275.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)   
  276.     {   
  277.         CAboutDlg dlgAbout;   
  278.         dlgAbout.DoModal();   
  279.     }   
  280.     else   
  281.     {   
  282.         CDialog::OnSysCommand(nID, lParam);   
  283.     }   
  284. }   
  285.    
  286. // If you add a minimize button to your dialog, you will need the code below    
  287. //  to draw the icon.  For MFC applications using the document/view model,    
  288. //  this is automatically done for you by the framework.    
  289.    
  290. void CPatternDlg::OnPaint()    
  291. {   
  292.     CPaintDC dc(this);    
  293.     HBRUSH hB;   
  294.     hB=CreateSolidBrush(RGB(255,255,255));   
  295.     SelectObject(dc,hB);   
  296.     Rectangle(dc,Startx,Starty,Startx+wid-1,Starty+lon-1);   
  297.     DeleteObject(hB);   
  298.    
  299.     if (IsIconic())   
  300.     {   
  301.         // device context for painting    
  302.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);   
  303.    
  304.         // Center icon in client rectangle    
  305.         int cxIcon = GetSystemMetrics(SM_CXICON);   
  306.         int cyIcon = GetSystemMetrics(SM_CYICON);   
  307.         CRect rect;   
  308.         GetClientRect(&rect);   
  309.         int x = (rect.Width() - cxIcon + 1) / 2;   
  310.         int y = (rect.Height() - cyIcon + 1) / 2;   
  311.    
  312.         // Draw the icon    
  313.         dc.DrawIcon(x, y, m_hIcon);   
  314.     }   
  315.     else   
  316.     {   
  317.         CDialog::OnPaint();   
  318.     }   
  319. }   
  320.    
  321. // The system calls this to obtain the cursor to display while the user drags    
  322. //  the minimized window.    
  323. HCURSOR CPatternDlg::OnQueryDragIcon()   
  324. {   
  325.     return (HCURSOR) m_hIcon;   
  326. }   
  327.    
  328. void CPatternDlg::OnEsp()    
  329. {   
  330.     OnOK();    
  331. }   
  332.    
  333. //识别函数    
  334. void CPatternDlg::OnRecog()    
  335. {     
  336.     int j1,k1,m1,n1,x,y,xmod,ymod;//xmod,ymod;    
  337.     int  flagok2=0;   
  338.     char s[20];   
  339.     DealData();   
  340.    
  341.     //安次序检验,有点下标数    
  342.     for(j1=0;j1<=zong&&test.total>=0&&flagok2==0;j1++)   
  343.     {             
  344.         if(tez[j1].total==test.total)       //如果总条数相等    
  345.         {          
  346.             for(k1=0;k1<=test.total;k1++)              
  347.             {     
  348.                 if(test.dian[k1]!=tez[j1].dian[k1])     
  349.                     break;   
  350.             }   
  351.                      
  352.             if(k1>test.total)      
  353.             //每条得点数相同    
  354.             {      
  355.                 for(m1=0;m1<=test.total;m1++)           //每条    
  356.                 {        
  357.                     for(n1=0;n1<=test.dian[m1];n1++)        //每点    
  358.                     {   
  359.                         xmod=2*(tez[j1].detail[m1][n1].region % 8);  //8等分    
  360.                         ymod=2*((int)tez[j1].detail[m1][n1].region/8);   
  361.                         x=test.detail[m1][n1].region % 16;   
  362.                         y=(int)test.detail[m1][n1].region/16;    //   5/16                                                                
  363.                          
  364.                         if(( test.detail[m1][n1].relate!=tez[j1].detail[m1][n1].relate   
  365.                             &&(test.detail[m1][n1].relate-10*(int)(test.detail[m1][n1].relate/10))!=tez[j1].detail[m1][n1].relate   
  366.                             &&(int)(test.detail[m1][n1].relate/10)!=tez[j1].detail[m1][n1].relate)   
  367.                             ||((tez[j1].detail[m1][n1].arc!=test.detail[m1][n1].arc)&&(test.total<4)&&(test.dian[m1]<4))   
  368.                             ||((x<XMOD-3||X>xmod+3)||(y<YMOD-3||Y>ymod+3)))//shi为total+1    
  369.                             break;   
  370.                     }   
  371.                     if(n1<=test.dian[m1])     
  372.                         break;     
  373.                 }   
  374.                 if(m1>test.total)   
  375.                 {   
  376.                     flagok2=1;   
  377.                     sprintf(s,"输入正确,可识别,为:%s, 在字库%d个",tez[j1].zifu,j1);   
  378.                     AfxMessageBox(s);   
  379.                     m_edit1.ReplaceSel(tez[j1].zifu);    
  380.                     OnClear();   
  381.                 }   
  382.             }   
  383.         }   
  384.     }                                               
  385.           
  386.     if(flagok2==0)   
  387.     {    
  388.         AfxMessageBox("字库不足或输入不规范,无法识别,请学习");   
  389.         OnRecogch();   
  390.     }   
  391. }      
  392.     
  393. //学习函数      
  394. void CPatternDlg::OnStudy()    
  395. {        
  396.     int j1,k1,m1,n1,k,m,xmod,ymod,x,y;//j1代表字典中的一个字,k 表示笔划,m 表示一个点    
  397.     CString z1;   
  398.     int i,j;   
  399.     int flagok1=0; //o无法识别,1可识别,已经识别出结果    
  400.     char s[30];   
  401.     Inputdialog inputdlg;   
  402.    
  403.     //处理数据    
  404.     DealData();   
  405.    
  406.     if(test.total==-1)   
  407.     {   

    给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
    这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值