基于Aforge的手势识别之一~~~简单的手写识别

来源:互联网 发布:wifi破解软件电脑版 编辑:程序博客网 时间:2024/05/16 01:05

作者:咪当我系欧巴

转自:http://blog.csdn.net/hellogv/article/details/5554691


本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

     上一篇文章介绍了如何用Aforge去捕捉运动物体,现在就介绍一个更深入的操作----手势识别。
      我实现手势识别的原理很简单:捕捉运动物体+手写识别,把运动的物体的轨迹记录下来,然后通过手写识别引擎去搜索数据中最匹配的数据,从而知道“写”的是什么。目前常见的开源手写识别引擎有zinnia,wagomu 这些,不过小弟我比较业余,只把网上的比较常见的手写识别代码改进一下,只能识别字母和数字,真想通过摄像头隔空“手写”的朋友就要多花时间玩玩上面提到的几个开源手写类库了。

      本文介绍的手写识别:先在一个固定大小的画板上,用鼠标画下某图形,输入该图形对应的字母,程序把画板上的字母特征点都保存下来特征数据库(相当于学习记忆),然后再在画板上画出类似该字母的图形,程序就通过新画的特征点搜索特征数据库从而找出最类似的字母。

接下来贴出核心代码,详细的代码请到这里下载:http://download.csdn.net/source/2312865

GetBMPContext()是把画板中的图形的特征分析出来,Learn()是把特征与特定的字母/数字对应起来保存到数据库,Recognise()是把当前画板的图形特征从数据库中搜索,从而找出对应的字母/数字。

[c-sharp] view plaincopyprint?
  1. const int SCAN_GAP = 10;  
  2. private String GetBMPContext(Bitmap bmp)  
  3. {  
  4.     Boolean bool1stScan = true;  
  5.     int ax = 0, ay = 0, bx = 0, by = 0;  
  6.     String result = "";  
  7.     for (int i = 1; i < bmp.Width; i = i + SCAN_GAP)  
  8.     {  
  9.         for (int j = 1; j < bmp.Height; j = j + SCAN_GAP)  
  10.         {  
  11.             if (bmp.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())  
  12.             {  
  13.                 if (bool1stScan == false)  
  14.                 {  
  15.                     if (i <= ax) ax = i;  
  16.                     if (i >= bx) bx = i;  
  17.                     if (j <= ay) ay = j;  
  18.                     if (j >= by) by = j;  
  19.                 }  
  20.                 else  
  21.                 {  
  22.                     bool1stScan = false;  
  23.                     ax = i;  
  24.                     bx = i;  
  25.                     ay = j;  
  26.                     by = j;  
  27.                 }  
  28.             }  
  29.         }  
  30.     }  
  31.   
  32.     Bitmap bmp2 = new Bitmap(20, 20);  
  33.     Graphics g2 = Graphics.FromImage((Image)bmp2);  
  34.     g2.Clear(Color.White);  
  35.     g2.DrawImage(bmp, new Rectangle(0, 0, bmp2.Width, bmp2.Height),  
  36.         new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);  
  37.     g2.Dispose();  
  38.     //            pictureBox1.Image = bmp2;  
  39.   
  40.     int a = 0, b = 0;  
  41.     for (int i = 0; i < bmp2.Width; i++)  
  42.     {  
  43.         for (int j = 0; j < bmp2.Height; j++)  
  44.         {  
  45.             if (bmp2.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())  
  46.                 result = result + "0";  
  47.             else  
  48.                 result = result + "1";  
  49.         }  
  50.     }  
  51.     return result;  
  52. }  
  53.   
  54. public void Learn(String name)  
  55. {  
  56.     StreamWriter sw = new StreamWriter(fileName, true);  
  57.     sw.WriteLine(name + " " + GetBMPContext(bmpDraw));  
  58.     sw.Close();  
  59. }  
  60.   
  61. public String Recognise()  
  62. {  
  63.     String current = GetBMPContext(bmpDraw);  
  64.     StreamReader sr = new StreamReader(fileName);  
  65.     int max = 0;  
  66.     String result = "";  
  67.     while (sr.EndOfStream == false)  
  68.     {  
  69.         String[] key = sr.ReadLine().Split(' ');  
  70.         String name = key[0];  
  71.         String data = key[1];  
  72.         int match = 0;  
  73.   
  74.         for (int i = 0; i < current.Length; i++)  
  75.         {  
  76.             if (current[i] == data[i])  
  77.                 match++;  
  78.         }  
  79.         if (match >= max)  
  80.         {  
  81.             result = name;  
  82.             max = match;  
  83.         }  
  84.         //Trace.WriteLine(result + ":" + match + "," + max);  
  85.     }  
  86.     sr.Close();  
  87.   
  88.     return result;  
  89. }  
  


0 0
原创粉丝点击