Exemplo n.º 1
0
        private Mat preprocessShape(OpenCvSharp.CPlusPlus.Rect shapeRect, Mat sceneMat)
        {
            var matRect = new OpenCvSharp.CPlusPlus.Rect(0, 0, sceneMat.Width, sceneMat.Height);

            shapeRect.Inflate((int)(shapeRect.Width * 0.1), (int)(shapeRect.Height * 0.1));
            shapeRect = shapeRect.Intersect(matRect);
            Mat shapeMat = sceneMat.SubMat(shapeRect);
            var size     = new OpenCvSharp.CPlusPlus.Size(128, 128);

            shapeMat = shapeMat.Resize(size);
            return(shapeMat);
        }
Exemplo n.º 2
0
        private static void Run()
        {
            var dm = DescriptorMatcher.Create("BruteForce");

            dm.Clear();

            string[] algoNames = Algorithm.GetList();
            Console.WriteLine(String.Join("\n", algoNames));

            SIFT al1 = new SIFT();

            string[] ppp = al1.GetParams();
            Console.WriteLine(ppp);
            var    t = al1.ParamType("contrastThreshold");
            double d = al1.GetDouble("contrastThreshold");

            t.ToString();
            d.ToString();

            var src    = new Mat("data/lenna.png");
            var rand   = new Random();
            var memory = new List <long>(100);

            var a1 = new Mat(src, Rect.FromLTRB(0, 0, 30, 40));
            var a2 = new Mat(src, Rect.FromLTRB(0, 0, 30, 40));
            var a3 = new Mat(src, Rect.FromLTRB(0, 0, 30, 40));

            a3.ToString();

            for (long i = 0; ; i++)
            {
                for (int j = 0; j < 200; j++)
                {
                    int c1   = rand.Next(100, 400);
                    int c2   = rand.Next(100, 400);
                    Mat temp = src.Row[c1];
                    src.Row[c1] = src.Row[c2];
                    src.Row[c2] = temp;
                }

                memory.Add(MyProcess.WorkingSet64);
                if (memory.Count >= 100)
                {
                    double average = memory.Average();
                    Console.WriteLine("{0:F3}MB", average / 1024.0 / 1024.0);
                    memory.Clear();
                    GC.Collect();
                }
            }
        }
