123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 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) {
- //farthest
- FingerPoint fp1 = finger.getFarthest();
- float touchFP1 = isTouchAt(fp1.getX(), fp1.getY());
- //nearest
- 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 depthAtTouch = depthImage.getDepthAt(touchX, touchY);
- 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);//counted pixels -> red
- if (Math.Abs(depthAtTouch - depth) < maxDepthDifference) {
- matchedPixels++;
- depthImage.setDepthAt(x, y, Int16.MaxValue);//matched pixels -> blue
- }
- countedPixels++;
- }
- }
- float rel = (float)matchedPixels / (float)countedPixels;
- //status bar (% of matched pixels) -> green
- for (int x = minX; x < minX + (maxX-minX)*rel; x++) {
- depthImage.setDepthAt(x, maxY-1, Int16.MaxValue-2);
- }
- return rel;
- }
- }
- }
|