OpenCv常用数学算法
算法1
计算点p到线AB的距离
/// <summary>
/// 计算点p到线AB的距离
/// </summary>
/// <param name="P"></param>
/// <param name="A"></param>
/// <param name="B"></param>
/// <returns></returns>
private double LineToCircle(Point P, Point A, Point B)
{
int A1 = A.Y - B.Y;
int B1 = B.X - A.X;
int C1 = A.X * B.Y - A.Y * B.X;
return Math.Abs(A1 * P.X + B1 * P.Y + C1) / Math.Sqrt(A1 * A1 + B1 * B1);
}
算法2
求两点间距离
/// <summary>
/// 求两点间距离
/// </summary>
/// <param name="A"></param>
/// <param name="B"></param>
/// <returns></returns>
private double PointDistance(Point A, Point B)
{
return Math.Sqrt(Math.Pow((A.X - B.X), 2) + Math.Pow((A.Y - B.Y), 2));
}
算法3
计算两条线的最小夹角
float LineAngle(Vec4i A, Vec4i B)
{
Point P11 = Point(A[0], A[1]);
Point P12 = Point(A[2], A[3]);
Point P21 = Point(B[0], B[1]);
Point P22 = Point(B[2], B[3]);
float ax = P11.x - P12.x;
float ay = P11.y - P12.y;
float bx = P21.x - P22.x;
float by = P21.y - P22.y;
float x = sqrt(ax * ax + ay * ay);
float y = sqrt(bx * bx + by * by);
float ang = (180.0 * acos(abs(ax * bx + ay * by) / (x * y))) / CV_PI;
if (ang > 90) ang = 90 - ang;
return ang;
}