Exemplo n.º 3
0
        static OpenCvSharp.CPlusPlus.Point TrackDetection(List <Mat> mats, int px, int py, int shiftx = 2, int shifty = 2, int shiftpitch = 4, int windowsize = 40, int phthresh = 5, bool debugflag = false)
        {
            int x0 = px - 256;
            int y0 = py - 220;

            List <rawmicrotrack> rms = new List <rawmicrotrack>();

            // Point2d pixel_cen = TrackDetection(binimages, 256, 220, 3, 3, 4, 90, 3);


            int counter = 0;

            for (int ax = -shiftx; ax <= shiftx; ax++)
            {
                for (int ay = -shifty; ay <= shifty; ay++)
                {
                    using (Mat big = Mat.Zeros(600, 600, MatType.CV_8UC1))
                        using (Mat imgMask = Mat.Zeros(big.Height, big.Width, MatType.CV_8UC1))
                        {
                            //make the size of mask
                            int ystart = big.Height / 2 + y0 - windowsize / 2;
                            int yend   = big.Height / 2 + y0 + windowsize / 2;
                            int xstart = big.Width / 2 + x0 - windowsize / 2;
                            int xend   = big.Width / 2 + x0 + windowsize / 2;

                            //make mask as shape of rectangle. by use of opencv
                            OpenCvSharp.CPlusPlus.Rect recMask = new OpenCvSharp.CPlusPlus.Rect(xstart, ystart, windowsize, windowsize);
                            Cv2.Rectangle(imgMask, recMask, 255, -1);//brightness=1, fill

                            for (int p = 0; p < mats.Count; p++)
                            {
                                int startx = big.Width / 2 - mats[p].Width / 2 + (int)(p * ax * shiftpitch / 8.0);
                                int starty = big.Height / 2 - mats[p].Height / 2 + (int)(p * ay * shiftpitch / 8.0);
                                Cv2.Add(
                                    big[starty, starty + mats[p].Height, startx, startx + mats[p].Width],
                                    mats[p],
                                    big[starty, starty + mats[p].Height, startx, startx + mats[p].Width]);
                            }

                            using (Mat big_c = big.Clone())
                            {
                                Cv2.Threshold(big, big, phthresh, 255, ThresholdType.ToZero);
                                Cv2.BitwiseAnd(big, imgMask, big);

                                //Mat roi = big[ystart, yend , xstart, xend];//メモリ領域がシーケンシャルにならないから輪郭抽出のときに例外が出る。

                                if (debugflag == true)
                                {//
                                    //bigorg.ImWrite(String.Format(@"{0}_{1}_{2}.png",counter,ax,ay));
                                    //Mat roiwrite = roi.Clone() * 30;
                                    //roiwrite.ImWrite(String.Format(@"roi_{0}_{1}_{2}.png", counter, ax, ay));
                                    Cv2.Rectangle(big_c, recMask, 255, 1);//brightness=1, fill
                                    Cv2.ImShow("big_cx30", big_c * 30);
                                    Cv2.ImShow("bigx30", big * 30);
                                    //Cv2.ImShow("imgMask", imgMask);
                                    //Cv2.ImShow("roi", roi * 30);
                                    Cv2.WaitKey(0);
                                }
                            }//using big_c

                            using (CvMemStorage storage = new CvMemStorage())
                                using (CvContourScanner scanner = new CvContourScanner(big.ToIplImage(), storage, CvContour.SizeOf, ContourRetrieval.Tree, ContourChain.ApproxSimple))
                                {
                                    foreach (CvSeq <CvPoint> c in scanner)
                                    {
                                        CvMoments mom = new CvMoments(c, false);
                                        if (c.ElemSize < 2)
                                        {
                                            continue;
                                        }
                                        if (mom.M00 < 1.0)
                                        {
                                            continue;
                                        }
                                        double        mx = mom.M10 / mom.M00;
                                        double        my = mom.M01 / mom.M00;
                                        rawmicrotrack rm = new rawmicrotrack();
                                        rm.ax = ax;
                                        rm.ay = ay;
                                        rm.cx = (int)(mx - big.Width / 2);
                                        rm.cy = (int)(my - big.Height / 2);
                                        rm.pv = (int)(mom.M00);
                                        rms.Add(rm);
                                        //Console.WriteLine(string.Format("{0}   {1} {2}   {3} {4}", rm.pv, ax, ay, rm.cx, rm.cy ));
                                    }
                                }//using contour

                            //big_c.Dispose();

                            counter++;
                        }//using Mat
                }//ay
            }//ax



            OpenCvSharp.CPlusPlus.Point trackpos = new OpenCvSharp.CPlusPlus.Point(0, 0);
            if (rms.Count > 0)
            {
                rawmicrotrack rm     = new rawmicrotrack();
                double        meancx = 0;
                double        meancy = 0;
                double        meanax = 0;
                double        meanay = 0;
                double        meanph = 0;
                double        meanpv = 0;
                double        sumpv  = 0;

                for (int i = 0; i < rms.Count; i++)
                {
                    meanpv += rms[i].pv * rms[i].pv;
                    meancx += rms[i].cx * rms[i].pv;
                    meancy += rms[i].cy * rms[i].pv;
                    meanax += rms[i].ax * rms[i].pv;
                    meanay += rms[i].ay * rms[i].pv;
                    sumpv  += rms[i].pv;
                }

                meancx /= sumpv;//重心と傾きを輝度値で重み付き平均
                meancy /= sumpv;
                meanax /= sumpv;
                meanay /= sumpv;
                meanpv /= sumpv;

                trackpos = new OpenCvSharp.CPlusPlus.Point(
                    (int)(meancx) + 256 - meanax * shiftpitch,
                    (int)(meancy) + 220 - meanay * shiftpitch
                    );

                double anglex = (meanax * shiftpitch * 0.267) / (3.0 * 7.0 * 2.2);
                double angley = (meanay * shiftpitch * 0.267) / (3.0 * 7.0 * 2.2);
                Console.WriteLine(string.Format("{0:f4} {1:f4}", anglex, angley));
            }
            else
            {
                trackpos = new OpenCvSharp.CPlusPlus.Point(-1, -1);
            }


            return(trackpos);
        }//track detection
        private void detectShapeCandidates(ref Bitmap bitmap, Boolean saveShapes)
        {
            Debug.WriteLine("Running OpenCV");
            string      myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            Mat         colorMat = BitmapConverter.ToMat(bitmap);
            MatOfDouble mu       = new MatOfDouble();
            MatOfDouble sigma    = new MatOfDouble();

            Cv2.MeanStdDev(colorMat, mu, sigma);
            double mean = mu.GetArray(0, 0)[0];

            mu.Dispose();
            sigma.Dispose();

            Mat greyMat = new Mat();

            Cv2.CvtColor(colorMat, greyMat, ColorConversion.BgraToGray, 0);
            greyMat = greyMat.GaussianBlur(new OpenCvSharp.CPlusPlus.Size(1, 1), 5, 5, BorderType.Default);
            greyMat = greyMat.Canny(0.5 * mean, 1.2 * mean, 3, true);

            Mat contourMat = new Mat(greyMat.Size(), colorMat.Type());

            greyMat.CopyTo(contourMat);
            var contours = contourMat.FindContoursAsArray(ContourRetrieval.List, ContourChain.ApproxSimple);

            for (int j = 0; j < contours.Length; j++)
            {
                var poly = Cv2.ApproxPolyDP(contours[j], 0.01 * Cv2.ArcLength(contours[j], true), true);
                int num  = poly.Length;

                if (num >= 4 && num < 20)
                {
                    var color = Scalar.Blue;
                    var rect  = Cv2.BoundingRect(poly);

                    if (rect.Height < 20 || rect.Width < 20)
                    {
                        continue;
                    }
                    if (saveShapes)
                    {
                        string path = Path.Combine(myPhotos, "shape_samples");
                        path = Path.Combine(path, "shape_sample_" + Path.GetRandomFileName() + ".png");
                        var matRect = new OpenCvSharp.CPlusPlus.Rect(0, 0, greyMat.Width, greyMat.Height);
                        rect.Inflate((int)(rect.Width * 0.1), (int)(rect.Height * 0.1));
                        rect = rect.Intersect(matRect);
                        Mat shapeMat = greyMat.SubMat(rect);
                        var size     = new OpenCvSharp.CPlusPlus.Size(128, 128);
                        shapeMat = shapeMat.Resize(size);
                        Bitmap shape = shapeMat.ToBitmap();
                        shape.Save(path);
                        shape.Dispose();
                        shapeMat.Dispose();
                        continue;
                    }
                    Cv2.Rectangle(colorMat, rect, color, 2);
                }
            }


            bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(colorMat);
            colorMat.Dispose();
            greyMat.Dispose();
            contourMat.Dispose();
        }
