EdgeImage.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Emgu.CV;
  8. using Emgu.CV.Structure;
  9. using bbiwarg.Recognition.FingerRecognition;
  10. using System.Diagnostics;
  11. using bbiwarg.Utility;
  12. using bbiwarg.Graphics;
  13. namespace bbiwarg.Images
  14. {
  15. class EdgeImage
  16. {
  17. public Image<Gray, Byte> Image { get; private set; }
  18. public Image<Gray, byte> RoughImage { get; private set; }
  19. public EdgeImage(DepthImage depthImage)
  20. {
  21. Image = (depthImage.Image * (255.0f / (float)(depthImage.MaxDepth - depthImage.MinDepth))).Canny(Constants.EdgeImageCannyStartThreshold, Constants.EdgeImageCannyLinkingThreshold, Constants.EdgeImageCannySize).ThresholdBinary(new Gray(0), new Gray(1));
  22. RoughImage = Image.Dilate(Constants.EdgeImageRoughNumDilationIterations);
  23. }
  24. public EdgeImage(Image<Gray, Byte> edgeImage)
  25. {
  26. Image = edgeImage;
  27. RoughImage = Image.Dilate(Constants.EdgeImageRoughNumDilationIterations);
  28. }
  29. public bool isEdgeAt(Point point)
  30. {
  31. return isEdgeAt(point.X, point.Y);
  32. }
  33. public bool isEdgeAt(int x, int y)
  34. {
  35. return (Image.Data[y, x, 0] > 0);
  36. }
  37. public bool isRoughEdgeAt(Point point)
  38. {
  39. return isRoughEdgeAt(point.X, point.Y);
  40. }
  41. public bool isRoughEdgeAt(int x, int y)
  42. {
  43. return (RoughImage.Data[y, x, 0] > 0);
  44. }
  45. public void removeEdgesInsidePolygon(Point[] polygon)
  46. {
  47. Image.FillConvexPoly(polygon, new Gray(0));
  48. RoughImage.FillConvexPoly(polygon, new Gray(0));
  49. }
  50. public Vector2D findNextEdge(Vector2D start, Vector2D direction, int maxSearchSize = 0, bool roughEdge=true, bool returnNullIfNoEdgeFound=true)
  51. {
  52. Vector2D max = new Vector2D(Image.Width-1, Image.Height-1);
  53. Vector2D maxGrow = (max - start) / direction;
  54. Vector2D maxDecline = start / direction.getAbsolute();
  55. int maxStepsX;
  56. if (direction.X > 0)
  57. maxStepsX = maxGrow.IntX;
  58. else if (direction.X < 0)
  59. maxStepsX = maxDecline.IntX;
  60. else
  61. maxStepsX = int.MaxValue;
  62. int maxStepsY;
  63. if (direction.Y > 0)
  64. maxStepsY = maxGrow.IntY;
  65. else if (direction.Y < 0)
  66. maxStepsY = maxDecline.IntY;
  67. else
  68. maxStepsY = int.MaxValue;
  69. int maxSteps = Math.Min(maxStepsX, maxStepsY);
  70. if (maxSearchSize != 0)
  71. maxSteps = Math.Min(maxSteps, maxSearchSize);
  72. Vector2D end = new Vector2D(start);
  73. for (int i = 0; i < maxSteps; i++)
  74. {
  75. end += direction;
  76. if((roughEdge && isRoughEdgeAt(end)) || (!roughEdge && isEdgeAt(end)))
  77. {
  78. return end;
  79. }
  80. }
  81. if (returnNullIfNoEdgeFound)
  82. return null;
  83. else
  84. return end;
  85. }
  86. public EdgeImage copy()
  87. {
  88. return new EdgeImage(Image.Copy());
  89. }
  90. }
  91. }