OutputImage.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 1, 1);
  54. Image.Draw(text, ref font, new Point(x, y), new Rgb(color));
  55. }
  56. public void drawImage(Image<Gray, byte> image, Color color)
  57. {
  58. Image[0] = Image[0].Or(image.Mul((float)color.R / byte.MaxValue));
  59. Image[1] = Image[1].Or(image.Mul((float)color.G / byte.MaxValue));
  60. Image[2] = Image[2].Or(image.Mul((float)color.B / byte.MaxValue));
  61. }
  62. public void drawQuadrangleGrid(Quadrangle quad, Color borderColor, Color gridColor, int numRows, int numCols) {
  63. Vector2D a = quad.BottomLeft;
  64. Vector2D b = quad.TopLeft;
  65. Vector2D c = quad.TopRight;
  66. Vector2D d = quad.BottomRight;
  67. Vector2D relAB = (b - a) / numRows;
  68. Vector2D relDC = (c - d) / numRows;
  69. Vector2D relBC = (c - b) / numCols;
  70. Vector2D relAD = (d - a) / numCols;
  71. for (int i = 1; i < numRows; i++)
  72. {
  73. drawLineSegment(new bbiwarg.Utility.LineSegment2D(a + i * relAB, d + i * relDC), gridColor);
  74. }
  75. for (int i = 1; i < numCols; i++)
  76. {
  77. drawLineSegment(new bbiwarg.Utility.LineSegment2D(a + i * relAD, b + i * relBC), gridColor);
  78. }
  79. drawLineSegment(new bbiwarg.Utility.LineSegment2D(a, b), borderColor);
  80. drawLineSegment(new bbiwarg.Utility.LineSegment2D(b, c), borderColor);
  81. drawLineSegment(new bbiwarg.Utility.LineSegment2D(c, d), borderColor);
  82. drawLineSegment(new bbiwarg.Utility.LineSegment2D(d, a), borderColor);
  83. }
  84. }
  85. }