You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OpenCVSharp计算仿射变换时出现部分结果错误的求助

OpenCVSharp仿射变换坐标输出异常问题

近期我在使用OpenPnP工具,为深入理解其原理,尝试用C#结合OpenCVSharp对带有3个基准点(F1-3,F1为PCB坐标系原点)和3个特征点(P1-3)的PCB进行仿射变换。已用CAD软件建模验证几何关系,明确了PCB叠加在机器坐标系(绿点为原点)下特征点的理论位置。

编写的代码如下:

using OpenCvSharp;

namespace affinetransform
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define fiducials in PCB coordinates
            Point2f[] pcbFiducials = new Point2f[]
            {
                new Point2f(0, 0),
                new Point2f(100, 0),
                new Point2f(100, -80)
            };

            // Corresponding fiducial points as measured in machine coordinates
            Point2f[] cameraFiducials = new Point2f[]
            {
                new Point2f(190.62f, -83.7f),
                new Point2f(290.24f, -74.99f),
                new Point2f(297.21f, -154.68f)
            };

            // Compute the affine transformation matrix
            Mat affineTransform = Cv2.GetAffineTransform(InputArray.Create(pcbFiducials), InputArray.Create(cameraFiducials));

            // Define the 3 'features' on the PCB we're interested in, in PCB coordinates
            Point2f[] pcbFeatures = new Point2f[]
            {
                new Point2f(24, -18),
                new Point2f(35, -62),
                new Point2f(74, -28)
            };

            // Convert feature points to a Mat object, because we need to pass Mat type to the Transform method
            Mat pcbFeaturesMat = new Mat(pcbFeatures.Length, // Rows
                                            1,                  // Columns
                                            MatType.CV_32FC2,
                                            pcbFeatures);       // The features on the PCB we want to transform
            for (int i = 0; i < pcbFeaturesMat.Rows; i++)
            {
                for (int j = 0; j < pcbFeaturesMat.Cols; j++)
                {
                    Console.Write($"{pcbFeaturesMat.At<float>(i, j)}\t");
                }
                Console.WriteLine();
            }


            // Transform feature points to machine/camera coordinates
            Mat cameraFeaturesMat = new Mat();
            Cv2.Transform(pcbFeaturesMat, cameraFeaturesMat, affineTransform);

            // Save the transformed points to a CSV file
            SavePointsToCsv(cameraFeaturesMat, @"C:\users\user\desktop\cameraFeatures.csv");
        }

        static void SavePointsToCsv(Mat points, string filename)
        {
            using (var writer = new System.IO.StreamWriter(filename))
            {
                writer.WriteLine("X,Y");
                for (int i = 0; i < points.Rows; i++)
                {
                    float x = points.At<float>(i, 0);
                    float y = points.At<float>(i, 1);
                    writer.WriteLine($"{x},{y}");
                }
            }
            Console.WriteLine($"Saved transformed points to {filename}");
        }
    }
}

代码输出的变换数据存在异常:正确的X值(216.09、230.89、266.78)位置错乱,还出现重复值及异常值,具体输出为:

X,Y
216.09705,230.88875
230.88875,266.7783
266.7783,1.11E-43

计算得到的仿射变换矩阵为:

Transformation Matrix:
0.99619995117187, -0.08712501525878906, 190.6199951171875
0.08709999084472657, 0.9961249351501464, -83.69999694824219

目前无法定位问题,寻求技术解决方向!

内容的提问来源于stack exchange,提问作者mountainred

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 12:39:52