1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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.Fingers;
- using bbiwarg.Detectors.Palm;
- using bbiwarg.Utility;
- namespace bbiwarg.Detectors.Touch
- {
- class TouchDetector
- {
- private DepthImage depthImage;
- private TouchImage touchImage;
- private List<Finger> fingers;
- private List<TouchEvent> touchEvents;
- public TouchDetector(List<Finger> fingers, DepthImage depthImage, TouchImage touchImage) {
- this.depthImage = depthImage;
- this.touchImage = touchImage;
- this.fingers = fingers;
- this.touchEvents = new List<TouchEvent>();
- float floodValueThreshold = 0.5f;
- foreach (Finger finger in fingers) {
- Vector2D tipPoint = finger.Tip;
- float floodValue = getFloodValue((int)tipPoint.X, (int)tipPoint.Y);
- if (floodValue > floodValueThreshold)
- {
- //correct touchEvent position
- Vector2D direction = finger.Line.Direction;
- float directionFactor = 10;
- float x = HelperFunctions.thresholdRange<float>(0, depthImage.getWidth() - 1, tipPoint.X + directionFactor * direction.X);
- float y = HelperFunctions.thresholdRange<float>(0, depthImage.getHeight() - 1, tipPoint.Y + directionFactor * direction.Y);
- Vector2D tep = new Vector2D(x,y);
- touchImage.setTouchAt((int)tep.X, (int)tep.Y, TouchImageState.touchDetected);
- TouchEvent touchEvent = new TouchEvent(tep, floodValue, finger);
- touchEvents.Add(touchEvent);
- }
- }
- }
- public List<TouchEvent> getTouchEvents() {
- return touchEvents;
- }
- private float getFloodValue(int touchX, int touchY) {
- int searchSize = 15;
- int maxDepthDifference = 20;
- Int16 fingerDiameter = 5;
- Int16 depthAtTouch = (Int16) (depthImage.getDepthAt(touchX, touchY) + fingerDiameter);
- int minX = Math.Max(touchX - searchSize, 0);
- int maxX = Math.Min(touchX + searchSize, depthImage.getWidth());
- int minY = Math.Max(touchY - searchSize, 0);
- int maxY = Math.Min(touchY + searchSize, depthImage.getHeight());
- 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);
- touchImage.setTouchAt(x, y, TouchImageState.touchArea);
- if (Math.Abs(depthAtTouch - depth) < maxDepthDifference) {
- matchedPixels++;
- touchImage.setTouchAt(x, y, TouchImageState.touchAreaMatched);
- }
- countedPixels++;
- }
- }
- float rel = (float)matchedPixels / (float)countedPixels;
- //status bar (% of matched pixels) -> green
- for (int x = minX; x < minX + (maxX-minX)*rel; x++) {
- touchImage.setTouchAt(x, maxY - 1, TouchImageState.touchAreaStatusBar);
- }
- return rel;
- }
- }
- }
|