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

2023-05-16

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文件方法

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • ESP32 优化笔记(四)IRAM 优化

    IRAM 优化 IRAM 优化优化措施1 IRAM 优化方法 xff1a 2 第二个方法修改 ld 文件与配置文件 xff1a 测试 demo IRAM 优化 优化措施 1 IRAM 优化方法 xff1a 简单方法 xff0c 既可以优化
  • Aliyun 学习笔记(一)Aliyun 平台介绍

    文章目录 1 Aliyun 平台介绍2 各平台差异 1 Aliyun 平台介绍 首先我们查看 阿里云 官网 gt 产品分类 gt 物联网 xff0c 可以看到 阿里云 有关物联网的所有产品 如图 xff1a 同样查看 阿里云 官网 gt 文
  • NVIDIA TX2 Ubuntu 18.04 无法打开SD卡/U盘的解决方法

    刚烧录好的TX2开发板不能识别SD卡时 xff0c 键入以下命令可解 span class token function sudo span span class token function apt get span span class
  • BLE Mesh(一)基础介绍

    文章目录 Bluetooth Mesh Introduction1 Overview2 Topology3 Publish Subscribe4 Provisioning5 Addressing6 Encryption Bluetooth
  • Aliyun 学习笔记(二)阿里云物联网平台介绍

    文章目录 1 阿里云物联网平台1 1 设备接入1 2 设备管理1 3 安全能力1 4 规则引擎 1 阿里云物联网平台 根据阿里云物联网平台文档可以了解到所有有关阿里云物联网平台的介绍 阿里云物联网平台为设备提供安全可靠的连接通信能力 xff
  • BLE Mesh(零)学习资料

    文章目录 1 解密蓝牙 mesh 系列 1 解密蓝牙 mesh 系列 蓝牙mesh入门篇 概述了蓝牙mesh网络技术 xff0c 以及蓝牙mesh网络中消息传输的工作原理 xff0c 包括发布 xff08 Publish xff09 订阅
  • ESP32 学习笔记(三十) ESP32 性能分析,与 Cortex-M4 性能比较

    文章目录 关于 EEMBCESP32 8266 与 Cortex M3 M4 性能比较 关于 EEMBC About EEMBC EEMBC 为自动驾驶 xff0c 移动成像 xff0c 物联网 xff0c 移动设备和许多其他应用程序中使用
  • BLE Mesh(二)BLE Mesh 基础

    Bluetooth Mesh Basics 1 Mesh vs Point to Point2 Devices and Nodes3 Elements4 Messages5 Addresses6 Publish Subscribe7 Sta
  • BLE Mesh(三)BLE Mesh 系统架构

    BLE Mesh 系统架构 1 Overview2 Bearer Layer3 Network Layer4 Lower Transport Layer5 Upper Transport Layer6 Access Layer7 Found
  • BLE Mesh(四)BLE Mesh 安全

    BLE Mesh Security 1 Mesh Security is Mandatory2 Mesh Security Fundamentals3 Separation of Concerns and Mesh Security Key
  • BLE Mesh(五)BLE Mesh 工作原理

    Bluetooth Mesh in Action 1 Message Publication and Delivery2 Multipath Delivery3 Managed Flooding3 1 Heartbeats3 2 TTL3
  • [置顶] BLE Mesh 学习笔记目录

    BLE Mesh 学习笔记目录 1 BLE Mesh 基础 1 BLE Mesh 基础 BLE Mesh xff08 零 xff09 学习资料BLE Mesh xff08 一 xff09 基础介绍BLE Mesh xff08 二 xff09
  • BLE Mesh(六)配网流程

    配网流程 概述配网协议配网承载层 Provisioning Bearer 配网协议 Provisioning Protocol 流程详解发送Beacon信号邀请交换公共密钥认证输出带外 xff08 Output OOB xff09 输入带外
  • Ubuntu导入python包或其他包时卡顿缓慢

    遇到如题所述问题 xff0c 多半是pip版本太低需要升级 xff0c 键入以下命令可解 span class token function sudo span pip3 span class token function install
  • Matter(一)Matter(C.H.I.P) 介绍

    Matter C H I P 介绍 Matter C H I P 介绍Architecture Overview参考资料 Matter C H I P 介绍 What is Project Connected Home over IP Pr
  • docker 停止容器,删除容器

    停止容器 docker stop ID 删除容器 docker rm id
  • SpringCloud学习笔记-Eureka

    文章目录 服务发现eureka简介架构图使用方法创建Eureka Server引入依赖增加注解修改配置文件 创建Eureka Client引入依赖修改配置文件 Eureka Server高可用编写高可用Eureka Server 用户认证新
  • 《Java核心技术卷一》学习笔记(一)

    写在前面 xff1a 仅作记录 swing和awt部分可以不怎么看 xff0c 前五章都是基础知识 xff0c 有些部分可以熟练java后再看 xff0c 后面的章节可以选择性地看 xff0c 如 xff1a 想刷算法题 了解数据结构 xf
  • go语言项目结合k8s

    1 获取k8s的某个包时 xff0c 报如下错误 xff0c 则可以到idea设置GOPROXY参数 go k8s io apimachinery 64 v0 0 0 20190413052414 d7deff9243b1 unrecogn
  • OpenCV4中读取摄像头参数YAML文件(C++和C#)

    0 xff1a 环境 OpenCV4 1 2 43 Win10 43 Visual Studio2019 我的工程是C 的 xff0c 需要程序读取摄像头参数yaml中的摄像头参数矩阵 畸变矩阵 其实这些opencv都已经帮我们实现了 1