OpenCV中的坐标运算 [C#]

在C#语言中,可以使用OpenCV库进行坐标运算,包括平移、缩放、点到点的距离和点到线的距离。

从Nuget下载 OpenCvSharp4.Windows

下面是一些示例代码:

点到点的距离:

cs 复制代码
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;
}

点到线的距离:

cs 复制代码
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=0
    double 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;
}

平移操作:

csharp 复制代码
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;
}

缩放操作:

csharp 复制代码
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;
}

旋转操作:

csharp 复制代码
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;
}
相关推荐
OpenCSG4 分钟前
CSGHub开源版本v1.2.0更新
人工智能
weixin_515202494 分钟前
第R3周:RNN-心脏病预测
人工智能·rnn·深度学习
Altair澳汰尔7 分钟前
数据分析和AI丨知识图谱,AI革命中数据集成和模型构建的关键推动者
人工智能·算法·机器学习·数据分析·知识图谱
AI视觉网奇28 分钟前
人脸生成3d模型 Era3D
人工智能·计算机视觉
call me by ur name31 分钟前
VLM--CLIP作分类任务的损失函数
人工智能·机器学习·分类
云空39 分钟前
《QT 5.14.1 搭建 opencv 环境全攻略》
开发语言·qt·opencv
编码小哥40 分钟前
opencv中的色彩空间
opencv·计算机视觉
吃个糖糖1 小时前
34 Opencv 自定义角点检测
人工智能·opencv·计算机视觉
禁默1 小时前
2024年图像处理、多媒体技术与机器学习
图像处理·人工智能·microsoft
花花少年1 小时前
【Windows版】opencv 和opencv_contrib配置
opencv·opencv_contrib