Я пытаюсь выполнить базовое преобразование печатной платы с тремя реперами и тремя элементами на C# с помощью OpenCVSharp.
Я смоделировал геометрию в некотором программном обеспечении САПР, чтобы еще раз проверить работоспособность, вот мой ' PCB'.
F1-3 — это 3 контрольные точки, причем F1 соответствует 0,0 системы координат печатной платы. P1-3 — это три «функции» на печатной плате, которые меня интересуют.
[img]https://i.sstatic .net/499mXKLj.png[/img]
Вот эта «PCB», наложенная на «машину», где зеленая точка — это 0,0 машины. Итак, по сути, я уже понял, какими будут местоположения трех функций относительно машины 0,0.

Я думаю, мне нужно выполнить «аффинное преобразование», и за несколько часов я собрал это воедино.
Код: Выделить всё
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
2, // Columns
MatType.CV_32FC2,
pcbFeatures); // The features on the PCB we want to transform
// 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(i, 0);
float y = points.At(i, 1);
writer.WriteLine($"{x},{y}");
}
}
Console.WriteLine($"Saved transformed points to {filename}");
}
}
}
Код: Выделить всё
X,Y
216.09705,230.88875
266.7783,190.62
190.62,190.62
Подробнее здесь: https://stackoverflow.com/questions/785 ... ith-opencv