MotionDetector1.cs ( File view )
From:PowerPoint Controler
Description:A s...
- By vaselly 2014-09-16
- View(s):40
- Download(s):0
- Point(s): 1
// Motion Detector // // Copyright Andrew Kirillov, 2005-2006 // andrew.kirillov@gmail.com // namespace motion { using System; using System.Drawing; using System.Drawing.Imaging; using System.Collections; using System.Runtime.InteropServices; using System.Reflection; using Tiger.Video.VFW; using System.Windows.Forms; using AForge.Imaging; using AForge.Imaging.Filters; ////// MotionDetector1 /// public class MotionDetector1 : IMotionDetector { private IFilter grayscaleFilter = new GrayscaleBT709(); private Difference differenceFilter = new Difference(); private Threshold thresholdFilter = new Threshold(15); private IFilter erosionFilter = new Erosion(); private Merge mergeFilter = new Merge(); private IFilter extrachChannel = new ExtractChannel(RGB.R); private ReplaceChannel replaceChannel = new ReplaceChannel(RGB.R, null); private Bitmap backgroundFrame; private BitmapData bitmapData; private FiltersSequence processingFilter = new FiltersSequence(); private MainForm _mForm=new MainForm(); private ArrayList points = new ArrayList(); private int blankFrameCount = 0; // Blank frame count private Bitmap leftArrow, rightArrow, upArrow, downArrow, d1aArrow, d1bArrow, d2aArrow, d2bArrow, question; private Bitmap lastGesture; private bool drawGesture = false; public string slideName; private bool calculateMotionLevel = false; private int width; // image width private int height; // image height private int pixelsChanged; // Get a handle to an application window. [DllImport("USER32.DLL")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // Activate an application window. [DllImport("USER32.DLL")] private static extern bool SetForegroundWindow(IntPtr hWnd); private class Beeper { [DllImport("Kernel32.dll")] public static extern bool Beep(UInt32 frequency, UInt32 duration); public static void RecognizedBeep() { Beep(2000, 50); System.Threading.Thread.Sleep(50); Beep(3000, 50); } public static void UnrecognizedBeep() { Beep(256, 200); } } // Motion level calculation - calculate or not motion level public bool MotionLevelCalculation { get { return calculateMotionLevel; } set { calculateMotionLevel = value; } } public void SlideName(string slidename) { slideName=slidename; } // Motion level - amount of changes in percents public double MotionLevel { get { return (double)pixelsChanged / (width * height); } } // Constructor public MotionDetector1() { } // Reset detector to initial state public void Reset() { if (backgroundFrame != null) { backgroundFrame.Dispose(); backgroundFrame = null; } } // Process new frame public void ProcessFrame(ref Bitmap image) { Utility.UnsafeBitmap uBitmap = new Utility.UnsafeBitmap(image); bool brightnessFound = false; float brightest = 0; int xPos = 0, yPos = 0; uBitmap.LockBitmap(); for (int y = 0; y < uBitmap.Bitmap.Height; y += 5) { for (int x = 0; x < uBitmap.Bitmap.Width; x += 5) { byte red, green, blue; red = uBitmap.GetPixel(x, y).red; green = uBitmap.GetPixel(x, y).green; blue = uBitmap.GetPixel(x, y).blue; float brightness = (299 * red + 587 * green + 114 * blue) / 1000; if (brightness > _mForm.threshold) { if (brightness > brightest) { brightest = brightness; xPos = x; yPos = y; brightnessFound = true; } } // (brightness > _mForm.threshold) } // x loop } // y loop if (brightnessFound == true) points.Add(new Point(xPos, yPos)); else blankFrameCount++; if (blankFrameCount < 5 && drawGesture == true) { Graphics dc = Graphics.FromImage((System.Drawing.Image)image); dc.DrawImage((System.Drawing.Image)lastGesture, 0, image.Height - 68); dc.Dispose(); } else if (blankFrameCount > 5 && drawGesture == true) { drawGesture = false; } if (blankFrameCount > 5) { if (points.Count >= 2) { Point[] pnts = (Point[])points.ToArray(typeof(Point)); string gesture = RecognizeMovement(pnts); drawGesture = true; if ((gesture != "?") && (_mForm.pptControl == true)) ControlPowerPoint(gesture); } points.Clear(); blankFrameCount = 0; } uBitmap.UnlockBitmap(); uBitmap.Dispose(); } // Code for controlling PowerPoint SlideShow private void ControlPowerPoint(string gesture) { IntPtr powerPointHandle = FindWindow(null, "PowerPoint Slide Show - ["+slideName+"]"); // Verify that PPS is a running process. if (powerPointHandle == IntPtr.Zero) { System.Windows.Forms.MessageBox.Show("Slide Show not running"); return; } switch (gesture) { case "LEFT": SetForegroundWindow(powerPointHandle); SendKeys.SendWait("p"); Beeper.RecognizedBeep(); break; case "RIGHT": SetForegroundWindow(powerPointHandle); SendKeys.SendWait("n"); Beeper.RecognizedBeep(); break; case "DIAGONAL2B": SetForegroundWindow(powerPointHandle); SendKeys.SendWait("n"); Beeper.RecognizedBeep(); break; case "DIAGONAL1A": SetForegroundWindow(powerPointHandle); SendKeys.SendWait("n"); Beeper.RecognizedBeep(); break; case "DIAGONAL1B": SetForegroundWindow(powerPointHandle); SendKeys.SendWait("P"); Beeper.RecognizedBeep(); break; case "DIAGONAL2A": SetForegroundWindow(powerPointHandle); SendKeys.SendWait("P"); Beeper.RecognizedBeep(); break; case "UP": SetForegroundWindow(powerPointHandle); SendKeys.SendWait("n"); Beeper.RecognizedBeep(); break; case "DOWN": SetForegroundWindow(powerPointHandle); SendKeys.SendWait("p"); Beeper.RecognizedBeep(); break; } } // Laser movement recognition code private string RecognizeMovement(Point[] pnts) { int x1, y1, x2, y2; int length = pnts.Length; x1 = pnts[0].X; y1 = pnts[0].Y; x2 = pnts[length - 1].X; y2 = pnts[length - 1].Y; int dx = Math.Abs(x2 - x1); int dy = Math.Abs(y2 - y1); bool diagonal = false; if (dx > dy) { if ((dy == 0) || ((dx / dy) >= 3)) { if ((x2 - x1) > 0) return ("RIGHT"); else return ("LEFT"); } else diagonal = true; } else if (dy > dx) { if ((dx == 0) || ((dy / dx) >= 3)) { if ((y2 - y1) > 0) return ("DOWN"); else return ("UP"); } else diagonal = true; } else diagonal = true; if (diagonal == true) { // Recognize diagonal type if ((x2 > x1) && (y2 > y1)) { return ("DIAGONAL1A"); } else if ((x2 < x1) && (y2 < y1)) { return ("DIAGONAL1B"); } else if ((x2 < x1) && (y2 > y1)) { return ("DIAGONAL2A"); } else if ((x2 > x1) && (y2 < y1)) { return ("DIAGONAL2B"); } } // If nothing else returned a value... return "?"; } } }
...
Expand> <Close
Sponsored links
File list
Tips: You can preview the content of files by clicking file names^_^Name | Size | Date |
---|---|---|
0 | 1.96 kB | |
0 | 1.96 kB | |
AssemblyInfo.cs | 2.37 kB | 2006-04-11 11:57 |
0 | 1.96 kB | |
0 | 1.96 kB | |
dshow.dll | 32.00 kB | 2007-06-13 21:51 |
dshow.pdb | 25.50 kB | 2007-06-13 21:51 |
0 | 1.96 kB | |
dshow.dll | 32.00 kB | 2007-06-07 20:26 |
0 | 1.96 kB | |
1.txt | 45.00 B | 2006-04-11 11:57 |
IBaseFilter.cs | 2.14 kB | 2006-04-11 11:57 |
IBasicVideo.cs | 4.70 kB | 2006-04-11 11:57 |
ICreateDevEnum.cs | 592.00 B | 2006-04-11 11:57 |
IEnumPins.cs | 831.00 B | 2006-04-11 11:57 |
IFileSourceFilter.cs | 861.00 B | 2006-04-11 11:57 |
IFilterGraph.cs | 1.58 kB | 2006-04-11 11:57 |
IGraphBuilder.cs | 2.84 kB | 2006-04-11 11:57 |
IMediaControl.cs | 1.49 kB | 2006-04-11 11:57 |
IMediaEvent.cs | 3.06 kB | 2006-04-11 11:57 |
IMediaPosition.cs | 1.88 kB | 2006-04-11 11:57 |
IMediaSeeking.cs | 2.79 kB | 2006-04-11 11:57 |
IPin.cs | 2.36 kB | 2006-04-11 11:57 |
IPropertyBag.cs | 853.00 B | 2006-04-11 11:57 |
ISampleGrabber.cs | 2.21 kB | 2006-04-11 11:57 |
IVideoFrameStep.cs | 747.00 B | 2006-04-11 11:57 |
IVideoWindow.cs | 5.90 kB | 2006-04-11 11:57 |
Structures.cs | 4.37 kB | 2006-04-11 11:57 |
Uuids.cs | 4.87 kB | 2006-04-11 11:57 |
Win32.cs | 919.00 B | 2006-04-11 11:57 |
dshow.csproj | 5.38 kB | 2007-03-16 20:38 |
Filter.cs | 2.57 kB | 2006-04-11 11:57 |
FilterCollection.cs | 2.14 kB | 2006-04-11 11:57 |
0 | 1.96 kB | |
0 | 1.96 kB | |
dshow.dll | 32.00 kB | 2007-06-13 21:51 |
dshow.pdb | 25.50 kB | 2007-06-13 21:51 |
0 | 1.96 kB | |
dshow.csproj.FileList.txt | 216.00 B | 2007-06-15 15:17 |
0 | 1.96 kB | |
dshow.dll | 32.00 kB | 2007-06-07 20:26 |
0 | 1.96 kB | |
dshow.dll | 28.00 kB | 2007-06-07 18:09 |
0 | 1.96 kB | |
Tools.cs | 1.11 kB | 2006-04-11 11:57 |
motion.sln | 1.79 kB | 2007-03-16 20:38 |
motion.suo | 35.00 kB | 2007-06-15 15:08 |
0 | 1.96 kB | |
AboutForm.cs | 11.91 kB | 2007-06-15 15:17 |
AboutForm.resx | 9.77 kB | 2007-06-15 15:17 |
App.ico | 26.07 kB | 2006-04-11 11:57 |
AssemblyInfo.cs | 2.47 kB | 2007-03-20 08:40 |
0 | 1.96 kB | |
0 | 1.96 kB | |
AForge.dll | 20.00 kB | 2007-03-16 19:54 |
AForge.Imaging.dll | 108.00 kB | 2007-03-19 18:42 |
AForge.Math.dll | 28.00 kB | 2007-03-16 19:54 |
App.ico | 26.07 kB | 2006-04-11 11:57 |
dshow.dll | 32.00 kB | 2007-06-13 21:51 |
dshow.pdb | 25.50 kB | 2007-06-13 21:51 |
motion.exe | 132.00 kB | 2007-06-15 15:17 |
motion.exe.config | 147.00 B | 2007-03-19 09:20 |
motion.pdb | 181.50 kB | 2007-06-15 15:17 |
motion.vshost.exe | 5.50 kB | 2005-09-23 07:56 |
motion.vshost.exe.config | 147.00 B | 2007-03-19 09:20 |
Tiger.Video.VFW.dll | 24.00 kB | 2007-06-13 21:51 |
Tiger.Video.VFW.pdb | 27.50 kB | 2007-06-13 21:51 |
0 | 1.96 kB | |
AForge.dll | 20.00 kB | 2007-03-16 19:54 |
AForge.Imaging.dll | 108.00 kB | 2007-03-19 18:42 |
AForge.Math.dll | 28.00 kB | 2007-03-16 19:54 |
dshow.dll | 32.00 kB | 2007-06-07 20:26 |
motion.exe | 124.00 kB | 2007-06-07 20:40 |
motion.exe.config | 147.00 B | 2007-03-19 09:20 |
motion.vshost.exe | 5.50 kB | 2005-09-23 07:56 |
motion.vshost.exe.config | 147.00 B | 2007-03-19 09:20 |
Tiger.Video.VFW.dll | 24.00 kB | 2007-06-07 20:26 |
Camera.cs | 3.47 kB | 2006-04-11 11:57 |
CameraWindow.cs | 5.17 kB | 2006-04-11 11:57 |
CameraWindow.resx | 5.55 kB | 2006-04-11 11:57 |
CaptureDeviceForm.cs | 4.49 kB | 2007-06-15 15:17 |
CaptureDeviceForm.resx | 5.37 kB | 2006-04-11 11:57 |
ClassDiagram1.cd | 1.00 B | 2007-06-07 18:39 |
Copyright.txt | 913.00 B | 2007-06-15 15:17 |
IMotionDetector.cs | 785.00 B | 2007-06-07 20:36 |
MainForm.cs | 21.34 kB | 2007-06-13 21:54 |
MainForm.resx | 45.81 kB | 2007-06-13 21:51 |
motion.csproj | 7.33 kB | 2007-06-07 18:45 |
MotionDetector1.cs | 9.95 kB | 2007-06-07 20:37 |
MotionDetector2.cs | 4.44 kB | 2007-06-07 20:38 |
MotionDetector3.cs | 5.06 kB | 2007-06-07 20:38 |
MotionDetector3Optimized.cs | 6.57 kB | 2007-06-07 20:38 |
MotionDetector4.cs | 4.89 kB | 2007-06-07 20:38 |
0 | 1.96 kB | |
0 | 1.96 kB | |
motion.AboutForm.resources | 3.00 kB | 2007-06-15 15:17 |
motion.CameraWindow.resources | 581.00 B | 2007-06-13 21:51 |
motion.CaptureDeviceForm.resources | 279.00 B | 2007-06-13 21:51 |
motion.csproj.GenerateResource.Cache | 1.00 kB | 2007-06-15 15:17 |
motion.exe | 132.00 kB | 2007-06-15 15:17 |
motion.MainForm.resources | 26.62 kB | 2007-06-13 21:51 |
motion.pdb | 181.50 kB | 2007-06-15 15:17 |
motion.Resources.1.gif | 828.00 B | 2006-04-11 11:57 |
motion.Resources.2.gif | 822.00 B | 2006-04-11 11:57 |
motion.Resources.3.gif | 830.00 B | 2006-04-11 11:57 |
motion.Resources.4.gif | 822.00 B | 2006-04-11 11:57 |
motion.Resources.5.gif | 822.00 B | 2006-04-11 11:57 |
motion.Resources.6.gif | 830.00 B | 2006-04-11 11:57 |
motion.Resources.7.gif | 821.00 B | 2006-04-11 11:57 |
motion.Resources.8.gif | 822.00 B | 2006-04-11 11:57 |
motion.Resources.9.gif | 822.00 B | 2006-04-11 11:57 |
motion.URLForm.resources | 269.00 B | 2007-06-13 21:51 |
ResolveAssemblyReference.cache | 13.42 kB | 2007-06-15 14:58 |
0 | 1.96 kB | |
motion.csproj.FileList.txt | 1.64 kB | 2007-06-15 15:17 |
0 | 1.96 kB | |
motion.AboutForm.resources | 3.00 kB | 2007-06-07 20:26 |
motion.CameraWindow.resources | 581.00 B | 2007-06-07 20:26 |
motion.CaptureDeviceForm.resources | 279.00 B | 2007-06-07 20:26 |
motion.csproj.GenerateResource.Cache | 1.00 kB | 2007-06-07 20:27 |
motion.exe | 124.00 kB | 2007-06-07 20:40 |
motion.MainForm.resources | 26.62 kB | 2007-06-07 20:27 |
motion.Resources.1.gif | 828.00 B | 2006-04-11 11:57 |
motion.Resources.2.gif | 822.00 B | 2006-04-11 11:57 |
motion.Resources.3.gif | 830.00 B | 2006-04-11 11:57 |
motion.Resources.4.gif | 822.00 B | 2006-04-11 11:57 |
motion.Resources.5.gif | 822.00 B | 2006-04-11 11:57 |
motion.Resources.6.gif | 830.00 B | 2006-04-11 11:57 |
motion.Resources.7.gif | 821.00 B | 2006-04-11 11:57 |
motion.Resources.8.gif | 822.00 B | 2006-04-11 11:57 |
motion.Resources.9.gif | 822.00 B | 2006-04-11 11:57 |
motion.URLForm.resources | 269.00 B | 2007-06-07 20:26 |
0 | 1.96 kB | |
ResolveAssemblyReference.cache | 10.44 kB | 2007-06-07 20:26 |
0 | 1.96 kB | |
0 | 1.96 kB | |
1.gif | 828.00 B | 2006-04-11 11:57 |
2.gif | 822.00 B | 2006-04-11 11:57 |
3.gif | 830.00 B | 2006-04-11 11:57 |
4.gif | 822.00 B | 2006-04-11 11:57 |
5.gif | 822.00 B | 2006-04-11 11:57 |
6.gif | 830.00 B | 2006-04-11 11:57 |
7.gif | 821.00 B | 2006-04-11 11:57 |
8.gif | 822.00 B | 2006-04-11 11:57 |
9.gif | 822.00 B | 2006-04-11 11:57 |
UnsafeBitmap.cs | 2.76 kB | 2007-06-15 14:58 |
URLForm.cs | 3.98 kB | 2006-04-11 11:57 |
URLForm.resx | 5.36 kB | 2006-04-11 11:57 |
0 | 1.96 kB | |
ByteArrayUtils.cs | 1.25 kB | 2006-04-11 11:57 |
CameraEvents.cs | 626.00 B | 2006-04-11 11:57 |
CaptureDevice.cs | 8.15 kB | 2006-04-11 11:57 |
IVideoSource.cs | 1.57 kB | 2006-04-11 11:57 |
JPEGStream.cs | 6.89 kB | 2006-04-11 11:57 |
MJPEGStream.cs | 9.12 kB | 2006-04-11 11:57 |
VideoFileSource.cs | 3.45 kB | 2006-04-11 11:57 |
VideoStream.cs | 8.47 kB | 2006-04-11 11:57 |
0 | 1.96 kB | |
AForge.dll | 20.00 kB | 2007-03-16 19:54 |
AForge.Imaging.dll | 108.00 kB | 2007-03-19 18:42 |
AForge.Math.dll | 28.00 kB | 2007-03-16 19:54 |
0 | 1.96 kB | |
AssemblyInfo.cs | 2.37 kB | 2006-04-11 11:57 |
AVIReader.cs | 5.29 kB | 2006-04-11 11:57 |
AVIWriter.cs | 5.59 kB | 2006-04-11 11:57 |
0 | 1.96 kB | |
0 | 1.96 kB | |
Tiger.Video.VFW.dll | 24.00 kB | 2007-06-13 21:51 |
Tiger.Video.VFW.pdb | 27.50 kB | 2007-06-13 21:51 |
0 | 1.96 kB | |
Tiger.Video.VFW.dll | 24.00 kB | 2007-06-07 20:26 |
0 | 1.96 kB | |
0 | 1.96 kB | |
0 | 1.96 kB | |
Tiger.Video.VFW.dll | 24.00 kB | 2007-06-13 21:51 |
Tiger.Video.VFW.pdb | 27.50 kB | 2007-06-13 21:51 |
0 | 1.96 kB | |
0 | 1.96 kB | |
Tiger.Video.VFW.dll | 20.00 kB | 2007-06-07 18:09 |
0 | 1.96 kB | |
Tiger.Video.VFW.dll | 24.00 kB | 2007-06-07 20:26 |
vfw.csproj.FileList.txt | 276.00 B | 2007-06-15 15:17 |
vfw.csproj | 3.55 kB | 2007-03-16 20:38 |
Win32.cs | 9.81 kB | 2006-04-11 11:57 |
Sponsored links