12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- 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;
- namespace bbiwarg.Detectors.Touch
- {
- class TouchDetector
- {
- private DepthImage depthImage;
- private List<Finger> fingers;
- public TouchDetector(DepthImage depthImage, List<Finger> fingers) {
- this.depthImage = depthImage;
- this.fingers = fingers;
- foreach (Finger finger in fingers) {
-
- FingerPoint fp1 = finger.getFarthest();
- float touchFP1 = isTouchAt(fp1.getX(), fp1.getY());
-
- FingerPoint fp2 = finger.getNearest();
- float touchFP2 = isTouchAt(fp2.getX(), fp2.getY());
- int depthDifference = fp1.getDepth() - fp2.getDepth();
- if(touchFP1 > 0.6f && touchFP2 > 0.6f && depthDifference > 20 && depthDifference < 50)
- Console.WriteLine("TouchEvent bei x:" + fp1.getX() + " y:" + fp1.getY() + " [FP1:" + Math.Round(touchFP1, 1) + " FP2:" + Math.Round(touchFP2, 1) + "]");
- }
- }
- private float isTouchAt(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);
- depthImage.setDepthAt(x, y, Int16.MaxValue - 1);
- if (Math.Abs(depthAtTouch - depth) < maxDepthDifference) {
- matchedPixels++;
- depthImage.setDepthAt(x, y, Int16.MaxValue);
- }
- countedPixels++;
- }
- }
- float rel = (float)matchedPixels / (float)countedPixels;
-
- for (int x = minX; x < minX + (maxX-minX)*rel; x++) {
- depthImage.setDepthAt(x, maxY-1, Int16.MaxValue-2);
- }
- return rel;
- }
- }
- }
|