1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Emgu.CV;
- using Emgu.CV.Structure;
- using bbiwarg.Images;
- using bbiwarg.Detectors.FingerDetection;
- using bbiwarg.Detectors.PalmDetection;
- using bbiwarg.Utility;
- using bbiwarg.Graphics;
- namespace bbiwarg.Detectors.TouchDetection
- {
- class TouchDetector
- {
- private DepthImage depthImage;
- private OutputImage outputImage;
- private List<Finger> fingers;
- public List<TouchEvent> TouchEvents { get; private set; }
- public TouchDetector(List<Finger> fingers, DepthImage depthImage, OutputImage outputImage)
- {
- this.depthImage = depthImage;
- this.outputImage = outputImage;
- this.fingers = fingers;
- this.TouchEvents = new List<TouchEvent>();
- foreach (Finger finger in fingers)
- {
- Vector2D tipPoint = finger.Tip;
- outputImage.fillCircle(tipPoint.IntX, tipPoint.IntY, 3, Constants.TouchEventTipColor);
- float floodValue = getFloodValue(tipPoint);
- if (floodValue > Constants.TouchEventMinFloodValue)
- {
- //correct touchEvent position
- Vector2D direction = finger.LineSegment.Line.Direction;
- Vector2D tep = (tipPoint + Constants.TouchEventTipCorrectionFactor * direction).moveInBound(0, 0, depthImage.Width - 1, depthImage.Height - 1);
- outputImage.fillCircle(tep.IntX, tep.IntY, 5, Constants.TouchEventDetectedColor);
- TouchEvent touchEvent = new TouchEvent(tep, floodValue, finger);
- TouchEvents.Add(touchEvent);
- }
- }
- }
- private float getFloodValue(Point touchPoint)
- {
- Int16 depthAtTouch = (Int16)(depthImage.getDepthAt(touchPoint) + Constants.TouchEventFingerDiameter);
- int minX = Math.Max(touchPoint.X - Constants.TouchEventAreaSize / 2, 0);
- int maxX = Math.Min(touchPoint.X + Constants.TouchEventAreaSize / 2, depthImage.Width-1);
- int minY = Math.Max(touchPoint.Y - Constants.TouchEventAreaSize / 2, 0);
- int maxY = Math.Min(touchPoint.Y + Constants.TouchEventAreaSize / 2, depthImage.Height-1);
- int matchedPixels = 0;
- int countedPixels = 0;
- for (int x = minX; x <= maxX; x++)
- {
- for (int y = minY; y <= maxY; y++)
- {
- Int16 depth = depthImage.getDepthAt(x, y);
- Color color = outputImage.getColotAt(x, y);
- Color subtractColor;
- if (depthAtTouch < depth && Math.Abs(depthAtTouch - depth) < Constants.TouchEventMaxDepthDifference)
- {
- matchedPixels++;
- subtractColor = Constants.TouchEventAreaMatchedSubtractColor;
- }
- else
- {
- subtractColor = Constants.TouchEventAreaNonMatchedSubtractColor;
- }
- Color newColor = Color.FromArgb(Math.Max(color.R - subtractColor.R, 0), Math.Max(color.G - subtractColor.G, 0), Math.Max(color.B - subtractColor.B, 0));
- outputImage.drawPixel(x, y, newColor);
- countedPixels++;
- }
- }
- float rel = (float)matchedPixels / (float)countedPixels;
- outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY)), Constants.TouchEventStatusBarColor);
- outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY-1), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY-1)), Constants.TouchEventStatusBarColor);
-
- return rel;
- }
- }
- }
|