OutputImage.cs 5.8 KB

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