如何基于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 相关文章推荐
python代码制作configure文件示例
Jul 28 Python
python访问mysql数据库的实现方法(2则示例)
Jan 06 Python
以一个投票程序的实例来讲解Python的Django框架使用
Feb 18 Python
独特的python循环语句
Nov 20 Python
Python实现的双色球生成功能示例
Dec 18 Python
pytorch + visdom 处理简单分类问题的示例
Jun 04 Python
使用sklearn之LabelEncoder将Label标准化的方法
Jul 11 Python
python编辑用户登入界面的实现代码
Jul 16 Python
python RabbitMQ 使用详细介绍(小结)
Nov 08 Python
神经网络相关之基础概念的讲解
Dec 29 Python
Python创建自己的加密货币的示例
Mar 01 Python
python上下文管理的使用场景实例讲解
Mar 03 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
首页四格,首页五格For6.0(GBK)(UTF-8)[12种组合][9-18][版主安装测试通过]
2007/09/24 PHP
PHP使用array_fill定义多维数组的方法
2015/03/18 PHP
PHP编写daemon process详解及实例代码
2016/09/30 PHP
PHP简单实现模拟登陆功能示例
2017/09/15 PHP
用javascript实现兼容IE7的类库 IE7_0_9.zip提供下载
2007/08/08 Javascript
javascript 点击整页变灰的效果(可做退出效果)。
2008/01/09 Javascript
JavaScript性能优化 创建文档碎片(document.createDocumentFragment)
2010/07/13 Javascript
fancybox modal的完美解决(右上的X)
2012/10/30 Javascript
javascript实现youku的视频代码自适应宽度
2015/05/25 Javascript
javascript设计模式--策略模式之输入验证
2015/11/27 Javascript
node网页分段渲染详解
2016/09/05 Javascript
BootStrap 下拉菜单点击之后不会出现下拉菜单(下拉菜单不弹出)的解决方案
2016/12/14 Javascript
Vue 2.x教程之基础API
2017/03/06 Javascript
angular4自定义组件非input元素实现ngModel双向数据绑定的方法
2018/12/28 Javascript
JS中使用react-tooltip插件实现鼠标悬浮显示框
2019/05/15 Javascript
python翻译软件实现代码(使用google api完成)
2013/11/26 Python
python中PIL安装简单教程
2016/04/21 Python
Python实现螺旋矩阵的填充算法示例
2017/12/28 Python
python判断一个集合是否为另一个集合的子集方法
2018/05/04 Python
python numpy和list查询其中某个数的个数及定位方法
2018/06/27 Python
用python实现k近邻算法的示例代码
2018/09/06 Python
python+os根据文件名自动生成文本
2019/03/21 Python
在python中实现同行输入/接收多个数据的示例
2019/07/20 Python
用Python去除图像的黑色或白色背景实例
2019/12/12 Python
Html5之svg可缩放矢量图形_动力节点Java学院整理
2017/07/17 HTML / CSS
html5 Canvas画图教程(11)—使用lineTo/arc/bezierCurveTo画椭圆形
2013/01/09 HTML / CSS
canvas如何实现多张图片编辑的图片编辑器
2020/03/10 HTML / CSS
项目专员岗位职责
2013/12/04 职场文书
电大毕业自我鉴定
2014/02/03 职场文书
家庭贫困证明书(3篇)
2014/09/15 职场文书
认错检讨书
2014/10/02 职场文书
环保建议书作文400字
2015/09/14 职场文书
2016年教师反腐倡廉心得体会
2016/01/13 职场文书
党员理论学习心得体会
2016/01/21 职场文书
工作建议书范文
2019/07/08 职场文书
浅谈Python响应式类库RxPy
2021/06/14 Python