Exemplo n.º 5
0
    // Update is called once per frame
    void Update()
    {
        Mat rawframe = new Mat ();
        Mat frame = new Mat();
        if (cap != null) {
            rawframe = new Mat (frameSize, MatType.CV_8UC1);
            frame = new Mat (new Size (640, (int)(640f * cap.FrameHeight / cap.FrameWidth)), MatType.CV_8UC1);

            if (cap.FrameWidth > 640) {
                cap.Retrieve (rawframe, 0);
                //cap.Read(frame); // get a new frame from camera
                Debug.Log ("frame width = " + frame.Width);
                Cv2.ConvertScaleAbs (rawframe, frame);
            } else {
                cap.Retrieve (frame, 0);
            }

        } else if (cameraTexture != null) {
            rawframe = new Mat (cameraTexture.height, cameraTexture.width, MatType.CV_8UC4, cameraTexture.GetPixels32 ());
            //frame = new Mat (new Size (640, (int)(640f * cameraTexture.height / cameraTexture.width)), MatType.CV_8UC1);
            frame = new Mat (cameraTexture.height, cameraTexture.width, MatType.CV_8UC1);
            Cv2.ConvertScaleAbs (rawframe, frame);
            Cv2.Flip(frame,frame,FlipMode.X);
            Debug.Log ("frame width = " + frame.Width);
        }

        // find face
        OpenCvSharp.CPlusPlus.Rect[] face = face_cascade.DetectMultiScale (
            frame, 1.2, 2, HaarDetectionType.ScaleImage, new Size (150, 150), new Size (350, 350));

        // face found
        if (face.Length > 0) {

            int i = 0;
            int j = 0;
            foreach (OpenCvSharp.CPlusPlus.Rect rec in face) {
                if (rec.Width > face [j].Width)
                    j = i;
                i++;
            }
            facerec = face [j];
            // draw
            facex = facerec.Left + facerec.Width / 2;
            facey = facerec.Top + facerec.Height / 2;
            Cv2.Line (frame, (int)(facex - 20), (int)facey, (int)(facex + 20), (int)facey, new CvScalar (0, 150, 0), 2);
            Cv2.Line (frame, (int)(facex), (int)(facey - 20), (int)(facex), (int)(facey + 20), new CvScalar (0, 150, 0), 2);
            Cv2.Rectangle (frame, facerec, new CvScalar (0, 255, 0), 2);

            // pos of face
            facex = (320f - facex) / 1500;
            facey = (240f - facey) / 1200;

            if(mode==0){
                // roi of eyes
                OpenCvSharp.CPlusPlus.Rect roi = new OpenCvSharp.CPlusPlus.Rect (facerec.Left, facerec.Top + facerec.Height / 4, facerec.Width, facerec.Height / 4);
                OpenCvSharp.CPlusPlus.Mat eyeroi = new Mat (frame, roi);
                IplImage eyeroigray = Cv.CreateImage (new CvSize (eyeroi.Width, eyeroi.Height), BitDepth.U8, 1);
                Mat eyeroigray2 = new Mat (eyeroigray, true);
                Cv2.CvtColor (eyeroi, eyeroigray2, ColorConversion.RgbToGray);
                // find eyes
                CvMemStorage storage = Cv.CreateMemStorage (0);
                CvCircleSegment[] circles = Cv2.HoughCircles (eyeroigray2, HoughCirclesMethod.Gradient, 1, 40,
                                                              cannythr, houghsumthr, 6, 10);
                // eyes found
                if (circles.Length > 1) {
                    for (i=0; i<circles.Length; i++) {
                        Cv2.Circle (frame, new OpenCvSharp.CPlusPlus.Point (circles [i].Center.X + facerec.Left,
                                                                          circles [i].Center.Y + facerec.Top + facerec.Height / 4),
                               											(int)circles [i].Radius, Cv.RGB (255, 0, 0), 3);
                    }
                    if (i == 2) {
                        // draw
                        eyex = (circles [0].Center.X + circles [1].Center.X) / 2 + facerec.Left;
                        eyey = (circles [0].Center.Y + circles [1].Center.Y) / 2 + facerec.Top + facerec.Height / 4;
                        Cv2.Line (frame, (int)(eyex - 20), (int)eyey, (int)(eyex + 20), (int)eyey, new CvScalar (0, 0, 150), 2);
                        Cv2.Line (frame, (int)(eyex), (int)(eyey - 20), (int)(eyex), (int)(eyey + 20), new CvScalar (0, 0, 150), 2);

                        // pos of eyes
                        eyex = (320f - eyex) / 1500;
                        eyey = (210f - eyey) / 1200;
                    }
                }
            }
            /*OpenCvSharp.CPlusPlus.Rect[] eye = eye_cascade.DetectMultiScale(
                eyeroi, 1.1, 2, HaarDetectionType.ScaleImage,new Size(20,20),new Size(100,100));
            if (eye.Length > 1){
                lefteye = eye[0];
                righteye = eye[1];
                float eyex = (((float)lefteye.Left + lefteye.Right + righteye.Left + righteye.Right)/4)/1500;
                float eyey = (((float)lefteye.Bottom + lefteye.Top + righteye.Bottom + righteye.Top)/4)/1200;
                cam.transform.position = new Vector3(eyex,eyey,-0.4f);
                cam.transform.LookAt(new Vector3(0f,0f,0f));
                Debug.Log("eye"+eyex+","+eyey);

                OpenCvSharp.CPlusPlus.Rect leye = new OpenCvSharp.CPlusPlus.Rect(lefteye.Left + facerec.Left,lefteye.Top + facerec.Top+facerec.Height/5, lefteye.Width, lefteye.Height);
                OpenCvSharp.CPlusPlus.Rect reye = new OpenCvSharp.CPlusPlus.Rect(righteye.Left + facerec.Left,righteye.Top + facerec.Top+facerec.Height/5, righteye.Width, righteye.Height);
                Cv2.Rectangle (frame, leye, scalar);
                Cv2.Rectangle (frame, reye, scalar);
            }*/

            // capture mode
            if (mode == 0) {	// use pos of face

                // LPF
                if (lpfmode == 0) { //Inertia
                    x = x * lpfk + facex * (1f - lpfk);
                    y = y * lpfk + facey * (1f - lpfk);
                } else if (lpfmode == 1) {	//mov avr
                    lastx [0] = facex;
                    lasty [0] = facey;
                    float sumx = 0f, sumy = 0f;
                    for (int n=0; n<(int)lpfn; n++) {
                        sumx = sumx + lastx [n];
                        sumy = sumy + lasty [n];
                    }
                    x = sumx / (int)lpfn;
                    y = sumy / (int)lpfn;
                    for (int n=(int)lpfn-1; n>0; n--) {
                        lastx [n] = lastx [n - 1];
                        lasty [n] = lasty [n - 1];
                    }
                }
            } else if (mode == 1) {	// use pos of eye

                // LPF
                if (lpfmode == 0) { //Inertia
                    x = x * lpfk + eyex * (1f - lpfk);
                    y = y * lpfk + eyey * (1f - lpfk);
                } else if (lpfmode == 1) {	//mov avr
                    lastx [0] = eyex;
                    lasty [0] = eyey;
                    float sumx = 0f, sumy = 0f;
                    for (int n=0; n<(int)lpfn; n++) {
                        sumx = sumx + lastx [n];
                        sumy = sumy + lasty [n];
                    }
                    x = sumx / (int)lpfn;
                    y = sumy / (int)lpfn;
                    for (int n=(int)lpfn-1; n>0; n--) {
                        lastx [n] = lastx [n - 1];
                        lasty [n] = lasty [n - 1];
                    }
                }
            }

            // draw x y after lpf
            float xdraw = 320f - x * 1500f;
            float ydraw = 240f - y * 1200f;
            if (mode == 1)
                ydraw = ydraw - 30;
            Cv2.Line (frame, (int)(xdraw - 20), (int)(ydraw - 20), (int)(xdraw + 20), (int)(ydraw + 20), new CvScalar (250, 0, 0), 2);
            Cv2.Line (frame, (int)(xdraw + 20), (int)(ydraw - 20), (int)(xdraw - 20), (int)(ydraw + 20), new CvScalar (250, 0, 0), 2);

            // move eye
            cam.transform.position = new Vector3 (x, y, -0.4f);
            cam.transform.LookAt (new Vector3 (0f, 0f, 0f));

        }

        tex.LoadImage (frame.ToBytes (".png"));

        // calculate area of view , then set to shader
        Ray ray1 = new Ray (GameObject.Find ("Eye").transform.position, new Vector3 (-0.15f, -0.1f, 0f) - GameObject.Find ("Eye").transform.position);
        Ray ray2 = new Ray (GameObject.Find ("Eye").transform.position, new Vector3 (-0.15f, 0.1f, 0f) - GameObject.Find ("Eye").transform.position);
        Ray ray3 = new Ray (GameObject.Find ("Eye").transform.position, new Vector3 (0.15f, 0.1f, 0f) - GameObject.Find ("Eye").transform.position);
        Ray ray4 = new Ray (GameObject.Find ("Eye").transform.position, new Vector3 (0.15f, -0.1f, 0f) - GameObject.Find ("Eye").transform.position);
        RaycastHit hitInfo;
        if (Physics.Raycast (ray1, out hitInfo)) {
            Debug.DrawLine (ray1.origin, hitInfo.point);
            GameObject.Find ("Hitpoint1").transform.position = hitInfo.point;
            float x1 = GameObject.Find ("Hitpoint1").transform.localPosition.x;
            float y1 = GameObject.Find ("Hitpoint1").transform.localPosition.y;
            GameObject.Find ("Plane").GetComponent<MeshRenderer> ().material.SetFloat ("_x1", 0.5f + x1);
            GameObject.Find ("Plane").GetComponent<MeshRenderer> ().material.SetFloat ("_y1", 0.5f + y1);
        }
        if (Physics.Raycast (ray2, out hitInfo)) {
            Debug.DrawLine (ray2.origin, hitInfo.point);
            GameObject.Find ("Hitpoint2").transform.position = hitInfo.point;
            float x2 = GameObject.Find ("Hitpoint2").transform.localPosition.x;
            float y2 = GameObject.Find ("Hitpoint2").transform.localPosition.y;
            GameObject.Find ("Plane").GetComponent<MeshRenderer> ().material.SetFloat ("_x2", 0.5f + x2);
            GameObject.Find ("Plane").GetComponent<MeshRenderer> ().material.SetFloat ("_y2", 0.5f + y2);
        }
        if (Physics.Raycast (ray3, out hitInfo)) {
            Debug.DrawLine (ray3.origin, hitInfo.point);
            GameObject.Find ("Hitpoint3").transform.position = hitInfo.point;
            float x3 = GameObject.Find ("Hitpoint3").transform.localPosition.x;
            float y3 = GameObject.Find ("Hitpoint3").transform.localPosition.y;
            GameObject.Find ("Plane").GetComponent<MeshRenderer> ().material.SetFloat ("_x3", 0.5f + x3);
            GameObject.Find ("Plane").GetComponent<MeshRenderer> ().material.SetFloat ("_y3", 0.5f + y3);
        }
        if (Physics.Raycast (ray4, out hitInfo)) {
            Debug.DrawLine (ray4.origin, hitInfo.point);
            GameObject.Find ("Hitpoint4").transform.position = hitInfo.point;
            float x4 = GameObject.Find ("Hitpoint4").transform.localPosition.x;
            float y4 = GameObject.Find ("Hitpoint4").transform.localPosition.y;
            GameObject.Find ("Plane").GetComponent<MeshRenderer> ().material.SetFloat ("_x4", 0.5f + x4);
            GameObject.Find ("Plane").GetComponent<MeshRenderer> ().material.SetFloat ("_y4", 0.5f + y4);
        }

        // Key
        if (Input.GetKeyDown (KeyCode.Tab)) {
            GameObject.Find ("CamQuad").GetComponent<MeshRenderer> ().enabled = !GameObject.Find ("CamQuad").GetComponent<MeshRenderer> ().enabled;
        }
        if (Input.GetKeyDown (KeyCode.F)) {
            focusmode = !focusmode;
        }
        if (Input.GetKeyDown (KeyCode.Space) && !physicsmode) {
            GameObject.Find ("Ribbon Girl").GetComponent<Animator> ().SetTrigger ("jump");
        }
        if (Input.GetKeyDown (KeyCode.P) && !physicsmode) {
            physicsmode = !physicsmode;
            //
            Object c = GameObject.Find ("Ribbon Girl").GetComponent ("MMD4MecanimModel");
            if (physicsmode) {
                c.GetType ().GetField ("physicsEngine").SetValue (c, 1);
            } else {
                c.GetType ().GetField ("physicsEngine").SetValue (c, 0);
            }
            MMD4MecanimModel m = (MMD4MecanimModel)c;
            if (physicsmode)
                m.initphy ();
            else
                m.disphy ();
        }
        if (Input.GetKeyDown (KeyCode.Escape)) {
            menu = !menu;
        }
        if (Input.GetKeyDown (KeyCode.Mouse0) && menu) {
            if (Input.mousePosition.x > 250 && Input.mousePosition.x < 350 && Input.mousePosition.y > 200 && Input.mousePosition.y < 250) {
                mode = mode + 1;
                mode = mode % 2;
            }
        }

        // FPS
        System.DateTime t1 = System.DateTime.Now;
        fps = 10000000f / (t1.Ticks - tlast.Ticks);
        tlast = t1;
    }
