欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-13 21:59:58
1 参数选择 径向畸变3个参数还是两个参数默认两个参数如果是三个参数2准备转化生成结果二参数的转化代码writeexternalandintrinsicmatrix(cameraparams62,'c...

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>

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

上一篇: ORACLE spool打印

下一篇: