EdgeImage.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using bbiwarg.Utility;
  2. using Emgu.CV;
  3. using Emgu.CV.Structure;
  4. using System;
  5. using System.Drawing;
  6. namespace bbiwarg.Images
  7. {
  8. public class EdgeImage
  9. {
  10. public Image<Gray, Byte> Image { get; private set; }
  11. public Image<Gray, byte> RoughImage { get; private set; }
  12. public EdgeImage(DepthImage depthImage)
  13. {
  14. 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));
  15. RoughImage = Image.Dilate(Parameters.EdgeImageRoughNumDilationIterations);
  16. }
  17. public EdgeImage(Image<Gray, Byte> edgeImage, Image<Gray, Byte> roughEdgeImage)
  18. {
  19. Image = edgeImage;
  20. RoughImage = roughEdgeImage;
  21. }
  22. public bool isEdgeAt(Point point)
  23. {
  24. return isEdgeAt(point.X, point.Y);
  25. }
  26. public bool isEdgeAt(int x, int y)
  27. {
  28. return (Image.Data[y, x, 0] > 0);
  29. }
  30. public bool isRoughEdgeAt(Point point)
  31. {
  32. return isRoughEdgeAt(point.X, point.Y);
  33. }
  34. public bool isRoughEdgeAt(int x, int y)
  35. {
  36. return (RoughImage.Data[y, x, 0] > 0);
  37. }
  38. public void removeEdgesInsidePolygon(Point[] polygon)
  39. {
  40. Image.FillConvexPoly(polygon, new Gray(0));
  41. RoughImage.FillConvexPoly(polygon, new Gray(0));
  42. }
  43. public Vector2D findNextRoughEdge(Vector2D start, Vector2D direction, int maxSearchSize = 0)
  44. {
  45. Vector2D maxGrow = (Parameters.ImageMaxPixel - start) / direction;
  46. Vector2D maxDecline = start / direction.getAbsolute();
  47. int maxStepsX;
  48. if (direction.X > 0)
  49. maxStepsX = maxGrow.IntX;
  50. else if (direction.X < 0)
  51. maxStepsX = maxDecline.IntX;
  52. else
  53. maxStepsX = int.MaxValue;
  54. int maxStepsY;
  55. if (direction.Y > 0)
  56. maxStepsY = maxGrow.IntY;
  57. else if (direction.Y < 0)
  58. maxStepsY = maxDecline.IntY;
  59. else
  60. maxStepsY = int.MaxValue;
  61. int maxSteps = Math.Min(maxStepsX, maxStepsY);
  62. if (maxSearchSize != 0)
  63. maxSteps = Math.Min(maxSteps, maxSearchSize);
  64. Vector2D end = new Vector2D(start);
  65. for (int i = 0; i < maxSteps; i++)
  66. {
  67. end += direction;
  68. if (isRoughEdgeAt(end))
  69. return end;
  70. }
  71. return null;
  72. }
  73. public EdgeImage copy()
  74. {
  75. return new EdgeImage(Image.Copy(), RoughImage.Copy());
  76. }
  77. }
  78. }