123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using OpenCvSharp;
- using PaintDotNet.Base;
- using System.Collections.Generic;
- using System.Drawing;
- namespace PaintDotNet.Adjust
- {
- /// <summary>
- /// 图像几何操作
- /// </summary>
- public class GeometryIntent
- {
- /// <summary>
- /// 图像旋转,参考以下网址
- /// https://blog.csdn.net/andylanzhiyong/article/details/84857915
- /// </summary>
- /// <param name="src">源</param>
- /// <param name="dst">目标</param>
- /// <param name="angle">角度</param>
- public static void ImageRotate(Mat src, Mat dst, FlipMode flipMode)
- {
- if (flipMode == FlipMode.XY)
- {
- Cv2.Flip(src, dst, flipMode);
- }
- else
- {
- Cv2.Transpose(src, dst);
- Cv2.Flip(dst, dst, flipMode);
- }
- }
- /// <summary>
- /// 图像偏移
- /// </summary>
- public static Mat ImageOffset(Mat src, List<Args> lists)
- {
- //偏移量
- int heightOffset = 0;
- int widthOffset = 0;
- Color color = Color.Red;
- for (int i = 0; i < lists.Count; i++)
- {
- Args args = lists[i];
- switch (args.Key)
- {
- case "Vertical":
- heightOffset = int.Parse(args.Value.ToString());
- break;
- case "Horizal":
- widthOffset = int.Parse(args.Value.ToString());
- break;
- case "FillColor":
- color = Color.FromArgb(int.Parse(args.Value.ToString()));
- break;
- default:
- break;
- }
- }
- Mat dst = new Mat();
- Point2f center = new Point2f(src.Cols / 2, src.Rows / 2);
- Mat rot = Cv2.GetRotationMatrix2D(center, 0, 1);
- rot.Set(0, 2, widthOffset * 1.0d);
- rot.Set(1, 2, heightOffset * 1.0d);
- 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) 三通道用*/);
- dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- return src;
- }
- /// <summary>
- /// 图像任意角度旋转
- /// </summary>
- /// <param name="src">源</param>
- /// <param name="dst">目标</param>
- /// <param name="angle">弧度</param>
- /// <param name="flags">插值标志</param>
- public static void ImageRotate(Mat src, Mat dst, float angle, InterpolationFlags flags)
- {
- Mat dst1 = new Mat();
- Point2f center = new Point2f(src.Cols / 2, src.Rows / 2);
- Mat rot = Cv2.GetRotationMatrix2D(center, -angle, 1);
- Size2f s2f = new Size2f(src.Size().Width, src.Size().Height);
- Rect box = new RotatedRect(new Point2f(0, 0), s2f, -angle).BoundingRect();
- double xx = rot.At<double>(0, 2) + box.Width / 2 - src.Cols / 2;
- double zz = rot.At<double>(1, 2) + box.Height / 2 - src.Rows / 2;
- rot.Set(0, 2, xx);
- rot.Set(1, 2, zz);
- //对图片进行仿射变换
- Cv2.WarpAffine(src, dst1, rot, box.Size, flags);
- dst1.CopyTo(dst);
- }
- /// <summary>
- /// 图像旋转
- /// </summary>
- public static Mat MatRotate(Mat src, float angle, Scalar scalar)
- {
- Mat dst = new Mat();
- Point2f center = new Point2f(src.Cols / 2, src.Rows / 2);
- Mat rot = Cv2.GetRotationMatrix2D(center, -angle, 1);
- Size2f s2f = new Size2f(src.Size().Width, src.Size().Height);
- Rect box = new RotatedRect(new Point2f(0, 0), s2f, -angle).BoundingRect();
- double xx = rot.At<double>(0, 2) + box.Width / 2 - src.Cols / 2;
- double zz = rot.At<double>(1, 2) + box.Height / 2 - src.Rows / 2;
- rot.Set(0, 2, xx);
- rot.Set(1, 2, zz);
- Cv2.WarpAffine(src, dst, rot, box.Size, InterpolationFlags.Linear,0, scalar);
- return dst;
- }
- }
- }
|