Click here to Skip to main content
15,891,316 members
Articles / Programming Languages / C#

Computer Vision: Virtual Buttons

Rate me:
Please Sign up or sign in to vote.
4.91/5 (50 votes)
2 Mar 2010CPOL3 min read 79.1K   6K   133  
In this article, we will put together code to control three virtual buttons in a webcam view.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace WindowsFormsApplication2
{
    static class Static_Variables
    {


        public static int button1_threshold = 15;
        public static int button2_threshold = 15;
        public static int button3_threshold = 15;

        // three rectangle will store each of the virtual button
        public static Rectangle[] rects;

        //detect motion or not
        public static bool IsDetecting = false;

        // should image show virtual buttons
        public static bool ShowButtons = true;

        //motion percentile
        public static int[] percentile_motion = new int[3];

        //motion percentile diffrence
        public static int[] percentile_motion_diffrence = new int[3];

        //make rectangles
        public static void Make_rectangles(Bitmap image)
        {
            int width = image.Width;
            int height = image.Height;

            // get rectangles for each virtual button
            Static_Variables.rects = new Rectangle[3];

            int rect_width = Math.Max(width / 5, height / 5);
            Size size = new Size(rect_width, rect_width);

            /* first virtual button at top and other two at bottom corners
             * you can further add more buttons here */
            int x = (width/2)-(rect_width/2);
            int y = 0;
            Point p = new Point(x, y);
            Static_Variables.rects[0] = new Rectangle(p, size);

            /*second virtual button*/
            x = 0;
            y = (height - 1) - rect_width;
            p = new Point(x, y);
            Static_Variables.rects[1] = new Rectangle(p, size);

            /*third virtual button*/
            x = (width-1)-rect_width;
            y = (height - 1) - rect_width;
            p = new Point(x, y);
            Static_Variables.rects[2] = new Rectangle(p, size);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
cat me.txt

Comments and Discussions