EdgeImage.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.ConvertScale<byte>(255f / (depthImage.MaxDepth - depthImage.MinDepth), 0).Canny(Parameters.EdgeImageCannyStartThreshold, Parameters.EdgeImageCannyLinkingThreshold, Parameters.EdgeImageCannySize).ThresholdBinary(new Gray(0), new Gray(1));
  22. RoughImage = Image.Dilate(Parameters.EdgeImageRoughNumDilationIterations);
  23. }
  24. public EdgeImage(Image<Gray, Byte> edgeImage, Image<Gray, Byte> roughEdgeImage)
  25. {
  26. Image = edgeImage;
  27. RoughImage = roughEdgeImage;
  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 findNextRoughEdge(Vector2D start, Vector2D direction, int maxSearchSize = 0)
  51. {
  52. Vector2D maxGrow = (Parameters.ImageMaxPixel - start) / direction;
  53. Vector2D maxDecline = start / direction.getAbsolute();
  54. int maxStepsX;
  55. if (direction.X > 0)
  56. maxStepsX = maxGrow.IntX;
  57. else if (direction.X < 0)
  58. maxStepsX = maxDecline.IntX;
  59. else
  60. maxStepsX = int.MaxValue;
  61. int maxStepsY;
  62. if (direction.Y > 0)
  63. maxStepsY = maxGrow.IntY;
  64. else if (direction.Y < 0)
  65. maxStepsY = maxDecline.IntY;
  66. else
  67. maxStepsY = int.MaxValue;
  68. int maxSteps = Math.Min(maxStepsX, maxStepsY);
  69. if (maxSearchSize != 0)
  70. maxSteps = Math.Min(maxSteps, maxSearchSize);
  71. Vector2D end = new Vector2D(start);
  72. for (int i = 0; i < maxSteps; i++)
  73. {
  74. end += direction;
  75. if (isRoughEdgeAt(end))
  76. return end;
  77. }
  78. return null;
  79. }
  80. public EdgeImage copy()
  81. {
  82. return new EdgeImage(Image.Copy(), RoughImage.Copy());
  83. }
  84. }
  85. }