1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Emgu.CV;
- using Emgu.CV.Structure;
- using System.Drawing;
- using bbiwarg.Detectors.Fingers;
- using bbiwarg.Utility;
- namespace bbiwarg.Images
- {
- class PalmImage
- {
- private Image<Gray, Byte> image;
- private int width, height;
- bool[,] inContour;
-
- public PalmImage(EdgeImage edgeImage, FingerTracker fingerTracker)
- {
- image = edgeImage.getImage(); //.Clone();
- width = image.Width;
- height = image.Height;
-
- foreach (Finger f in fingerTracker.getFingers())
- {
- Vector<float> tip = new Vector<float>(new float[] { f.Tip.X, f.Tip.Y });
- Vector<float> hand = new Vector<float>(new float[] { f.Hand.X, f.Hand.Y });
- for (int x = 0; x < width; ++x)
- {
- for (int y = 0; y < height; ++y)
- {
- if (image.Data[y, x, 0] != 0)
- {
- Vector<float> p = new Vector<float>(new float[] { x, y });
- float l2 = (tip - hand) * (tip - hand);
- float t = (p - tip) * (hand - tip) / l2;
- if (t >= 0.0 && t <= 1.0)
- {
- Vector<float> tmp = hand - tip;
- tmp.multiply(t);
- Vector<float> projection = tip + tmp;
- if (p.distance(projection) <= 15)
- image.Data[y, x, 0] = 0;
- }
- }
- }
- }
- }
- findContour();
- }
- private void findContour()
- {
- //image = image.Dilate(1);
- Contour<Point> contour = image.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE,
- Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST);
- Contour<Point> maxContour = contour;
- double maxPerimeter = 0;
- while (contour != null)
- {
- if (contour.Perimeter > maxPerimeter)
- {
- maxPerimeter = contour.Perimeter;
- maxContour = contour;
- }
- contour = contour.HNext;
- }
- inContour = new bool[width, height];
- if (maxContour != null)
- {
- foreach (Point p in maxContour)
- inContour[p.X, p.Y] = true;
- }
- }
- public bool belongsToPalm(int x, int y)
- {
- return inContour[x, y];
- }
- }
- }
|