Exemplo n.º 6
0
 public Shape(ShapeType type, OpenCvSharp.CPlusPlus.Rect boundingRect)
 {
     this.type         = type;
     this.boundingRect = boundingRect;
 }
Exemplo n.º 7
0
        private unsafe void OpenCV(ref Bitmap bitmap)
        {
            Mat         testMat = BitmapConverter.ToMat(bitmap);
            MatOfDouble mu      = new MatOfDouble();
            MatOfDouble sigma   = new MatOfDouble();

            Cv2.MeanStdDev(testMat, mu, sigma);
            double mean = mu.GetArray(0, 0)[0];

            mu.Dispose();
            sigma.Dispose();

            SimpleBlobDetector.Params circleParameters = new SimpleBlobDetector.Params();
            circleParameters.FilterByCircularity = true;
            circleParameters.MinCircularity      = (float)0.85;
            circleParameters.MaxCircularity      = (float)1;
            circleParameters.MinArea             = 30; // Modify the value on the fly (TODO use bigger circle)

            SimpleBlobDetector detectCircleBlobs = new SimpleBlobDetector(circleParameters);

            fingerPoints = detectCircleBlobs.Detect(testMat);
            detectCircleBlobs.Dispose();

            // If Finger found basically
            if (fingerPoints != null)
            {
                this.fingerSize = 0;
                int fingerIndex = -1;
                for (int i = 0; i < fingerPoints.Length; i++)
                {
                    if (fingerPoints[i].Size >= this.fingerSize)
                    {
                        this.fingerSize = (int)fingerPoints[i].Size;
                        fingerIndex     = i;
                    }
                }

                if (fingerIndex != -1)
                {
                    OpenCvSharp.CPlusPlus.Point coordinate = fingerPoints[fingerIndex].Pt;
                    this.fingerSize = (int)((fingerPoints[fingerIndex].Size) * Math.Sqrt(2));
                    testMat.Set <Vec3b>(coordinate.Y, coordinate.X, new Vec3b(0, 255, 0));
                    RotatedRect rRect           = new RotatedRect(new Point2f(coordinate.X, coordinate.Y), new Size2f(this.fingerSize, this.fingerSize), 0);
                    Point2f[]   circleVerticies = rRect.Points();
                    //this.fingerCoordinates[0] = coordinate.X;
                    //this.fingerCoordinates[1] = coordinate.Y;
                    int height = (int)(circleVerticies[0].Y - circleVerticies[1].Y);
                    int width  = (int)(circleVerticies[2].X - circleVerticies[1].X);
                    int startX = (int)(circleVerticies[0].X);
                    int startY = (int)(circleVerticies[1].Y);
                    this.fingerDepth = MapColortoDepth(startX, startY, this.fingerSize, this.fingerSize);
                    OpenCvSharp.CPlusPlus.Rect featureRect = new OpenCvSharp.CPlusPlus.Rect(startX, startY, this.fingerSize, this.fingerSize);

                    // Draw box around finger
                    for (int j = 0; j < 4; j++)
                    {
                        Cv2.Line(testMat, circleVerticies[j], circleVerticies[(j + 1) % 4], new Scalar(0, 255, 0));
                    }

                    Boolean    intersectOccurance = false;
                    List <int> intersectIndicies  = new List <int>();
                    for (int i = 0; i < this.controls.Count; i++)
                    {
                        if (this.controls[i].boundingRect.IntersectsWith(featureRect))
                        {
                            double diff = fingerDepth - this.controls[i].depth;
                            if (Math.Abs(diff) < 0.5)
                            {
                                intersectOccurance = true;
                                intersectIndicies.Add(i);
                            }
                        }
                    }

                    System.Text.StringBuilder append = new System.Text.StringBuilder();
                    if (intersectOccurance)
                    {
                        for (int i = 0; i < intersectIndicies.Count; i++)
                        {
                            append.Append(" " + this.controls[intersectIndicies[i]].title + " " + intersectIndicies[i].ToString());
                        }
                        this.OutputText = "Pressed Button" + append; //TODO Make this more obvious
                    }
                    else
                    {
                        this.OutputText = "No State";
                    }
                }
            }

            bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(testMat);
            testMat.Dispose();
        }