EmguCV - 鱼眼校准

问题描述 投票:0回答:1

在一个学校项目中,我正在尝试校准具有鱼眼失真的大华网络摄像机。 我们需要校准相机以使图像不失真,因为我们需要平面图像来进行图像处理。

到目前为止,我们已经成功地使用 OpenCV 在 Python 中进行了校准,但脚本的其余部分是用 C# 编写的,因此我们希望使用 EmguCV 包装器(OpenCV for .NET)将代码转换为 C#

如果我错了,请纠正我,但到目前为止我已经完成了以下步骤:

  • 我拍了 50 张棋盘网格的照片,用来寻找角点
  • 我让 OpenCV 计算正确的矩阵,一个称为 CameraMatrix,另一个是失真系数。
  • 使用 2 个计算出的矩阵,我们对图像进行去畸变,结果是一个没有畸变的平面图像。

在 Python 中,此代码可以运行。我得到了两个可以消除失真的矩阵。 我尝试在 C# 中复制这些矩阵,而不进行其余的校准。由于是相同的相机设置,因此畸变应该是相同的。 然而,当我尝试对矩阵进行硬编码时,结果不是我想要的。

这可能是与 EmguCV(.NET 的 OpenCV 包装器)相关的问题,还是与我的代码相关的问题?

Python代码

img = cv2.imread(filename)
K = np.array(...) # removed for brevity
D = np.array(...) # removed for brevity
DIM = (width, height) # image resolution
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2)
undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)

C#代码

Matrix<double> cameraMatrix = new Matrix<double>(3, 3);
Matrix<double> distortionCoeffs = new Matrix<double>(4, 1);
Mat outputMap1 = new Mat();
Mat outputMap2 = new Mat();
Mat r = new Mat(); // Can be empty, according to the documentation
Mat p = new Mat(); // Can be empty, according to the documentation
Fisheye.InitUndistorRectifyMap(cameraMatrix, distortionCoeffs, r, p, image.Size, DepthType.Cv32F, outputMap1, outputMap2);
CvInvoke.Remap(image, undistorted, outputMap1, outputMap2, Inter.Linear, BorderType.Constant);
c# python opencv computer-vision emgucv
1个回答
0
投票

写下答案希望对某人有帮助

我试图完全这样做,首先使用Python生成K&D,然后按照此在Google Colab中取消扭曲鱼眼图像https://medium.com/@kenneth Jiang/calibrate-fisheye-lens-using-opencv -333b05afa0b0

然后想使用 OpenCVSharp 在 C# 代码中执行相同的操作。 经过近一天的尝试,我终于能够想出一个工作代码,如下所示。

适合我的 C# 代码

        OpenCvSharp.Size DIM = new OpenCvSharp.Size(1280, 1024);
        Mat K = new Mat(3, 3, MatType.CV_64FC1, new double[,] { { 411.23914063230376, 0.0, 625.1291183561409 }, { 0.0, 411.64580081704975, 493.39384657887706 }, { 0.0, 0.0, 1.0 } });
        Mat D = new Mat(4, 1, MatType.CV_64FC1, new double[,] { { 0.004481730916688151 }, { 0.018118651707822304 }, { -0.010641336952383246 }, { 0.001460032817948352 } });

        // Initialize undistortion map
        Mat map1 = new Mat(), map2 = new Mat();
        Cv2.FishEye.InitUndistortRectifyMap(K, D, Mat.Eye(3, 3, MatType.CV_64FC1), K, DIM, MatType.CV_16SC2, map1, map2);

        // Load the fish-eye image
        Mat imgFishEye = Cv2.ImRead("fisheye/1.bmp");

        // Undistort the fish-eye image
        Mat undistortedImg = new Mat();
        Cv2.Remap(imgFishEye, undistortedImg, map1, map2, InterpolationFlags.Linear, BorderTypes.Constant);

        // Display the undistorted image
        Cv2.ImShow("Undistorted Image", undistortedImg);
© www.soinside.com 2019 - 2024. All rights reserved.