123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- 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;
- namespace bbiwarg.Detectors.Fingers
- {
- class Finger
- {
- private List<FingerPoint> fingerPoints;
- private FingerPoint nearest;
- private FingerPoint farthest;
- private PointF direction;
- private PointF pointOnLine;
- private PointF start;
- private PointF end;
- private bool startEndUpToDate;
- public Finger(FingerPoint fingerPoint)
- {
- fingerPoints = new List<FingerPoint>();
- nearest = fingerPoint;
- farthest = fingerPoint;
- startEndUpToDate = false;
- addFingerPoint(fingerPoint);
- }
- public FingerPoint getNearest()
- {
- return nearest;
- }
- public FingerPoint getFarthest()
- {
- return farthest;
- }
- public PointF getStart()
- {
- if (!startEndUpToDate) updateStartEnd();
- return start;
- }
- public PointF getEnd()
- {
- if (!startEndUpToDate) updateStartEnd();
- return end;
- }
- public void addFingerPoint(FingerPoint fingerPoint)
- {
- fingerPoints.Add(fingerPoint);
- //update nearest
- if (fingerPoint.getDepth() < nearest.getDepth()) nearest = fingerPoint;
- //update farthest
- if (fingerPoint.getDepth() > farthest.getDepth()) farthest = fingerPoint;
- startEndUpToDate = false;
- }
- public float getMinDistance(FingerPoint fingerPoint)
- {
- float minDinstance = float.MaxValue;
- foreach (FingerPoint fp in fingerPoints)
- {
- float distance = fp.getDistanceTo(fingerPoint);
- if (distance < minDinstance)
- {
- minDinstance = distance;
- }
- }
- return minDinstance;
- }
- public float getLength()
- {
- FingerPoint fp1 = getNearest();
- FingerPoint fp2 = getFarthest();
- float distance = fp1.getDistanceTo(fp2);
- return distance;
- }
- private PointF projectToLine(PointF p)
- {
- float px = p.X, py = p.Y, dx = direction.X, dy = direction.Y, ox = pointOnLine.X, oy = pointOnLine.Y;
- float diffx = px - ox;
- float diffy = py - oy;
- float diff_d = (diffx * dx + diffy * dy);
- float d_d = (dx * dx + dy * dy);
- float q = diff_d / d_d;
- return new PointF(ox + q * dx, oy + q * dy);
- }
- private void updateStartEnd() {
- //update direction+pointonline
- PointF[] pointArray = new PointF[fingerPoints.Count];
- int i = 0;
- foreach (FingerPoint fp in fingerPoints)
- {
- pointArray[i] = new PointF(fp.getX(), fp.getY());
- ++i;
- }
- PointCollection.Line2DFitting(pointArray, Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_L2, out direction, out pointOnLine);
- //update start+end
- start = projectToLine(new PointF(farthest.getX(), farthest.getY()));
- end = projectToLine(new PointF(nearest.getX(), nearest.getY()));
- startEndUpToDate = true;
- }
- }
- }
|