OutputImage.cs 5.9 KB

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