在C#语言中,可以使用OpenCV库进行坐标运算,包括平移、缩放、点到点的距离和点到线的距离。
从Nuget下载 OpenCvSharp4.Windows
下面是一些示例代码:
点到点的距离:
using OpenCvSharp;/// <summary>
/// 点到点距离
/// </summary>
/// <param name="point1">点1</param>
/// <param name="point2">点2</param>
/// <returns>距离</returns>
private double Distance_Point2Point(Point2d point1, Point2d point2)
{double distance = Math.Sqrt(Math.Pow(point1.X - point2.X, 2) + Math.Pow(point1.Y - point2.Y, 2));return distance;
}
点到线的距离:
using OpenCvSharp;/// <summary>
/// 点到直线距离
/// </summary>
/// <param name="point">点</param>
/// <param name="line">两点组成直线</param>
/// <returns>距离</returns>
private double Distance_Point2Line(Point2d point, Point2d[] line)
{Point2d lineStart = line[0];Point2d lineEnd = line[1];// 以lineStart到lineEnd为直线,直线方程AX+BY+C=0double A = lineStart.Y - lineEnd.Y;double B = lineEnd.X - lineStart.X;double C = lineStart.X * lineEnd.Y - lineStart.Y * lineEnd.X;double distance = Math.Abs(A * point.X + B * point.Y + C) / Math.Sqrt(Math.Pow(A, 2) + Math.Pow(B, 2));return distance;
}
平移操作:
using OpenCvSharp;/// <summary>
/// 平移操作
/// </summary>
/// <param name="point_lst">点集</param>
/// <param name="originalPoint">新坐标系的原点</param>
/// <returns>点集</returns>
private List<Point2d> Translated_Option(List<Point2d> point_lst, Point2d originalPoint)
{List<Point2d> lst = new List<Point2d>();foreach(Point2d p in point_lst){Point2d pz = new Point2d(p.X - originalPoint.X, p.Y - originalPoint.Y);lst.Add(pz);}return lst;
}
缩放操作:
using OpenCvSharp;/// <summary>
/// 缩放操作
/// </summary>
/// <param name="point_lst">点集</param>
/// <param name="originalPoint">缩放的中心点</param>
/// <param name="scaleFactor">缩放因子</param>
/// <returns>点集</returns>
private List<Point2d> Scale_Option(List<Point2d> point_lst, Point2d originalPoint, double scaleFactor)
{List<Point2d> lst = new List<Point2d>();foreach (Point2d p in point_lst){double X = (p.X - originalPoint.X) * scaleFactor + originalPoint.X;double Y = (p.Y - originalPoint.Y) * scaleFactor + originalPoint.Y;Point2d pz = new Point2d(X, Y);lst.Add(pz);}return lst;
}
旋转操作:
using OpenCvSharp;/// <summary>
/// 旋转操作
/// </summary>
/// <param name="point_lst">点集</param>
/// <param name="originalPoint">旋转的中心点</param>
/// <param name="radian">偏转角的弧度值</param>
/// <returns>点集</returns>
private List<Point2d> Rotate_Option(List<Point2d> point_lst, Point2d originalPoint, double radian)
{List<Point2d> lst = new List<Point2d>();foreach (Point2d p in point_lst){double _X = p.X - originalPoint.X;double _Y = p.Y - originalPoint.Y;double X_ = _X * Math.Cos(radian) + _Y * Math.Sin(radian);double Y_ = _Y * Math.Cos(radian) - _X * Math.Sin(radian);double X = X_ + originalPoint.X;double Y = Y_ + originalPoint.Y;Point2d pz = new Point2d(X, Y);lst.Add(pz);}return lst;
}