基於Aforge的手勢識別之一~~~簡單的手寫識別

来源:互联网 发布:淘宝一键抢拍 编辑:程序博客网 时间:2024/05/15 23:07

本文來自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. }