EdgeImage.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.ConvertScale<byte>(255f / (depthImage.MaxDepth - depthImage.MinDepth), 0).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, Image<Gray, Byte> roughEdgeImage)
  29. {
  30. Image = edgeImage;
  31. RoughImage = roughEdgeImage;
  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 maxGrow = (Constants.MaxPixel - start) / direction;
  57. Vector2D maxDecline = start / direction.getAbsolute();
  58. int maxStepsX;
  59. if (direction.X > 0)
  60. maxStepsX = maxGrow.IntX;
  61. else if (direction.X < 0)
  62. maxStepsX = maxDecline.IntX;
  63. else
  64. maxStepsX = int.MaxValue;
  65. int maxStepsY;
  66. if (direction.Y > 0)
  67. maxStepsY = maxGrow.IntY;
  68. else if (direction.Y < 0)
  69. maxStepsY = maxDecline.IntY;
  70. else
  71. maxStepsY = int.MaxValue;
  72. int maxSteps = Math.Min(maxStepsX, maxStepsY);
  73. if (maxSearchSize != 0)
  74. maxSteps = Math.Min(maxSteps, maxSearchSize);
  75. Vector2D end = new Vector2D(start);
  76. for (int i = 0; i < maxSteps; i++)
  77. {
  78. end += direction;
  79. if ((roughEdge && isRoughEdgeAt(end)) || (!roughEdge && isEdgeAt(end)))
  80. {
  81. return end;
  82. }
  83. }
  84. if (returnNullIfNoEdgeFound)
  85. return null;
  86. else
  87. return end;
  88. }
  89. public EdgeImage copy()
  90. {
  91. return new EdgeImage(Image.Copy(), RoughImage.Copy());
  92. }
  93. }
  94. }