Emgu CV 簡介php
衆所周知,Emgu CV是.NET平臺下對OpenCV圖像處理庫的封裝,也就是.NET版的OpenCV。開發者能夠很方便的經過C#,VB等語言調用OpenCV函數來實現相應的圖像處理功能。html
其下載地址爲:http://www.emgu.com/wiki/index.php/Main_Page。安裝過程極其簡單,網上教程不少,這裏也就不詳述了。算法
手勢識別app
在計算機科學中,手勢識別是經過數學算法來識別人類手勢的一個議題。手勢識別能夠來自人的身體各部位的運動,但通常是指臉部和手的運動。ide
騰訊有一個小工具,叫「QQ手勢達人forPPT」,能夠經過手勢運動來控制PPT的放映,本文章亦是經過Emgu CV實現相似效果。函數
PPT控制類工具
控制的兩個核心,一個是手勢的檢測,另外一個就是如何使用C#控制PPT的播放、中止、上一頁、下一頁等操做。下面這個類即實現經過C#來控制PPT的加載,播放控制等。優化
1: // ***********************************************************************
2: // Assembly : HandTrackerTestWindow
3: // Author : pengdian
4: // Created : 08-29-2014
5: //
6: // Last Modified By : pengdian
7: // Last Modified On : 08-29-2014
8: // ***********************************************************************
9: // <copyright file="PowerPointOperate.cs" company="nikoyo">
10: // Copyright (c) . All rights reserved.
11: // </copyright>
12: // <summary>PPT操做</summary>
13: // ***********************************************************************
14: using System;
15: using System.IO;
16:
17: /// <summary>
18: /// The HandTrackerTestWindow namespace.
19: /// </summary>
20: namespace HandTrackerTestWindow
21: {
22: /// <summary>
23: /// PPT操做類.
24: /// </summary>
25: public class PowerPointOperate
26: {
27: /// <summary>
28: /// The object application
29: /// </summary>
30: private static Microsoft.Office.Interop.PowerPoint.Application objApp = null;
31:
32: /// <summary>
33: /// The object pres
34: /// </summary>
35: private static Microsoft.Office.Interop.PowerPoint.Presentation objPres = null;
36:
37: /////// <summary>
38: /////// The object ss ws
39: /////// </summary>
40: ////private Microsoft.Office.Interop.PowerPoint.SlideShowWindows objSSWs;
41:
42: /////// <summary>
43: /////// The object SST
44: /////// </summary>
45: ////private Microsoft.Office.Interop.PowerPoint.SlideShowTransition objSST;
46:
47: /// <summary>
48: /// The object SSS
49: /// </summary>
50: private static Microsoft.Office.Interop.PowerPoint.SlideShowSettings objSSS;
51:
52: /////// <summary>
53: /////// The object SLD RNG
54: /////// </summary>
55: ////private Microsoft.Office.Interop.PowerPoint.SlideRange objSldRng;
56:
57: public bool IsStart
58: {
59: get
60: {
61: if (objPres == null || objPres.SlideShowWindow == null || objPres.SlideShowWindow.View == null)
62: {
63: return false;
64: }
65: return objPres.SlideShowWindow.View.State == Microsoft.Office.Interop.PowerPoint.PpSlideShowState.ppSlideShowRunning ? true : false;
66: }
67: }
68:
69: /// <summary>
70: /// Prevents a default instance of the <see cref="PowerPointOperate"/> class from being created.
71: /// </summary>
72: private PowerPointOperate()
73: {
74: }
75:
76: /// <summary>
77: /// 單例模式,獲取ppt操做類實例.
78: /// </summary>
79: /// <returns>PowerPointOperate.</returns>
80: public static PowerPointOperate GetInstance()
81: {
82: return new PowerPointOperate();
83: }
84:
85: /// <summary>
86: /// 加載PPT文件.
87: /// </summary>
88: /// <param name="pptFile">The PPT file.</param>
89: /// <exception cref="System.Exception">指定文件不存在</exception>
90: public void LoadFile(string pptFile)
91: {
92: if (string.IsNullOrWhiteSpace(pptFile) || !File.Exists(pptFile))
93: {
94: throw new Exception("指定文件不存在");
95: }
96:
97: ////防止連續打開多個PPT程序.
98: if (objApp == null)
99: {
100: objApp = new Microsoft.Office.Interop.PowerPoint.Application() { Visible = Microsoft.Office.Core.MsoTriState.msoTrue };
101: }
102:
103: try
104: {
105: ////以只讀方式打開,方便操做結束後保存.
106: objPres = objApp.Presentations.Open(pptFile, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);
107: //////Prevent Office Assistant from displaying alert messages:
108: ////this.bAssistantOn = objApp.Assistant.On;
109: ////objApp.Assistant.On = false;
110: }
111: catch
112: {
113: objApp.Quit();
114: GC.Collect();
115: }
116: finally
117: {
118: objSSS = null;
119: }
120: }
121:
122: /// <summary>
123: /// 開始播放PPT
124: /// </summary>
125: public void PPTStart()
126: {
127: try
128: {
129: if (objApp != null && objPres != null)
130: {
131: objSSS = objPres.SlideShowSettings;
132: objSSS.Run();
133: }
134: }
135: catch
136: {
137: objApp.Quit();
138: GC.Collect();
139: }
140: }
141:
142: /// <summary>
143: /// 中止播放PPT
144: /// </summary>
145: public void PPTStop()
146: {
147: try
148: {
149: if (objApp != null && IsStart)
150: {
151: objPres.SlideShowWindow.View.Exit();
152: }
153: }
154: catch
155: {
156: objApp.Quit();
157: GC.Collect();
158: }
159: finally
160: {
161: objSSS = null;
162: }
163: }
164:
165: /// <summary>
166: /// 關閉PPT文檔。
167: /// </summary>
168: public void PPTClose()
169: {
170: if (objApp != null)
171: {
172: objApp.Quit();
173: objApp = null;
174: }
175:
176: GC.Collect();
177: }
178:
179: /// <summary>
180: /// PPT下一頁。
181: /// </summary>
182: public void NextSlide()
183: {
184: if (objApp != null && IsStart)
185: {
186: objPres.SlideShowWindow.View.Next();
187: }
188: }
189:
190: /// <summary>
191: /// PPT上一頁。
192: /// </summary>
193: public void PreviousSlide()
194: {
195: if (objApp != null && IsStart)
196: {
197: objPres.SlideShowWindow.View.Previous();
198: }
199: }
200: }
201: }
手勢識別與運動檢測ui
這裏惟一的難點就是手勢模型的建立,所幸,這種麻煩東西咱們能夠從「QQ手勢達人forPPT」中提取出來,剩下的就是識別和檢測手勢了。代碼以下:this
1: using Emgu.CV;
2: using Emgu.CV.Structure;
3: using Emgu.CV.Util;
4: using System;
5: using System.Collections.Generic;
6: using System.Drawing;
7: using System.IO;
8: using System.Linq;
9: using System.Text;
10:
11: namespace HandTrackerTestWindow
12: {
13: public class HandTracker : IDisposable
14: {
15: CascadeClassifier palmHaar = new CascadeClassifier(@"data\palm.dat");
16: CascadeClassifier fistHaar = new CascadeClassifier(@"data\fist.dat");
17:
18: public void Dispose()
19: {
20: if (palmHaar != null)
21: {
22: palmHaar.Dispose();
23: palmHaar = null;
24: }
25: if (fistHaar != null)
26: {
27: fistHaar.Dispose();
28: fistHaar = null;
29: }
30: }
31:
32: public Rectangle DetectPalm(string imagePath)
33: {
34: if (!File.Exists(imagePath))
35: {
36: return new Rectangle(0, 0, 0, 0);
37: }
38: Image<Bgr, Byte> img = new Image<Bgr, byte>(imagePath);
39: Image<Gray, Byte> grayImage = img.Copy().Convert<Gray, byte>();
40: Rectangle[] palmDetected = palmHaar.DetectMultiScale(grayImage, 1.4, 10, new Size(20, 20), Size.Empty);
41: //faces.AddRange(facesDetected);
42: if (palmDetected == null || palmDetected.Length == 0)
43: {
44: return new Rectangle(0, 0, 0, 0);
45: }
46:
47: return palmDetected[0];
48: }
49: public Rectangle DetectPalm(Bitmap bitmap)
50: {
51: if (bitmap == null)
52: {
53: return new Rectangle(0, 0, 0, 0);
54: }
55: Image<Bgr, Byte> img = new Image<Bgr, byte>(bitmap);
56: Image<Gray, Byte> grayImage = img.Copy().Convert<Gray, byte>();
57: Rectangle[] palmDetected = palmHaar.DetectMultiScale(grayImage, 1.4, 10, new Size(20, 20), Size.Empty);
58: //faces.AddRange(facesDetected);
59: if (palmDetected == null || palmDetected.Length == 0)
60: {
61: return new Rectangle(0, 0, 0, 0);
62: }
63:
64: return palmDetected[0];
65: }
66: public Rectangle DetectFist(string imagePath)
67: {
68: if (!File.Exists(imagePath))
69: {
70: return new Rectangle(0, 0, 0, 0);
71: }
72: Image<Bgr, Byte> img = new Image<Bgr, byte>(imagePath);
73: Image<Gray, Byte> grayImage = img.Copy().Convert<Gray, byte>();
74: Rectangle[] fistDetected = fistHaar.DetectMultiScale(grayImage, 1.4, 10, new Size(20, 20), Size.Empty);
75: if (fistDetected == null || fistDetected.Length == 0)
76: {
77: return new Rectangle(0, 0, 0, 0);
78: }
79:
80: return fistDetected[0];
81: }
82:
83: public Rectangle DetectFist(Bitmap bitmap)
84: {
85: if (bitmap == null)
86: {
87: return new Rectangle(0, 0, 0, 0);
88: }
89: Image<Bgr, Byte> img = new Image<Bgr, byte>(bitmap);
90: Image<Gray, Byte> grayImage = img.Copy().Convert<Gray, byte>();
91: Rectangle[] fistDetected = fistHaar.DetectMultiScale(grayImage, 1.4, 10, new Size(20, 20), Size.Empty);
92: if (fistDetected == null || fistDetected.Length == 0)
93: {
94: return new Rectangle(0, 0, 0, 0);
95: }
96:
97: return fistDetected[0];
98: }
99: }
100: }
解決了手勢檢測控制類和PPT放映控制類,剩下的就是建立一個界面來進行控制了。
1: private void test()
2: {
3: bool isPalm = false;
4: bool isFist = false;
5: int count = 0;
6: while (true)
7: {
8: if (count > 10)
9: {
10: isPalm = false;
11: isFist = false;
12: count = 0;
13: this.Invoke(showControllerDelegate, false);
14: }
15: if (!isPalm)
16: {
17: isFist = false;
18: Bitmap bitmap = videoPlayer.GetCurrentVideoFrame();
19: if (handTracker.DetectPalm(bitmap).Width > 0)
20: {
21: isPalm = true;
22: count = 0;
23: this.Invoke(showControllerDelegate, true);
24: }
25: Thread.Sleep(50);
26: }
27: else
28: {
29: if (!isFist)
30: {
31: Bitmap bitmap = videoPlayer.GetCurrentVideoFrame();
32: Rectangle rect = handTracker.DetectFist(bitmap);
33: if (rect.Width > 0)
34: {
35: firstRect = rect;
36: isFist = true;
37: count = 0;
38: this.Invoke(showButtonDelegate, true);
39: }
40: else
41: {
42: isFist = false;
43: if (handTracker.DetectPalm(bitmap).Width > 0)
44: {
45: isPalm = true;
46: count = 0;
47: this.Invoke(showButtonDelegate, false);
48: }
49: else
50: {
51: count++;
52: }
53:
54: }
55: Thread.Sleep(50);
56: }
57: else
58: {
59: Bitmap bitmap = videoPlayer.GetCurrentVideoFrame();
60: Rectangle rect = handTracker.DetectFist(bitmap);
61: if (rect.Width > 0)
62: {
63: if (firstRect.Left - rect.Left > 30)
64: {
65: this.Invoke(nextPPTDelegate);
66: PowerPointOperate.GetInstance().NextSlide();
67: isPalm = false;
68: isFist = false;
69: count = 0;
70: Thread.Sleep(500);
71: this.Invoke(showControllerDelegate, false);
72: }
73: else if (firstRect.Left - rect.Left < -30)
74: {
75: this.Invoke(prePPTDelegate);
76: PowerPointOperate.GetInstance().PreviousSlide();
77: isPalm = false;
78: isFist = false;
79: count = 0;
80: Thread.Sleep(500);
81: this.Invoke(showControllerDelegate, false);
82: }
83: else
84: {
85: isPalm = true;
86: isFist = true;
87: count = 0;
88: }
89: //this.Invoke(showButtonDelegate);
90: }
91: else
92: {
93: if (handTracker.DetectPalm(bitmap).Width > 0)
94: {
95: isPalm = true;
96: isFist = false;
97: count = 0;
98: this.Invoke(showButtonDelegate, false);
99: }
100: else
101: {
102: count++;
103: }
104: }
105: Thread.Sleep(50);
106: }
107:
108: }
109:
110:
111: //PowerPointOperate.GetInstance().PPTStop();
112: }
113: }
最後實現的效果圖以下:
檢測手掌:
檢測拳頭:
移動拳頭控制PPT
因爲只是寫一個用於演示的Demo,所以代碼沒作優化,有興趣的能夠自行修改,目前只作了上一頁和下一頁,開始播放和中止播放,有興趣的能夠自行補上。
最後,附上源代碼地址(百度網盤):http://pan.baidu.com/s/1o6nyYFK
文章已遷移到:http://www.izonso.com/forum.php