OutputImage.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using bbiwarg.Utility;
  8. using Emgu.CV;
  9. using Emgu.CV.Structure;
  10. namespace bbiwarg.Output
  11. {
  12. class OutputImage : Image<Rgb, byte>
  13. {
  14. public OutputImage(int width, int height)
  15. : base(width, height)
  16. {
  17. }
  18. public OutputImage(ImageSize size)
  19. : base(size.Width, size.Height)
  20. {
  21. }
  22. public Color getColotAt(int x, int y)
  23. {
  24. byte red = Data[y, x, 0];
  25. byte green = Data[y, x, 1];
  26. byte blue = Data[y, x, 2];
  27. return Color.FromArgb(red, green, blue);
  28. }
  29. public void drawLineSegment(bbiwarg.Utility.LineSegment2D lineSegment, Color color, int thickness = 1)
  30. {
  31. Draw(new Emgu.CV.Structure.LineSegment2D(lineSegment.P1, lineSegment.P2), new Rgb(color), thickness);
  32. }
  33. public void drawContour(Contour<Point> contour, Color color, int thickness = 1)
  34. {
  35. Draw(contour, new Rgb(color), thickness);
  36. }
  37. public void drawPoints(Seq<Point> points, Color color, int thickness = 1)
  38. {
  39. Draw(points, new Rgb(color), thickness);
  40. }
  41. public void drawPixel(Point position, Color color)
  42. {
  43. Data[position.Y, position.X, 0] = color.R;
  44. Data[position.Y, position.X, 0] = color.G;
  45. Data[position.Y, position.X, 0] = color.B;
  46. }
  47. public void fillCircle(Point position, float radius, Color color)
  48. {
  49. Draw(new CircleF(position, radius), new Rgb(color), 0);
  50. }
  51. public void fillRectangle(Rectangle rect, Color color)
  52. {
  53. Draw(rect, new Rgb(color), -1);
  54. }
  55. public void drawRectangle(Rectangle rect, Color color, int thichness = 0)
  56. {
  57. Draw(rect, new Rgb(color), thichness);
  58. }
  59. public void drawText(Point position, String text, Color color)
  60. {
  61. MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 1, 1);
  62. Draw(text, ref font, position, new Rgb(color));
  63. }
  64. public void drawDefect(ConvexityDefect defect, Color pointColor, Color lineColor)
  65. {
  66. drawLineSegment(new Utility.LineSegment2D(defect.OuterShort, defect.Inner), lineColor);
  67. drawLineSegment(new Utility.LineSegment2D(defect.OuterLong, defect.Inner), lineColor);
  68. fillCircle(defect.Inner, 2, pointColor);
  69. fillCircle(defect.OuterShort, 2, pointColor);
  70. fillCircle(defect.OuterLong, 2, pointColor);
  71. }
  72. public void drawQuadrangleGrid(Quadrangle quad, Color borderColor, Color gridColor, int numRows, int numCols)
  73. {
  74. Vector2D a = quad.TopLeft;
  75. Vector2D b = quad.TopRight;
  76. Vector2D c = quad.BottomRight;
  77. Vector2D d = quad.BottomLeft;
  78. Vector2D relAB = (b - a) / numCols;
  79. Vector2D relDC = (c - d) / numCols;
  80. Vector2D relBC = (c - b) / numRows;
  81. Vector2D relAD = (d - a) / numRows;
  82. for (int i = 1; i < numCols; i++)
  83. {
  84. drawLineSegment(new bbiwarg.Utility.LineSegment2D(a + i * relAB, d + i * relDC), gridColor);
  85. }
  86. for (int i = 1; i < numRows; i++)
  87. {
  88. drawLineSegment(new bbiwarg.Utility.LineSegment2D(a + i * relAD, b + i * relBC), gridColor);
  89. }
  90. drawLineSegment(new bbiwarg.Utility.LineSegment2D(a, b), borderColor);
  91. drawLineSegment(new bbiwarg.Utility.LineSegment2D(b, c), borderColor);
  92. drawLineSegment(new bbiwarg.Utility.LineSegment2D(c, d), borderColor);
  93. drawLineSegment(new bbiwarg.Utility.LineSegment2D(d, a), borderColor);
  94. }
  95. public void drawBorder(Color color)
  96. {
  97. drawRectangle(new Rectangle(0, 0, Width, Height), color);
  98. }
  99. public void drawTouchGesture(List<Vector2D> positions, Vector2D scale, float opacity = 1)
  100. {
  101. int numPositions = positions.Count;
  102. Color lineColor = Parameters.TouchEventVisualizerLineColor;
  103. Color pointColor = Parameters.TouchEventVisualizerPointColor;
  104. Color lineColorFaded = Color.FromArgb((int)(opacity * lineColor.R), (int)(opacity * lineColor.G), (int)(opacity * lineColor.B));
  105. Color pointColorFaded = Color.FromArgb((int)(opacity * pointColor.R), (int)(opacity * pointColor.G), (int)(opacity * pointColor.B));
  106. for (int i = 1; i < numPositions; i++)
  107. {
  108. drawLineSegment(new Utility.LineSegment2D(positions[i - 1].scale(scale), positions[i].scale(scale)), lineColorFaded);
  109. }
  110. Vector2D lastPos = positions[numPositions - 1].scale(scale);
  111. fillCircle(lastPos, 3, pointColorFaded);
  112. }
  113. public void drawImage(Image<Gray, byte> image, Color color)
  114. {
  115. if (color.R != 0)
  116. {
  117. if (color.R != byte.MaxValue)
  118. this[0] = this[0].Or(image.Mul((float)color.R / byte.MaxValue));
  119. else
  120. this[0] = this[0].Or(image);
  121. }
  122. if (color.G != 0)
  123. {
  124. if (color.G != byte.MaxValue)
  125. this[1] = this[1].Or(image.Mul((float)color.G / byte.MaxValue));
  126. else
  127. this[1] = this[1].Or(image);
  128. }
  129. if (color.B != 0)
  130. {
  131. if (color.B != byte.MaxValue)
  132. this[2] = this[2].Or(image.Mul((float)color.B / byte.MaxValue));
  133. else
  134. this[2] = this[2].Or(image);
  135. }
  136. }
  137. }
  138. }