OutputImage.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using BBIWARG.Utility;
  2. using Emgu.CV;
  3. using Emgu.CV.Structure;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. namespace BBIWARG.Output
  8. {
  9. /// <summary>
  10. /// An <see cref="Image"/> which provides additional drawing functions.
  11. /// </summary>
  12. internal class OutputImage : Image<Rgb, byte>
  13. {
  14. /// <summary>
  15. /// Creates an OutputImage.
  16. /// </summary>
  17. /// <param name="width">image width</param>
  18. /// <param name="height">image height</param>
  19. public OutputImage(int width, int height)
  20. : base(width, height)
  21. {
  22. }
  23. /// <summary>
  24. /// Creates an OutputImage.
  25. /// </summary>
  26. /// <param name="size">image size</param>
  27. public OutputImage(ImageSize size)
  28. : base(size.Width, size.Height)
  29. {
  30. }
  31. /// <summary>
  32. /// Draws a border around the image.
  33. /// </summary>
  34. /// <param name="color">color used to draw the border</param>
  35. public void drawBorder(Color color)
  36. {
  37. drawRectangle(new Rectangle(0, 0, Width, Height), color);
  38. }
  39. /// <summary>
  40. /// Draws a contour.
  41. /// </summary>
  42. /// <param name="contour">contour to draw</param>
  43. /// <param name="color">color used to draw the contour</param>
  44. /// <param name="thickness">thickness of the contour</param>
  45. public void drawContour(Contour<Point> contour, Color color, int thickness = 1)
  46. {
  47. Draw(contour, new Rgb(color), thickness);
  48. }
  49. /// <summary>
  50. /// Draws a convexity defect.
  51. /// </summary>
  52. /// <param name="defect">the defect to draw</param>
  53. /// <param name="pointColor">color used to draw the points of the defect</param>
  54. /// <param name="lineColor">color used to draw the lines of the defect</param>
  55. public void drawDefect(ConvexityDefect defect, Color pointColor, Color lineColor)
  56. {
  57. drawLineSegment(new Utility.LineSegment2D(defect.OuterShort, defect.Inner), lineColor);
  58. drawLineSegment(new Utility.LineSegment2D(defect.OuterLong, defect.Inner), lineColor);
  59. fillCircle(defect.Inner, 2, pointColor);
  60. fillCircle(defect.OuterShort, 2, pointColor);
  61. fillCircle(defect.OuterLong, 2, pointColor);
  62. }
  63. /// <summary>
  64. /// Draws a grayscale image into channels of the image.
  65. /// </summary>
  66. /// <param name="image">the image to draw</param>
  67. /// <param name="color">if a color component is != 0 the corresponding image channel is drawn to and image is scaled by (color component value / 255)</param>
  68. public void drawImage(Image<Gray, byte> image, Color color)
  69. {
  70. if (color.R != 0)
  71. {
  72. if (color.R != byte.MaxValue)
  73. this[0] = this[0].Or(image.Mul((float)color.R / byte.MaxValue));
  74. else
  75. this[0] = this[0].Or(image);
  76. }
  77. if (color.G != 0)
  78. {
  79. if (color.G != byte.MaxValue)
  80. this[1] = this[1].Or(image.Mul((float)color.G / byte.MaxValue));
  81. else
  82. this[1] = this[1].Or(image);
  83. }
  84. if (color.B != 0)
  85. {
  86. if (color.B != byte.MaxValue)
  87. this[2] = this[2].Or(image.Mul((float)color.B / byte.MaxValue));
  88. else
  89. this[2] = this[2].Or(image);
  90. }
  91. }
  92. /// <summary>
  93. /// Draws a line segment.
  94. /// </summary>
  95. /// <param name="lineSegment">line segment to draw</param>
  96. /// <param name="color">color used to draw the line segment</param>
  97. /// <param name="thickness">thickness of the line segment</param>
  98. public void drawLineSegment(Utility.LineSegment2D lineSegment, Color color, int thickness = 1)
  99. {
  100. Draw(new Emgu.CV.Structure.LineSegment2D(lineSegment.P1, lineSegment.P2), new Rgb(color), thickness);
  101. }
  102. /// <summary>
  103. /// Colors a pixel.
  104. /// </summary>
  105. /// <param name="position">position of the pixel</param>
  106. /// <param name="color">new color for pixel</param>
  107. public void drawPixel(Point position, Color color)
  108. {
  109. Data[position.Y, position.X, 0] = color.R;
  110. Data[position.Y, position.X, 0] = color.G;
  111. Data[position.Y, position.X, 0] = color.B;
  112. }
  113. /// <summary>
  114. /// Draws a grid into a quadrangle.
  115. /// </summary>
  116. /// <param name="quad">the quadrangle</param>
  117. /// <param name="borderColor">color used to draw the border lines</param>
  118. /// <param name="gridColor">color used to draw the grid</param>
  119. /// <param name="numRows">number of rows in the grid</param>
  120. /// <param name="numCols">number of columns in the grid</param>
  121. public void drawQuadrangleGrid(Quadrangle quad, Color borderColor, Color gridColor, int numRows, int numCols)
  122. {
  123. Vector2D a = quad.TopLeft;
  124. Vector2D b = quad.TopRight;
  125. Vector2D c = quad.BottomRight;
  126. Vector2D d = quad.BottomLeft;
  127. Vector2D relAB = (b - a) / numCols;
  128. Vector2D relDC = (c - d) / numCols;
  129. Vector2D relBC = (c - b) / numRows;
  130. Vector2D relAD = (d - a) / numRows;
  131. for (int i = 1; i < numCols; i++)
  132. {
  133. drawLineSegment(new Utility.LineSegment2D(a + i * relAB, d + i * relDC), gridColor);
  134. }
  135. for (int i = 1; i < numRows; i++)
  136. {
  137. drawLineSegment(new Utility.LineSegment2D(a + i * relAD, b + i * relBC), gridColor);
  138. }
  139. drawLineSegment(new Utility.LineSegment2D(a, b), borderColor);
  140. drawLineSegment(new Utility.LineSegment2D(b, c), borderColor);
  141. drawLineSegment(new Utility.LineSegment2D(c, d), borderColor);
  142. drawLineSegment(new Utility.LineSegment2D(d, a), borderColor);
  143. }
  144. /// <summary>
  145. /// Draws a unfilled rectangle.
  146. /// </summary>
  147. /// <param name="rect">the rectangle</param>
  148. /// <param name="color">color used to raw the rectangle</param>
  149. /// <param name="thichness">thickness of the rectangle</param>
  150. public void drawRectangle(Rectangle rect, Color color, int thichness = 0)
  151. {
  152. Draw(rect, new Rgb(color), thichness);
  153. }
  154. /// <summary>
  155. /// Draws text.
  156. /// </summary>
  157. /// <param name="position">top left corner to draw the text at</param>
  158. /// <param name="text">the text to draw</param>
  159. /// <param name="color">color used to draw the text</param>
  160. public void drawText(Point position, String text, Color color)
  161. {
  162. MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 1, 1);
  163. Draw(text, ref font, position, new Rgb(color));
  164. }
  165. /// <summary>
  166. /// Draws a touch gesture as the current point and a faded line.
  167. /// </summary>
  168. /// <param name="positions">touch positions</param>
  169. /// <param name="scale">scale used to transform given positions into positions in the image</param>
  170. /// <param name="opacity">opacity of the faded lines</param>
  171. public void drawTouchGesture(List<Vector2D> positions, Vector2D scale, float opacity = 1)
  172. {
  173. int numPositions = positions.Count;
  174. Color lineColor = Parameters.TouchEventVisualizerLineColor;
  175. Color pointColor = Parameters.TouchEventVisualizerPointColor;
  176. Color lineColorFaded = Color.FromArgb((int)(opacity * lineColor.R), (int)(opacity * lineColor.G), (int)(opacity * lineColor.B));
  177. Color pointColorFaded = Color.FromArgb((int)(opacity * pointColor.R), (int)(opacity * pointColor.G), (int)(opacity * pointColor.B));
  178. for (int i = 1; i < numPositions; i++)
  179. {
  180. drawLineSegment(new Utility.LineSegment2D(positions[i - 1].scale(scale), positions[i].scale(scale)), lineColorFaded);
  181. }
  182. Vector2D lastPos = positions[numPositions - 1].scale(scale);
  183. fillCircle(lastPos, 3, pointColorFaded);
  184. }
  185. /// <summary>
  186. /// Draws a filled circle.
  187. /// </summary>
  188. /// <param name="position">center of the circle</param>
  189. /// <param name="radius">radius of the circle</param>
  190. /// <param name="color">color used to draw the circle</param>
  191. public void fillCircle(Point position, float radius, Color color)
  192. {
  193. Draw(new CircleF(position, radius), new Rgb(color), 0);
  194. }
  195. /// <summary>
  196. /// Draws a filled rectangle.
  197. /// </summary>
  198. /// <param name="rect">the rectangle</param>
  199. /// <param name="color">color used to fill the rectangle</param>
  200. public void fillRectangle(Rectangle rect, Color color)
  201. {
  202. Draw(rect, new Rgb(color), -1);
  203. }
  204. /// <summary>
  205. /// Returns the color at a position.
  206. /// </summary>
  207. /// <param name="x">x coordinate of the position</param>
  208. /// <param name="y">y coordinate of the position</param>
  209. /// <returns>color at position (x, y)</returns>
  210. public Color getColorAt(int x, int y)
  211. {
  212. byte red = Data[y, x, 0];
  213. byte green = Data[y, x, 1];
  214. byte blue = Data[y, x, 2];
  215. return Color.FromArgb(red, green, blue);
  216. }
  217. }
  218. }