private void Button_Click_1(object sender, RoutedEventArgs e)
 {
     if (_isBtnOn)
     {
         arduino.Write("l_down");
         _isBtnOn = false;
     }
     else
     {
         arduino.Write("l_up");
         _isBtnOn = true;
     }
 }
예제 #2
0
        // detektiranje prstiju, crtanje rezultata i binary maske
        private void HandleFrame(Mat frame)
        {
            frame.Resize(640);

            var mask    = CreateHandMask(frame);
            var contour = CreateHandContour(mask);

            var hullIndices = CreateRoughHull(contour, 25).ToArray();

            var vertices       = CreateHullDefectVertices(contour, hullIndices);
            var verticesAngled = FilterVerticesWithAngle(vertices, 60);

            var result = frame.Clone();

            var defectVertices = verticesAngled as DefectVertex[] ?? verticesAngled.ToArray();

            var fingers = defectVertices.Length;

            _arduinoService.Write(new HandUpdateMessage(fingers, contour != null, hullIndices.Length > 0));

            if (!_configService.Config.ShowFeed)
            {
                return;
            }

            if (contour != null)
            {
                frame.DrawContours(new[] { contour }, -1, Scalar.BlueViolet, 2);
            }

            foreach (var vertex in defectVertices)
            {
                frame.Line(vertex.point, vertex.d1, Scalar.Aquamarine, 2);
                frame.Line(vertex.point, vertex.d2, Scalar.Aquamarine, 2);
                frame.Ellipse(new RotatedRect(vertex.point, new Size2f(20, 20), 0), Scalar.Blue, 2);
                result.Ellipse(new RotatedRect(vertex.point, new Size2f(20, 20), 0), Scalar.Blue, 2);
            }

            result.PutText(fingers.ToString(), new Point(20, result.Height - 60), HersheyFonts.HersheySimplex,
                           2,
                           Scalar.DimGray, 2, LineTypes.AntiAlias);


            var rows = result.Rows;
            var cols = result.Cols;

            using var sideBySide = new Mat(rows, cols * 2, MatType.CV_8UC3);
            using var r          = new Mat(sideBySide, new Rect(0, 0, cols, rows));
            using var f          = new Mat(sideBySide, new Rect(cols, 0, cols, rows));


            result.CopyTo(r);
            frame.CopyTo(f);

            Cv2.ImShow("MASKA", mask);
            Cv2.ImShow("REZULTAT", sideBySide);
        }