EdgeImage.cs 5.7 KB

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