OpenCV4中读取摄像头参数YAML文件(C++和C#)

0:环境

OpenCV4.1.2 + Win10 + Visual Studio2019

我的工程是C#的,需要程序读取摄像头参数yaml中的摄像头参数矩阵、畸变矩阵。

其实这些opencv都已经帮我们实现了。

1. 校正后摄像头参数文件内容

%YAML:1.0
---
calibration_time: "Fri Apr  3 14:42:35 2020"
nframes: 19
image_width: 640
image_height: 480
board_width: 6
board_height: 9
square_size: 2.5000000000000000e-01
flags: 0
camera_matrix: !!opencv-matrix
   rows: 3
   cols: 3
   dt: d
   data: [ 7.2763439887528966e+02, 0., 3.2443968653576053e+02, 0.,
       7.2680443579297730e+02, 2.5505615729084781e+02, 0., 0., 1. ]
distortion_coefficients: !!opencv-matrix
   rows: 5
   cols: 1
   dt: d
   data: [ -4.3909094302956458e-01, 3.0440882575317296e-01,
       -1.0954197868000457e-03, 1.4524597201079157e-03, 0. ]
avg_reprojection_error: 1.3128532570033699e-01
per_view_reprojection_errors: !!opencv-matrix
   rows: 19
   cols: 1
   dt: f
   data: [ 1.21728398e-01, 8.25564787e-02, 7.56738111e-02,
       2.98229724e-01, 1.05492942e-01, 1.16928436e-01, 1.27288967e-01,
       1.39672935e-01, 6.24108762e-02, 1.26655892e-01, 1.18461736e-01,
       1.06294610e-01, 1.42708063e-01, 1.31679595e-01, 1.58154428e-01,
       1.18642427e-01, 1.05000891e-01, 7.35756531e-02, 1.10303752e-01 ]
extrinsic_parameters: !!opencv-
//...内容省略
image_points: 
//...内容省略

我需要用程序自动读取文件内的camera_matrix和distortion_coefficients以校正图像。

2. C++源码

参考[1]给出了C++中读取参数的代码。但是opencv版本较老。我修改了几个变量名。

#include <opencv2/opencv.hpp>
#include <vector>


int main(int argc, char** argv)
{
    //read a yaml file
    cv::FileStorage fs_read("camera.yml", cv::FileStorage::READ);
    // first method:use (type) operator on FIleNode
    int frame_count = (int)fs_read["nframes"];

    std::string date;
    //second method:use FileNode::operator >>
    fs_read["calibration_time"] >> date;

    int image_width = fs_read["image_width"];
    int image_height = fs_read["image_height"];



    cv::Mat camera_matrix, distort_coefficient;
    fs_read["camera_matrix"] >> camera_matrix;
    fs_read["distortion_coefficients"] >> distort_coefficient;

    std::cout << "frame_count: " << frame_count << std::endl
        << "calibration data: " << date << std::endl
        << "camera matrix: " << camera_matrix << std::endl
        << "distortion coeffs: " << distort_coefficient << std::endl;


    fs_read.release();

    return 0;
}

运行结果:

 

3 C#源码


// opencv4sharp
using OpenCvSharp;

string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
filePath = filePath + "camera.yml";

FileStorage fs = new FileStorage(filePath, FileStorage.Mode.Read);
string calibration_time = fs["calibration_time"].ToString();
Console.WriteLine("calibration_time: {0}", calibration_time);

Mat cameraMatrix, distCoeffs;
cameraMatrix = fs["camera_matrix"].ToMat();
distCoeffs = fs["distortion_coefficients"].ToMat();

Console.WriteLine("camera_matrix: ");
Console.Write(camera_matrix.At<double>(0 ,0));
Console.Write(" ");
Console.Write(camera_matrix.At<double>(0, 1));
Console.Write(" ");
Console.WriteLine(camera_matrix.At<double>(0, 2));

Console.Write(camera_matrix.At<double>(1, 0));
Console.Write(" ");
Console.Write(camera_matrix.At<double>(1, 1));
Console.Write(" ");
Console.WriteLine(camera_matrix.At<double>(1, 2));

Console.Write(camera_matrix.At<double>(2, 0));
Console.Write(" ");
Console.Write(camera_matrix.At<double>(2, 1));
Console.Write(" ");
Console.WriteLine(camera_matrix.At<double>(2, 2));

运行结果:

参考[1]:OpenCV中读取YAML文件方法

猜你喜欢

转载自blog.csdn.net/qq_27158179/article/details/105295287