EdgeImage.cs 5.7 KB

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