GeometryIntent.cs 4.2 KB

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