EdgeImage.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 int Width { get; private set; }
  20. public int Height { get; private set; }
  21. public EdgeImage(DepthImage depthImage)
  22. {
  23. Width = depthImage.Width;
  24. Height = depthImage.Height;
  25. Image = (depthImage.Image * (255.0f / (float)(depthImage.MaxDepth - depthImage.MinDepth))).Canny(Constants.EdgeImageCannyStartThreshold, Constants.EdgeImageCannyLinkingThreshold, Constants.EdgeImageCannySize).ThresholdBinary(new Gray(0), new Gray(1));
  26. RoughImage = Image.Dilate(Constants.EdgeImageRoughNumDilationIterations);
  27. }
  28. public EdgeImage(Image<Gray, Byte> edgeImage)
  29. {
  30. Image = edgeImage;
  31. RoughImage = Image.Dilate(Constants.EdgeImageRoughNumDilationIterations);
  32. }
  33. public bool isEdgeAt(Point point)
  34. {
  35. return isEdgeAt(point.X, point.Y);
  36. }
  37. public bool isEdgeAt(int x, int y)
  38. {
  39. return (Image.Data[y, x, 0] > 0);
  40. }
  41. public bool isRoughEdgeAt(Point point)
  42. {
  43. return isRoughEdgeAt(point.X, point.Y);
  44. }
  45. public bool isRoughEdgeAt(int x, int y)
  46. {
  47. return (RoughImage.Data[y, x, 0] > 0);
  48. }
  49. public void removeEdgesInsidePolygon(Point[] polygon)
  50. {
  51. Image.FillConvexPoly(polygon, new Gray(0));
  52. RoughImage.FillConvexPoly(polygon, new Gray(0));
  53. }
  54. public Vector2D findNextEdge(Vector2D start, Vector2D direction, int maxSearchSize = 0, bool roughEdge=true, bool returnNullIfNoEdgeFound=true)
  55. {
  56. Vector2D max = new Vector2D(Image.Width-1, Image.Height-1);
  57. Vector2D maxGrow = (max - start) / direction;
  58. Vector2D maxDecline = start / direction.getAbsolute();
  59. int maxStepsX;
  60. if (direction.X > 0)
  61. maxStepsX = maxGrow.IntX;
  62. else if (direction.X < 0)
  63. maxStepsX = maxDecline.IntX;
  64. else
  65. maxStepsX = int.MaxValue;
  66. int maxStepsY;
  67. if (direction.Y > 0)
  68. maxStepsY = maxGrow.IntY;
  69. else if (direction.Y < 0)
  70. maxStepsY = maxDecline.IntY;
  71. else
  72. maxStepsY = int.MaxValue;
  73. int maxSteps = Math.Min(maxStepsX, maxStepsY);
  74. if (maxSearchSize != 0)
  75. maxSteps = Math.Min(maxSteps, maxSearchSize);
  76. Vector2D end = new Vector2D(start);
  77. for (int i = 0; i < maxSteps; i++)
  78. {
  79. end += direction;
  80. if((roughEdge && isRoughEdgeAt(end)) || (!roughEdge && isEdgeAt(end)))
  81. {
  82. return end;
  83. }
  84. }
  85. if (returnNullIfNoEdgeFound)
  86. return null;
  87. else
  88. return end;
  89. }
  90. public EdgeImage copy()
  91. {
  92. return new EdgeImage(Image.Copy());
  93. }
  94. }
  95. }