如何基于matlab相机标定导出xml文件


Posted in Python onNovember 02, 2020

1 参数选择 径向畸变3个参数还是两个参数

默认两个参数

如何基于matlab相机标定导出xml文件

如果是三个参数

如何基于matlab相机标定导出xml文件

2准备转化生成结果

如何基于matlab相机标定导出xml文件

如何基于matlab相机标定导出xml文件

二参数的转化代码

writeExternalandIntrinsicMatrix(cameraParams62,'cameraParams622.xml');

如何基于matlab相机标定导出xml文件

function writeExternalandIntrinsicMatrix(cameraParams,file)
%writeXML(cameraParams,file)
 
docNode = com.mathworks.xml.XMLUtils.createDocument('opencv_storage');
docRootNode = docNode.getDocumentElement;
IntrinsicMatrix = ((cameraParams.IntrinsicMatrix)');
 
TangentialDistortion =cameraParams.TangentialDistortion;
%Distortion = [cameraParams.RadialDistortion(1:2),TangentialDistortion, cameraParams.RadialDistortion(3)];
Distortion = [cameraParams.RadialDistortion(1:2),TangentialDistortion,0];
FocalLength = cameraParams.FocalLength;
camera_matrix = docNode.createElement('IntrinsicCam'); %锟斤拷锟斤拷mat锟节碉拷
camera_matrix.setAttribute('type_id','opencv-matrix'); %锟斤拷锟斤拷mat锟节碉拷锟斤拷锟斤拷
rows = docNode.createElement('rows'); %锟斤拷锟斤拷锟叫节碉拷
rows.appendChild(docNode.createTextNode(sprintf('%d',3))); %锟斤拷锟斤拷锟侥憋拷锟节点,锟斤拷锟斤拷为锟叫碉拷锟接节碉拷
camera_matrix.appendChild(rows); %锟斤拷锟叫节碉拷锟斤拷为mat锟接节碉拷
 
cols = docNode.createElement('cols');
cols.appendChild(docNode.createTextNode(sprintf('%d',3)));
camera_matrix.appendChild(cols);
 
dt = docNode.createElement('dt');
dt.appendChild(docNode.createTextNode('d'));
camera_matrix.appendChild(dt);
 
data = docNode.createElement('data');
for i=1:3
  for j=1:3
    data.appendChild(docNode.createTextNode(sprintf('%.16f ',IntrinsicMatrix(i,j))));
  end
  data.appendChild(docNode.createTextNode(sprintf('\n')));
end
camera_matrix.appendChild(data);
docRootNode.appendChild(camera_matrix);
 
distortion = docNode.createElement('DistortionCam');
distortion.setAttribute('type_id','opencv-matrix');
rows = docNode.createElement('rows');
rows.appendChild(docNode.createTextNode(sprintf('%d',1)));
distortion.appendChild(rows);
 
cols = docNode.createElement('cols');
cols.appendChild(docNode.createTextNode(sprintf('%d',5)));
distortion.appendChild(cols);
 
dt = docNode.createElement('dt');
dt.appendChild(docNode.createTextNode('d'));
distortion.appendChild(dt);
data = docNode.createElement('data');
for i=1:5
   data.appendChild(docNode.createTextNode(sprintf('%.16f ',Distortion(i))));
end
distortion.appendChild(data);
docRootNode.appendChild(distortion);
 
 
focalLength = docNode.createElement('FocalLength');
focalLength.setAttribute('type_id','opencv-matrix');
rows = docNode.createElement('rows');
rows.appendChild(docNode.createTextNode(sprintf('%d',1)));
focalLength.appendChild(rows);
cols = docNode.createElement('cols');
cols.appendChild(docNode.createTextNode(sprintf('%d',1)));
focalLength.appendChild(cols);
dt = docNode.createElement('dt');
dt.appendChild(docNode.createTextNode('d'));
focalLength.appendChild(dt);
data = docNode.createElement('data');
for i=1:1
   data.appendChild(docNode.createTextNode(sprintf('%.16f ',FocalLength(i))));
end
focalLength.appendChild(data);
docRootNode.appendChild(focalLength);
 
% distortion = docNode.createElement('Pmatrix');
% distortion.setAttribute('type_id','opencv-matrix');
% rows = docNode.createElement('rows');
% rows.appendChild(docNode.createTextNode(sprintf('%d',1)));
% distortion.appendChild(rows);
%
% cols = docNode.createElement('cols');
% cols.appendChild(docNode.createTextNode(sprintf('%d',4)));
% distortion.appendChild(cols);
%
% dt = docNode.createElement('dt');
% dt.appendChild(docNode.createTextNode('d'));
% distortion.appendChild(dt);
% data = docNode.createElement('data');
% for i=1:4
%    data.appendChild(docNode.createTextNode(sprintf('%.16f ',Distortion(i))));
% end
% distortion.appendChild(data);
% docRootNode.appendChild(distortion);
 
 
xmlFileName = file;
xmlwrite(xmlFileName,docNode);
end

二参数的保存结果

<?xml version="1.0" encoding="utf-8"?>
<opencv_storage>
  <IntrinsicCam type_id="opencv-matrix">
   <rows>3</rows>
   <cols>3</cols>
   <dt>d</dt>
   <data>1558.5669994681102253 0.0000000000000000 821.5211092415044050
0.0000000000000000 1557.8077127262038175 460.9748043702705331
0.0000000000000000 0.0000000000000000 1.0000000000000000
</data>
  </IntrinsicCam>
  <DistortionCam type_id="opencv-matrix">
   <rows>1</rows>
   <cols>5</cols>
   <dt>d</dt>
   <data>-0.1873006682834817 0.0171597428423078 0.0000000000000000 0.0000000000000000 0.0000000000000000 </data>
  </DistortionCam>
  <FocalLength type_id="opencv-matrix">
   <rows>1</rows>
   <cols>1</cols>
   <dt>d</dt>
   <data>1558.5669994681102253 </data>
  </FocalLength>
</opencv_storage>

三参数的转化代码

function writeXML(cameraParams,file)
%writeXML(cameraParams,file)
%功能:将相机校正的参数保存为xml文件
%输入:
%cameraParams:相机校正数据结构
%file:xml文件名
%说明在xml文件是由一层层的节点组成的。
%首先创建父节点 fatherNode,
%然后创建子节点 childNode=docNode.createElement(childNodeName),
%再将子节点添加到父节点 fatherNode.appendChild(childNode)
docNode = com.mathworks.xml.XMLUtils.createDocument('opencv_storage'); %创建xml文件对象
docRootNode = docNode.getDocumentElement; %获取根节点
 
IntrinsicMatrix = (cameraParams.IntrinsicMatrix)'; %相机内参矩阵
RadialDistortion = cameraParams.RadialDistortion; %相机径向畸变参数向量1*3
TangentialDistortion =cameraParams.TangentialDistortion; %相机切向畸变向量1*2
  Distortion = [RadialDistortion(1:2),TangentialDistortion,RadialDistortion(3)]; %构成opencv中的畸变系数向量[k1,k2,p1,p2,k3]
 
camera_matrix = docNode.createElement('camera-matrix'); %创建mat节点
camera_matrix.setAttribute('type_id','opencv-matrix'); %设置mat节点属性
rows = docNode.createElement('rows'); %创建行节点
rows.appendChild(docNode.createTextNode(sprintf('%d',3))); %创建文本节点,并作为行的子节点
camera_matrix.appendChild(rows); %将行节点作为mat子节点
 
cols = docNode.createElement('cols');
cols.appendChild(docNode.createTextNode(sprintf('%d',3)));
camera_matrix.appendChild(cols);
 
dt = docNode.createElement('dt');
dt.appendChild(docNode.createTextNode('d'));
camera_matrix.appendChild(dt);
 
data = docNode.createElement('data');
for i=1:3
  for j=1:3
    data.appendChild(docNode.createTextNode(sprintf('%.16f ',IntrinsicMatrix(i,j))));
  end
  data.appendChild(docNode.createTextNode(sprintf('\n')));
end
camera_matrix.appendChild(data);
docRootNode.appendChild(camera_matrix);
 
distortion = docNode.createElement('distortion');
distortion.setAttribute('type_id','opencv-matrix');
rows = docNode.createElement('rows');
rows.appendChild(docNode.createTextNode(sprintf('%d',5)));
distortion.appendChild(rows);
 
cols = docNode.createElement('cols');
cols.appendChild(docNode.createTextNode(sprintf('%d',1)));
distortion.appendChild(cols);
 
dt = docNode.createElement('dt');
dt.appendChild(docNode.createTextNode('d'));
distortion.appendChild(dt);
data = docNode.createElement('data');
for i=1:5
   data.appendChild(docNode.createTextNode(sprintf('%.16f ',Distortion(i))));
end
distortion.appendChild(data);
 
docRootNode.appendChild(distortion);
 
xmlFileName = file;
xmlwrite(xmlFileName,docNode);
end

三参数的转化保存结果

<?xml version="1.0" encoding="utf-8"?>
<opencv_storage>
  <camera-matrix type_id="opencv-matrix">
   <rows>3</rows>
   <cols>3</cols>
   <dt>d</dt>
   <data>1558.6100144620272658 0.0000000000000000 821.6453269280840459
0.0000000000000000 1557.8120286433929778 460.8682816753835141
0.0000000000000000 0.0000000000000000 1.0000000000000000
</data>
  </camera-matrix>
  <distortion type_id="opencv-matrix">
   <rows>5</rows>
   <cols>1</cols>
   <dt>d</dt>
   <data>-0.1840928673709393 -0.0328189923757994 0.0000000000000000 0.0000000000000000 0.2205440258401062 </data>
  </distortion>
</opencv_storage>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
pycharm 使用心得(九)解决No Python interpreter selected的问题
Jun 06 Python
Python判断文件和文件夹是否存在的方法
May 21 Python
Python调用ctypes使用C函数printf的方法
Aug 23 Python
python实现读Excel写入.txt的方法
Apr 29 Python
一条命令解决mac版本python IDLE不能输入中文问题
May 15 Python
python判断输入日期为第几天的实例
Nov 13 Python
在PyCharm中实现关闭一个死循环程序的方法
Nov 29 Python
python实现图片压缩代码实例
Aug 12 Python
Python3 socket即时通讯脚本实现代码实例(threading多线程)
Jun 01 Python
Python 的 __str__ 和 __repr__ 方法对比
Sep 02 Python
anaconda升级sklearn版本的实现方法
Feb 22 Python
Python基础之字符串格式化详解
Apr 21 Python
PyCharm安装PyQt5及其工具(Qt Designer、PyUIC、PyRcc)的步骤详解
Nov 02 #Python
Python如何急速下载第三方库详解
Nov 02 #Python
关于python3.9安装wordcloud出错的问题及解决办法
Nov 02 #Python
一篇文章带你搞定Ubuntu中打开Pycharm总是卡顿崩溃
Nov 02 #Python
jupyter notebook 写代码自动补全的实现
Nov 02 #Python
Python QT组件库qtwidgets的使用
Nov 02 #Python
python利用opencv保存、播放视频
Nov 02 #Python
You might like
PHP中nowdoc和heredoc使用需要注意的一点
2014/03/21 PHP
ThinkPHP表单令牌错误的相关解决方法分析
2016/05/20 PHP
PHP的PDO事务与自动提交
2019/01/24 PHP
获取body标签的两种方法
2011/10/13 Javascript
jquery网页元素拖拽插件效果及实现
2013/08/05 Javascript
jQuery动画效果-slideUp slideDown上下滑动示例代码
2013/08/28 Javascript
2014年最火的Node.JS后端框架推荐
2014/10/27 Javascript
JSON相关知识汇总
2015/07/03 Javascript
JavaScript中实现键值对应的字典与哈希表结构的示例
2016/06/12 Javascript
AngularJS基础 ng-show 指令简单示例
2016/08/03 Javascript
JS中input表单隐藏域及其使用方法
2017/02/13 Javascript
Vue实现动态响应数据变化
2017/04/28 Javascript
浅谈ES6 模板字符串的具体使用方法
2017/11/07 Javascript
mockjs,json-server一起搭建前端通用的数据模拟框架教程
2017/12/18 Javascript
JavaScript数据结构与算法之二叉树遍历算法详解【先序、中序、后序】
2019/02/21 Javascript
Vue基于vuex、axios拦截器实现loading效果及axios的安装配置
2019/04/26 Javascript
vue webpack重写cookie路径的方法
2019/07/10 Javascript
layui复选框的全选与取消实现方法
2019/09/02 Javascript
vue实现网络图片瀑布流 + 下拉刷新 + 上拉加载更多(步骤详解)
2020/01/14 Javascript
JavaScript类的继承多种实现方法
2020/05/30 Javascript
Flask框架学习笔记(一)安装篇(windows安装与centos安装)
2014/06/25 Python
Python中特殊函数集锦
2015/07/27 Python
python的多重继承的理解
2017/08/06 Python
Python全局变量与局部变量区别及用法分析
2018/09/03 Python
tensorflow实现在函数中用tf.Print输出中间值
2020/01/21 Python
Python模拟伯努利试验和二项分布代码实例
2020/05/27 Python
材料工程专业毕业生求职信
2014/03/04 职场文书
大龄毕业生求职别忘职业规划
2014/03/11 职场文书
售后服务承诺书模板
2014/05/21 职场文书
暑期社会实践先进个人主要事迹
2014/05/22 职场文书
汽修专业自荐信
2014/07/07 职场文书
敬老月活动总结
2014/08/28 职场文书
2014办公室年度工作总结
2014/12/09 职场文书
百家讲坛观后感
2015/06/12 职场文书
详解RedisTemplate下Redis分布式锁引发的系列问题
2021/04/27 Redis
漫画《尖帽子的魔法工坊》宣布动画化
2022/04/06 日漫