C#+OpenCV基础(四)_旋转翻转透视
1、旋转
/// <summary>
/// 旋转图片
/// </summary>
/// <param name="mat">图片</param>
/// <param name="rotate">旋转方向</param>
/// <returns></returns>
public static Mat Rotate(Mat mat, RotateFlags rotate)
{
Mat result = new Mat();
Cv2.Rotate(mat, result, rotate);
return result;
}
/// <summary>
/// 任意角度旋转图片
/// </summary>
/// <param name="mat">图片</param>
/// <param name="input">旋转矩阵;</param>
/// <param name="dsize">输出图片大小</param>
/// <param name="flags">像素插值方式</param>
/// <param name="borderMode">背景填充默认为常量</param>
/// <param name="borderValue">填充颜色默认为黑色</param>
/// <returns></returns>
public static Mat WarpAffine(Mat mat, InputArray input, Size dsize,
InterpolationFlags flags = InterpolationFlags.Linear, BorderTypes borderMode = BorderTypes.Constant, Scalar? borderValue = null)
{
Mat dst = new Mat();
//Mat input = Cv2.GetRotationMatrix2D(new Point2f(mat.Cols/2f,mat.Rows/2f),45,0.6);
Cv2.WarpAffine(mat, dst, input, dsize, flags, borderMode, borderValue); // dsize=mat.Size()
return dst;
}
2、翻转
/// <summary>
/// 翻转图片
/// </summary>
/// <param name="mat">图片</param>
/// <param name="flipCode">绕X/Y轴翻转</param>
/// <returns></returns>
public static Mat Flip(Mat mat, FlipMode flipCode)
{
Mat result = new Mat();
Cv2.Flip(mat, result, flipCode);
return result;
}
3、透视
/// <summary>
/// 透视图片
/// </summary>
/// <param name="mat">图片</param>
/// <param name="srcPoints">源图像中的 三角形顶点坐标 Point2f[] srcPoints = new Point2f[]{ new Point2f(0,0), new Point2f(mat.Cols,0), new Point2f(0,mat.Rows)};</param>
/// <param name="dstPoints">目标图像中的 三角形顶点坐标 Point2f[] dstPoints = new Point2f[]{ new Point2f(mat.Cols *0.00f,mat.Rows*0.33f), new Point2f(mat.Cols *0.85f,mat.Rows*0.25f), new Point2f(mat.Cols *0.15f,mat.Rows*0.70f)};</param>
/// <returns></returns>
public static Mat GetAffineTransform(Mat mat, Point2f[] srcPoints, Point2f[] dstPoints)
{
Mat dst = new Mat();
//Point2f[] srcPoints = new Point2f[]
//{
// new Point2f(0,0),
// new Point2f(mat.Cols,0),
// new Point2f(0,mat.Rows)
//};
//Point2f[] dstPoints = new Point2f[]
//{
// new Point2f(mat.Cols *0.00f,mat.Rows*0.33f),
// new Point2f(mat.Cols *0.85f,mat.Rows*0.25f),
// new Point2f(mat.Cols *0.15f,mat.Rows*0.70f)
//};
Mat input = Cv2.GetAffineTransform(srcPoints, dstPoints);
Cv2.WarpAffine(mat, dst, input, mat.Size()); // dsize=mat.Size()
return dst;
}
本文来自博客园,作者:꧁执笔小白꧂,转载请注明原文链接:https://www.cnblogs.com/qq2806933146xiaobai/p/18294361