当前位置:   article > 正文

C#联合OpenCV(四)图像的旋转、翻转、仿射变换_c# opencv 旋转图像

c# opencv 旋转图像

图像的翻转

  1. public static void Forexample()
  2. {
  3. var Src_Images = Cv2.ImRead("lenna.png", ImreadModes.Color);
  4. var SRC = new Mat();
  5. var SRC1 = new Mat();
  6. Cv2.Flip(Src_Images, SRC, FlipMode.X);
  7. Cv2.Flip(Src_Images, SRC1, FlipMode.Y);
  8. Cv2.ImShow("1", Src_Images);
  9. Cv2.ImShow("2", SRC);
  10. Cv2.ImShow("3", SRC1);
  11. }

可以实现XY的转置翻转 镜像

  1. //转置矩阵
  2. public static void Transpose(InputArray src, OutputArray dst)
  3. {
  4. //需要搭配使用
  5. Cv2.Flip(src,dst,FlipMode.XY);
  6. Cv2.Transpose(src, dst);
  7. }

 图像的旋转

rotate 这个算子只能旋转固定的角度。90度180度270度

  1. public static void Forexample()
  2. {
  3. var Src_Images = Cv2.ImRead("lenna.png", ImreadModes.Color);
  4. var SRC = new Mat();
  5. var SRC1 = new Mat();
  6. var SRC2 = new Mat();
  7. //90度
  8. Cv2.Rotate(Src_Images, SRC, RotateFlags.Rotate90Clockwise);
  9. //180度
  10. Cv2.Rotate(Src_Images, SRC2, RotateFlags.Rotate180);
  11. //270度
  12. Cv2.Rotate(Src_Images, SRC1, RotateFlags.Rotate90Counterclockwise);
  13. Cv2.ImShow("1", Src_Images);
  14. Cv2.ImShow("2", SRC);
  15. Cv2.ImShow("3", SRC1);
  16. Cv2.ImShow("4", SRC2);
  17. }

 图像的任意角度旋转+仿射变换

  1. //仿射变换
  2. //方法一 旋转缩放
  3. //Point center = new Point(SrcImage.Cols / 2, SrcImage.Rows / 2);
  4. //double angle =0;
  5. //double scale = 1;
  6. //Mat rot_mat = Cv2.GetRotationMatrix2D(center, angle, scale);
  7. //方法二 旋转平移缩放
  8. Point center = new Point(SrcImage.Cols / 2, SrcImage.Rows / 2);
  9. double angle = 60.0 / 180.0 * Cv2.PI;//旋转角度,正值表示逆时针旋转
  10. //旋转平移缩放
  11. Mat rot_mat = GetTransformRotationMatrix2D(center, 100, 0, 0, 1);
  12. Mat warp_rotate_dst =new Mat();
  13. Cv2.WarpAffine(SrcImage, warp_rotate_dst, rot_mat, SrcImage.Size());
  14. Cv2.ImShow("warp_rotate_dst", warp_rotate_dst);
  15. Cv2.ImShow("origindst", SrcImage);

 旋转平移封装代码

  1. public static Mat GetTransformRotationMatrix2D(OpenCvSharp.Point2f center, double Translation_X, double Translation_Y, double Rotate_Angle, double Scale = 1)
  2. {
  3. Mat M = Mat.Zeros(2, 3, MatType.CV_64FC1);
  4. M.At<double>(0, 0) = (double)(Scale * Math.Cos(Rotate_Angle * Math.PI / 180));
  5. M.At<double>(0, 1) = -(double)(Scale * Math.Sin(Rotate_Angle * Math.PI / 180));
  6. M.At<double>(1, 0) = (double)(Scale * Math.Sin(Rotate_Angle * Math.PI / 180));
  7. M.At<double>(1, 1) = (double)(Scale * Math.Cos(Rotate_Angle * Math.PI / 180));
  8. M.At<double>(0, 2) = (double)((1 - M.At<double>(0, 0)) * center.X - M.At<double>(0, 1) * center.Y + Translation_X);
  9. M.At<double>(1, 2) = (double)(M.At<double>(0, 1) * center.X + (1 - M.At<double>(0, 0)) * center.Y + Translation_Y);
  10. return M;
  11. }

图像的平移

 修改代码

 Mat rot_mat = GetTransformRotationMatrix2D(center, 0, 0, 45, 1);

 图像的旋转

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/100343?site
推荐阅读
相关标签
  

闽ICP备14008679号