OutputImage.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Graphics
  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 drawRectangle(int x, int y, int width, int height, Color color, int thichness = 0)
  49. {
  50. Image.Draw(new Rectangle(x, y, width, height), new Rgb(color), thichness);
  51. }
  52. public void drawText(int x, int y, String text, Color color)
  53. {
  54. MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 1, 1);
  55. Image.Draw(text, ref font, new Point(x, y), new Rgb(color));
  56. }
  57. public void drawDefect(ConvexityDefect defect, Color pointColor, Color lineColor) {
  58. drawLineSegment(new Utility.LineSegment2D(defect.OuterShort, defect.Inner), lineColor);
  59. drawLineSegment(new Utility.LineSegment2D(defect.OuterLong, defect.Inner), lineColor);
  60. fillCircle(defect.Inner.IntX, defect.Inner.IntY, 2, pointColor);
  61. fillCircle(defect.OuterShort.IntX, defect.OuterShort.IntY, 2, pointColor);
  62. fillCircle(defect.OuterLong.IntX, defect.OuterLong.IntY, 2, pointColor);
  63. }
  64. public void drawImage(Image<Gray, byte> image, Color color)
  65. {
  66. if (color.R != 0)
  67. {
  68. if (color.R != byte.MaxValue)
  69. Image[0] = Image[0].Or(image.Mul((float)color.R / byte.MaxValue));
  70. else
  71. Image[0] = Image[0].Or(image);
  72. }
  73. if (color.G != 0)
  74. {
  75. if (color.G != byte.MaxValue)
  76. Image[1] = Image[1].Or(image.Mul((float)color.G / byte.MaxValue));
  77. else
  78. Image[1] = Image[1].Or(image);
  79. }
  80. if (color.B != 0)
  81. {
  82. if (color.B != byte.MaxValue)
  83. Image[2] = Image[2].Or(image.Mul((float)color.B / byte.MaxValue));
  84. else
  85. Image[2] = Image[2].Or(image);
  86. }
  87. }
  88. public void drawQuadrangleGrid(Quadrangle quad, Color borderColor, Color gridColor, int numRows, int numCols)
  89. {
  90. Vector2D a = quad.TopLeft;
  91. Vector2D b = quad.TopRight;
  92. Vector2D c = quad.BottomRight;
  93. Vector2D d = quad.BottomLeft;
  94. Vector2D relAB = (b - a) / numCols;
  95. Vector2D relDC = (c - d) / numCols;
  96. Vector2D relBC = (c - b) / numRows;
  97. Vector2D relAD = (d - a) / numRows;
  98. for (int i = 1; i < numCols; i++)
  99. {
  100. drawLineSegment(new bbiwarg.Utility.LineSegment2D(a + i * relAB, d + i * relDC), gridColor);
  101. }
  102. for (int i = 1; i < numRows; i++)
  103. {
  104. drawLineSegment(new bbiwarg.Utility.LineSegment2D(a + i * relAD, b + i * relBC), gridColor);
  105. }
  106. drawLineSegment(new bbiwarg.Utility.LineSegment2D(a, b), borderColor);
  107. drawLineSegment(new bbiwarg.Utility.LineSegment2D(b, c), borderColor);
  108. drawLineSegment(new bbiwarg.Utility.LineSegment2D(c, d), borderColor);
  109. drawLineSegment(new bbiwarg.Utility.LineSegment2D(d, a), borderColor);
  110. }
  111. }
  112. }