12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- 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;
- private List<TouchEvent> touchEvents;
- public TouchDetector(DepthImage depthImage, List<Finger> fingers) {
- this.depthImage = depthImage;
- this.fingers = fingers;
- this.touchEvents = new List<TouchEvent>();
- float touchValueThreshold = 0.5f;
- foreach (Finger finger in fingers) {
- FingerPoint fp1 = finger.getFarthest();
- FingerPoint fp2 = finger.getNearest();
- FingerPoint fp;
- if (fp1.getY() < fp2.getY())
- fp = fp1;
- else
- fp = fp2;
- float touchValue = getTouchValueAt(fp.getX(), fp.getY());
- if (touchValue > touchValueThreshold)
- {
- TouchEvent touchEvent = new TouchEvent(fp.getX(), fp.getY(), touchValue);
- touchEvents.Add(touchEvent);
- }
- }
- }
- public List<TouchEvent> getTouchEvents() {
- return touchEvents;
- }
- private float getTouchValueAt(int touchX, int touchY) {
- int searchSize = 15;
- int maxDepthDifference = 20;
- Int16 fingerDiameter = 10;
- 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);//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;
- }
- }
- }
|