5,276,406 members and growing! (16,695 online)
Email Password   helpLost your password?
Multimedia » DirectX » General     Intermediate

Laser Gesture Recognition

By Ashish Derhgawen

A quick article on setting up a simple, real-time laser gesture recognition application and using it to control Windows Media Player.
C# 1.0, C# 2.0, C#Windows, .NET, .NET 2.0, NT4, Win2K, WinXPVS2005, VS, Dev

Posted: 9 Jan 2007
Updated: 9 Jan 2007
Views: 85,775
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
Prize winner in Competition "C# Dec 2006"
70 votes for this Article.
Popularity: 8.87 Rating: 4.81 out of 5
2 votes, 2.9%
1
0 votes, 0.0%
2
1 vote, 1.4%
3
5 votes, 7.1%
4
62 votes, 88.6%
5

Sample Image - lasergesture.jpg

Introduction

Wouldn't it be great if we could somehow give visual commands to our computer without touching the keyboard or mouse? In this article, we will put together a simple laser gesture recognition application and use it to control Windows Media Player. This is far more comfortable than using a remote control because you don't have to look for the correct buttons in the dark. All you have to do is make a few simple gestures anywhere in the camera's field of view with a laser pointer, and that's it! This program recognizes simple gestures made on a wall with a laser pointer such as left, right, up, down, two downward and two upward diagonals. This program could be modified to recognize some more gestures; however, it cannot recognize complex gestures since I haven't taken a neural network approach for image recognition.

Video

Our first step is to get a video feed into our application from a webcam. We can use DirectX's component DirectShow for accomplishing this. I have directly used Andrew Kirillov's Motion Detection code[^] (with permission) for image acquisition. I modified the code in MotionDetector1.cs to perform laser gesture recognition.

Recognition

The program searches for the brightest pixel in its field of view (which is a laser dot, in our case) with luminance above a certain threshold value. Luminance of a pixel can be calculated using its RGB values, with a simple formula:

Luminance = (299 * red + 587 * green + 114 * blue) / 1000

After it finds the pixel, it analyzes how much that point moved along the x and y axes. Based on these parameters, the program tries to recognize the movement. For example, if the laser dot's movement along the x-axis is much more than its movement along the y-axis, the program will determine that it was more or less a horizontal movement. Then, based on the initial and final position of the laser dot, it will determine if the movement was towards the left or towards the right. It uses a similar technique to detect upward, downward, and diagonal movements.

Controlling Windows Media Player

For controlling the Windows Media Player, the program simply simulates some of the keyboard shortcuts used by Media Player. This code can skip to the next/previous track, or play, pause, stop a track based on the gesture the program recognizes:

// 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 void ControlMediaPlayer(string gesture)
{
    IntPtr mediaPlayerHandle = 
           FindWindow("WMPlayerApp", "Windows Media Player");

    // Verify that WMP is a running process.

    if (mediaPlayerHandle == IntPtr.Zero)
    {
        System.Windows.Forms.MessageBox.Show("WMP is not running.");
        return;
    }

    switch (gesture)
    {        
        case "LEFT":
            SetForegroundWindow(mediaPlayerHandle);
            SendKeys.SendWait("^b");
            break;

        case "RIGHT":
            SetForegroundWindow(mediaPlayerHandle);
            SendKeys.SendWait("^f");
            break;

        case "UP":
            SetForegroundWindow(mediaPlayerHandle);
            SendKeys.SendWait("^s");
            break;

        case "DOWN":
            SetForegroundWindow(mediaPlayerHandle);
            SendKeys.SendWait("^p");
            break;
    }
}

Using the program

Since the program searches for the brightest pixel in the camera's field of view, the lighting conditions of your room can affect its performance. So, adjust the brightness threshold and lighting conditions so that nothing (except the laser) exceeds the brightness threshold.

In the sample program I've provided with this article, the gestures for controlling Windows Media Player are:

  • Up – Stop
  • Down – Play/Pause
  • Left – Previous Track
  • Right – Next Track

You can also easily modify the code and use diagonal gestures for volume control. :)

Conclusion

We have reached the end of this article. I might update this program to perform more complex gesture recognition later on. However, for now, have fun with it! You can find some videos of this application being used to control Media Player on my blog[^]. You can also easily modify the code to make this program do much more than just controlling Windows Media Player. Have fun!

History

  • [13-JAN-2007] - Minor corrections
  • [09-JAN-2007] - Initial publication

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Ashish Derhgawen


View my blog: http://ashishrd.blogspot.com[^]
Location: India India

Other popular DirectX articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 73 (Total in Forum: 73) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralSuper Program!membermikkojay8:41 8 Mar '08  
GeneralRe: Super Program!memberAshish Derhgawen18:30 17 Mar '08  
GeneralHrmmemberdubbele onzin3:46 6 Nov '07  
QuestionWMP is not runningmemberAbu Syed Khan5:55 25 May '07  
AnswerRe: WMP is not runningmemberAshish Derhgawen19:15 10 Jun '07  
GeneralTranslate to VB?memberCorbet Peters17:20 20 May '07  
Generalreportmembershirish_agarwal_8518:42 14 May '07  
GeneralGreat WorkmemberGiorgi Dalakishvili2:45 3 May '07  
GeneralRe: Great WorkmemberAshish Derhgawen11:27 3 May '07  
Generaloutput XYmemberCarl-Johnson4567:34 31 Mar '07  
GeneralRe: output XYmemberAshish Derhgawen23:00 1 Apr '07  
Generalhimembersarithak5480:53 12 Mar '07  
GeneralGreat Job!memberpab5645467:38 10 Mar '07  
GeneralRe: Great Job!memberAshish Derhgawen20:16 11 Mar '07  
GeneralAwesomememberNiiiissssshhhhhuuuuu23:27 4 Mar '07  
GeneralAwesome!!!!memberNiiiissssshhhhhuuuuu23:26 4 Mar '07  
GeneralWell donememberSacha Barber0:53 1 Mar '07  
GeneralRe: Well donememberAshish Derhgawen3:42 1 Mar '07  
GeneralVery neat.memberCaptain See Sharp16:10 16 Feb '07  
GeneralRe: Very neat.memberAshish Derhgawen18:01 16 Feb '07  
GeneralMonthly CompetitionmemberSacha Barber0:16 13 Feb '07  
GeneralRe: Monthly CompetitionmemberAshish Derhgawen8:05 14 Feb '07  
GeneralRe: Monthly CompetitionmemberDr.Luiji9:55 16 Feb '07  
GeneralThanks for sharingmemberPolymorpher15:17 17 Jan '07  
GeneralRe: Thanks for sharingmemberAshish Derhgawen19:14 17 Jan '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Jan 2007
Editor: Smitha Vijayan
Copyright 2007 by Ashish Derhgawen
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project