本文主要是介绍基於Aforge的手勢識別之一~~~簡單的手寫識別,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文來自http://blog.csdn.net/hellogv/ ,引用必須注明出處!
上一篇文章介紹了如何用Aforge去捕捉運動物體,現在就介紹一個更深入的操作----手勢識別。
我實現手勢識別的原理很簡單:捕捉運動物體+手寫識別,把運動的物體的軌跡記錄下來,然後通過手寫識別引擎去搜索數據中最匹配的數據,從而知道「寫」的是什麼。目前常見的開源手寫識別引擎有zinnia,wagomu 這些,不過小弟我比較業余,只把網上的比較常見的手寫識別代碼改進一下,只能識別字母和數字,真想通過攝像頭隔空「手寫」的朋友就要多花時間玩玩上面提到的幾個開源手寫類庫了。
本文介紹的手寫識別:先在一個固定大小的畫板上,用鼠標畫下某圖形,輸入該圖形對應的字母,程序把畫板上的字母特征點都保存下來特征數據庫(相當於學習記憶),然後再在畫板上畫出類似該字母的圖形,程序就通過新畫的特征點搜索特征數據庫從而找出最類似的字母。
接下來貼出核心代碼,詳細的代碼請到這裡下載:http://download.csdn.net/source/2312865
GetBMPContext()是把畫板中的圖形的特征分析出來,Learn()是把特征與特定的字母/數字對應起來保存到數據庫,Recognise()是把當前畫板的圖形特征從數據庫中搜索,從而找出對應的字母/數字。
- const int SCAN_GAP = 10;
- private String GetBMPContext(Bitmap bmp)
- {
- Boolean bool1stScan = true;
- int ax = 0, ay = 0, bx = 0, by = 0;
- String result = "";
- for (int i = 1; i < bmp.Width; i = i + SCAN_GAP)
- {
- for (int j = 1; j < bmp.Height; j = j + SCAN_GAP)
- {
- if (bmp.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
- {
- if (bool1stScan == false)
- {
- if (i <= ax) ax = i;
- if (i >= bx) bx = i;
- if (j <= ay) ay = j;
- if (j >= by) by = j;
- }
- else
- {
- bool1stScan = false;
- ax = i;
- bx = i;
- ay = j;
- by = j;
- }
- }
- }
- }
- Bitmap bmp2 = new Bitmap(20, 20);
- Graphics g2 = Graphics.FromImage((Image)bmp2);
- g2.Clear(Color.White);
- g2.DrawImage(bmp, new Rectangle(0, 0, bmp2.Width, bmp2.Height),
- new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
- g2.Dispose();
- // pictureBox1.Image = bmp2;
- int a = 0, b = 0;
- for (int i = 0; i < bmp2.Width; i++)
- {
- for (int j = 0; j < bmp2.Height; j++)
- {
- if (bmp2.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
- result = result + "0";
- else
- result = result + "1";
- }
- }
- return result;
- }
- public void Learn(String name)
- {
- StreamWriter sw = new StreamWriter(fileName, true);
- sw.WriteLine(name + " " + GetBMPContext(bmpDraw));
- sw.Close();
- }
- public String Recognise()
- {
- String current = GetBMPContext(bmpDraw);
- StreamReader sr = new StreamReader(fileName);
- int max = 0;
- String result = "";
- while (sr.EndOfStream == false)
- {
- String[] key = sr.ReadLine().Split(' ');
- String name = key[0];
- String data = key[1];
- int match = 0;
- for (int i = 0; i < current.Length; i++)
- {
- if (current[i] == data[i])
- match++;
- }
- if (match >= max)
- {
- result = name;
- max = match;
- }
- //Trace.WriteLine(result + ":" + match + "," + max);
- }
- sr.Close();
- return result;
- }
这篇关于基於Aforge的手勢識別之一~~~簡單的手寫識別的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!