GeometryIntent.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using OpenCvSharp;
  2. using SmartCoalApplication.Core.Param;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace SmartCoalApplication.Adjust
  10. {
  11. /// <summary>
  12. /// 图像几何操作
  13. /// </summary>
  14. public class GeometryIntent
  15. {
  16. /// <summary>
  17. /// 图像旋转,参考以下网址
  18. /// https://blog.csdn.net/andylanzhiyong/article/details/84857915
  19. /// </summary>
  20. /// <param name="src">源</param>
  21. /// <param name="dst">目标</param>
  22. /// <param name="angle">角度</param>
  23. public static void ImageRotate(Mat src, Mat dst, FlipMode flipMode)
  24. {
  25. if (flipMode == FlipMode.XY)
  26. {
  27. Cv2.Flip(src, dst, flipMode);
  28. }
  29. else
  30. {
  31. Cv2.Transpose(src, dst);
  32. Cv2.Flip(dst, dst, flipMode);
  33. }
  34. }
  35. /// <summary>
  36. /// 图像偏移
  37. /// </summary>
  38. public static Mat ImageOffset(Mat src, List<Args> lists)
  39. {
  40. //偏移量
  41. int heightOffset = 0;
  42. int widthOffset = 0;
  43. Color color = Color.Red;
  44. for (int i = 0; i < lists.Count; i++)
  45. {
  46. Args args = lists[i];
  47. switch (args.Key)
  48. {
  49. case "Vertical":
  50. heightOffset = int.Parse(args.Value.ToString());
  51. break;
  52. case "Horizal":
  53. widthOffset = int.Parse(args.Value.ToString());
  54. break;
  55. case "FillColor":
  56. color = Color.FromArgb(int.Parse(args.Value.ToString()));
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. Mat dst = new Mat();
  63. Point2f center = new Point2f(src.Cols / 2, src.Rows / 2);
  64. Mat rot = Cv2.GetRotationMatrix2D(center, 0, 1);
  65. rot.Set(0, 2, widthOffset * 1.0d);
  66. rot.Set(1, 2, heightOffset * 1.0d);
  67. Cv2.WarpAffine(src, dst, rot, src.Size(), InterpolationFlags.Linear, BorderTypes.Constant, new Scalar(color.B, color.G, color.R, 255)/*Scalar.FromRgb(color.R,color.G,color.B) 三通道用*/);
  68. dst.CopyTo(src);
  69. if (dst != null) dst.Dispose();
  70. return src;
  71. }
  72. /// <summary>
  73. /// 图像任意角度旋转
  74. /// </summary>
  75. /// <param name="src">源</param>
  76. /// <param name="dst">目标</param>
  77. /// <param name="angle">弧度</param>
  78. /// <param name="flags">插值标志</param>
  79. public static void ImageRotate(Mat src, Mat dst, float angle, InterpolationFlags flags)
  80. {
  81. Mat dst1 = new Mat();
  82. Point2f center = new Point2f(src.Cols / 2, src.Rows / 2);
  83. Mat rot = Cv2.GetRotationMatrix2D(center, -angle, 1);
  84. Size2f s2f = new Size2f(src.Size().Width, src.Size().Height);
  85. Rect box = new RotatedRect(new Point2f(0, 0), s2f, -angle).BoundingRect();
  86. double xx = rot.At<double>(0, 2) + box.Width / 2 - src.Cols / 2;
  87. double zz = rot.At<double>(1, 2) + box.Height / 2 - src.Rows / 2;
  88. rot.Set(0, 2, xx);
  89. rot.Set(1, 2, zz);
  90. //对图片进行仿射变换
  91. Cv2.WarpAffine(src, dst1, rot, box.Size, flags);
  92. dst1.CopyTo(dst);
  93. }
  94. /// <summary>
  95. /// 图像旋转
  96. /// </summary>
  97. public static Mat MatRotate(Mat src, float angle, Scalar scalar)
  98. {
  99. Mat dst = new Mat();
  100. Point2f center = new Point2f(src.Cols / 2, src.Rows / 2);
  101. Mat rot = Cv2.GetRotationMatrix2D(center, -angle, 1);
  102. Size2f s2f = new Size2f(src.Size().Width, src.Size().Height);
  103. Rect box = new RotatedRect(new Point2f(0, 0), s2f, -angle).BoundingRect();
  104. double xx = rot.At<double>(0, 2) + box.Width / 2 - src.Cols / 2;
  105. double zz = rot.At<double>(1, 2) + box.Height / 2 - src.Rows / 2;
  106. rot.Set(0, 2, xx);
  107. rot.Set(1, 2, zz);
  108. Cv2.WarpAffine(src, dst, rot, box.Size, InterpolationFlags.Linear, 0, scalar);
  109. return dst;
  110. }
  111. }
  112. }