PalmImage.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Emgu.CV;
  7. using Emgu.CV.Structure;
  8. using System.Drawing;
  9. namespace bbiwarg.Images
  10. {
  11. class PalmImage
  12. {
  13. private Image<Gray, Byte> image;
  14. private int width, height;
  15. private Contour<Point> contour;
  16. bool[,] inContour;
  17. public PalmImage(EdgeImage edgeImage)
  18. {
  19. image = edgeImage.getImage().Clone();
  20. width = image.Width;
  21. height = image.Height;
  22. findContours();
  23. }
  24. private void findContours()
  25. {
  26. image = image.Dilate(3);
  27. contour = image.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
  28. Contour<Point> maxContour = contour;
  29. double maxPerimeter = 0;
  30. int count = 0;
  31. while (contour != null)
  32. {
  33. ++count;
  34. if (contour.Perimeter > maxPerimeter)
  35. {
  36. maxPerimeter = contour.Perimeter;
  37. maxContour = contour;
  38. }
  39. contour = contour.HNext;
  40. }
  41. Console.WriteLine("numContours: " + count + "\nmaxPerimeter = " + maxPerimeter);
  42. inContour = new bool[width, height];
  43. foreach (Point p in maxContour)
  44. inContour[p.X, p.Y] = true;
  45. }
  46. public bool belongsToPalm(int x, int y)
  47. {
  48. return inContour[x, y];
  49. }
  50. }
  51. }