EdgeImage.cs 2.9 KB

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