12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using bbiwarg.Images;
- using bbiwarg.Output;
- using bbiwarg.Recognition.Tracking;
- using bbiwarg.Utility;
- using bbiwarg.Input.InputHandling;
- namespace bbiwarg.Recognition.TouchRecognition
- {
- /// <summary>
- /// Keeps track of touches over a period of time and generates touch events
- /// </summary>
- class TouchTracker : Tracker<Touch, TrackedTouch>
- {
- /// <summary>
- /// the touch events in the current frame
- /// </summary>
- private List<TouchEvent> touchEvents;
- /// <summary>
- /// Initializes a new instance of the TouchTracker class.
- /// </summary>
- /// <param name="imageSize">Size of the input image.</param>
- public TouchTracker(ImageSize imageSize)
- : base(imageSize)
- {
- touchEvents = new List<TouchEvent>();
- }
- /// <summary>
- /// Updates the tracked touches, and stores the (optimized) touches in frameData.trackedTouches and the touch events in frameData.touchEvents.
- /// </summary>
- /// <param name="frameData"></param>
- public void trackTouches(FrameData frameData)
- {
- trackObjects(frameData.DetectedTouches);
- frameData.TrackedTouches = getOptimizedTouches();
- frameData.TouchEvents = flushTouchEvents();
- }
- /// <summary>
- /// Calculates the similarity [0-1] of a tracked touch and a detected touch.
- /// </summary>
- /// <param name="trackedTouch">the tracked touch</param>
- /// <param name="detectedTouch">the detected touch</param>
- /// <returns>the similarity</returns>
- public override float calculateSimilarity(TrackedTouch trackedTouch, Touch detectedTouch)
- {
- return (trackedTouch.FingerID == detectedTouch.Finger.TrackID) ? 1 : 0;
- }
- /// <summary>
- /// Creates a new TrackedTouch
- /// </summary>
- /// <param name="detectedObject">the detected touch</param>
- /// <returns>a new TrackedTouch</returns>
- protected override TrackedTouch createTrackedObject(Touch detectedObject)
- {
- TrackedTouch tt = new TrackedTouch(idPool.getNextUnusedID(), detectedObject, Parameters.TouchTrackerNumFramesDetectedUntilTracked, Parameters.TouchTrackerNumFramesLostUntilDeleted);
- tt.TouchEvent += touchEvent;
- return tt;
- }
- /// <summary>
- /// Get all optimized representations of all tracked touches
- /// </summary>
- /// <returns>all optimized tracked touches</returns>
- private List<Touch> getOptimizedTouches()
- {
- List<Touch> optimizedTouchs = new List<Touch>();
- foreach (TrackedTouch tp in TrackedObjects)
- {
- if (tp.IsTouchActive)
- optimizedTouchs.Add(tp.OptimizedTouch);
- }
- return optimizedTouchs;
- }
- private void touchEvent(object sender, TouchEvent e)
- {
- touchEvents.Add(e);
- }
- public List<TouchEvent> flushTouchEvents()
- {
- List<TouchEvent> flushedTouchEvents = touchEvents;
- touchEvents = new List<TouchEvent>();
- return flushedTouchEvents;
- }
- }
- }
|