EdgeImage.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /// <summary>
  9. /// EdgeImage creates an edge image form a depth image and stores it as an <see cref="Image"/>
  10. /// </summary>
  11. public class EdgeImage
  12. {
  13. /// <summary>
  14. /// the size of the edge image
  15. /// </summary>
  16. public ImageSize Size { get; private set; }
  17. /// <summary>
  18. /// the edge image
  19. /// </summary>
  20. public Image<Gray, Byte> Image { get; private set; }
  21. /// <summary>
  22. /// dilated version of the edge image
  23. /// </summary>
  24. public Image<Gray, Byte> RoughImage { get; private set; }
  25. /// <summary>
  26. /// Constructs a new EdgeImage from a depth image.
  27. /// </summary>
  28. /// <param name="depthImage">the depth image</param>
  29. public EdgeImage(DepthImage depthImage)
  30. {
  31. Size = depthImage.Size;
  32. 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));
  33. RoughImage = Image.Dilate(Parameters.EdgeImageRoughNumDilationIterations);
  34. }
  35. /// <summary>
  36. /// Creates a new EdgeImage from the data of another edge image.
  37. /// </summary>
  38. /// <param name="edgeImage">the edge image</param>
  39. /// <param name="roughEdgeImage">dilated version of the edge image</param>
  40. /// <param name="size">size of the dege image</param>
  41. public EdgeImage(Image<Gray, Byte> edgeImage, Image<Gray, Byte> roughEdgeImage, ImageSize size)
  42. {
  43. Size = size;
  44. Image = edgeImage;
  45. RoughImage = roughEdgeImage;
  46. }
  47. /// <summary>
  48. /// Returns true iff an edge is at a point.
  49. /// </summary>
  50. /// <param name="point">the point</param>
  51. /// <returns>true iff an edge is at point</returns>
  52. public bool isEdgeAt(Point point)
  53. {
  54. return isEdgeAt(point.X, point.Y);
  55. }
  56. /// <summary>
  57. /// Returns true iff an edge is at a position.
  58. /// </summary>
  59. /// <param name="x">x coordinate of the position</param>
  60. /// <param name="y">y coordinate of the position</param>
  61. /// <returns>true iff an edge is at the position</returns>
  62. public bool isEdgeAt(int x, int y)
  63. {
  64. return (Image.Data[y, x, 0] > 0);
  65. }
  66. /// <summary>
  67. /// Returns true iff an edge is at a point in the rough edge image.
  68. /// </summary>
  69. /// <param name="point">the point</param>
  70. /// <returns>true iff an edge is at point in the rough edge image</returns>
  71. public bool isRoughEdgeAt(Point point)
  72. {
  73. return isRoughEdgeAt(point.X, point.Y);
  74. }
  75. /// <summary>
  76. /// Returns true iff an edge is at a position in the rough edge image.
  77. /// </summary>
  78. /// <param name="x">x coordinate of the position</param>
  79. /// <param name="y">y coordinate of the position</param>
  80. /// <returns>true iff an edge is at the position in the rough edge image</returns>
  81. public bool isRoughEdgeAt(int x, int y)
  82. {
  83. return (RoughImage.Data[y, x, 0] > 0);
  84. }
  85. /// <summary>
  86. /// Removes all edges inside the given polygon in both edge images.
  87. /// </summary>
  88. /// <param name="polygon">the polygon</param>
  89. public void removeEdgesInsidePolygon(Point[] polygon)
  90. {
  91. Image.FillConvexPoly(polygon, new Gray(0));
  92. RoughImage.FillConvexPoly(polygon, new Gray(0));
  93. }
  94. /// <summary>
  95. /// Returns the first vector starting at start and going in the given direction, which rounds to a point containing an edge in the rough edge image
  96. /// or null if no such vector is found.
  97. /// </summary>
  98. /// <param name="start">start position</param>
  99. /// <param name="direction">search direction</param>
  100. /// <param name="maxSearchSize">maximum number of steps in direction</param>
  101. /// <returns></returns>
  102. public Vector2D findNextRoughEdge(Vector2D start, Vector2D direction, int maxSearchSize = 0)
  103. {
  104. Vector2D maxGrow = (Size.MaxPixel - start) / direction;
  105. Vector2D maxDecline = start / direction.getAbsolute();
  106. int maxStepsX;
  107. if (direction.X > 0)
  108. maxStepsX = maxGrow.IntX;
  109. else if (direction.X < 0)
  110. maxStepsX = maxDecline.IntX;
  111. else
  112. maxStepsX = int.MaxValue;
  113. int maxStepsY;
  114. if (direction.Y > 0)
  115. maxStepsY = maxGrow.IntY;
  116. else if (direction.Y < 0)
  117. maxStepsY = maxDecline.IntY;
  118. else
  119. maxStepsY = int.MaxValue;
  120. int maxSteps = Math.Min(maxStepsX, maxStepsY);
  121. if (maxSearchSize != 0)
  122. maxSteps = Math.Min(maxSteps, maxSearchSize);
  123. Vector2D end = new Vector2D(start);
  124. for (int i = 0; i < maxSteps; i++)
  125. {
  126. end += direction;
  127. if (isRoughEdgeAt(end))
  128. return end;
  129. }
  130. return null;
  131. }
  132. /// <summary>
  133. /// Returns a copy of this edge image.
  134. /// </summary>
  135. /// <returns>copy of this edge image</returns>
  136. public EdgeImage copy()
  137. {
  138. return new EdgeImage(Image.Copy(), RoughImage.Copy(), Size);
  139. }
  140. }
  141. }