SoFunction
Updated on 2024-11-19

How to export xml file based on matlab camera calibration

1 Parameter selection Radial distortion 3 or 2 parameters

Default two parameters

If three parameters

2 Preparing for conversion generates results

Transformation code for two parameters

writeExternalandIntrinsicMatrix(cameraParams62,'');

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

Saving results for two parameters

<?xml version="1.0" encoding="utf-8"?>
<opencv_storage>
  <IntrinsicCam type_>
   <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_>
   <rows>1</rows>
   <cols>5</cols>
   <dt>d</dt>
   <data>-0.1873006682834817 0.0171597428423078 0.0000000000000000 0.0000000000000000 0.0000000000000000 </data>
  </DistortionCam>
  <FocalLength type_>
   <rows>1</rows>
   <cols>1</cols>
   <dt>d</dt>
   <data>1558.5669994681102253 </data>
  </FocalLength>
</opencv_storage>

Transformation code for three parameters

function writeXML(cameraParams,file)
%writeXML(cameraParams,file)
%functionality:Save the camera calibration parameters asxmlfile
%importation:
%cameraParams:Camera calibration data structure
%file:xmlfile名
%notesxmlfile是由一层层的nodal组成的。
%First create the parent node fatherNode,
%Then create child nodes childNode=(childNodeName),
%Add the child node to the parent again (childNode)
docNode = ('opencv_storage'); %establishxmlfile对象
docRootNode = ; %Get root node
 
IntrinsicMatrix = ()'; %Camera Internal Reference Matrix
RadialDistortion = ; %Camera radial distortion parameter vector1*3
TangentialDistortion =; %Camera tangential distortion vector1*2
  Distortion = [RadialDistortion(1:2),TangentialDistortion,RadialDistortion(3)]; %composeopencvThe vector of distortion coefficients in[k1,k2,p1,p2,k3]
 
camera_matrix = ('camera-matrix'); %establishmatnodal
camera_matrix.setAttribute('type_id','opencv-matrix'); %set upmatnodal属性
rows = ('rows'); %establish行nodal
((sprintf('%d',3))); %establish文本nodal,并作为行的子nodal
camera_matrix.appendChild(rows); %将行nodal作为mat子nodal
 
cols = ('cols');
((sprintf('%d',3)));
camera_matrix.appendChild(cols);
 
dt = ('dt');
(('d'));
camera_matrix.appendChild(dt);
 
data = ('data');
for i=1:3
  for j=1:3
    ((sprintf('%.16f ',IntrinsicMatrix(i,j))));
  end
  ((sprintf('\n')));
end
camera_matrix.appendChild(data);
(camera_matrix);
 
distortion = ('distortion');
('type_id','opencv-matrix');
rows = ('rows');
((sprintf('%d',5)));
(rows);
 
cols = ('cols');
((sprintf('%d',1)));
(cols);
 
dt = ('dt');
(('d'));
(dt);
data = ('data');
for i=1:5
   ((sprintf('%.16f ',Distortion(i))));
end
(data);
 
(distortion);
 
xmlFileName = file;
xmlwrite(xmlFileName,docNode);
end

Transformation preservation results for three parameters

<?xml version="1.0" encoding="utf-8"?>
<opencv_storage>
  <camera-matrix type_>
   <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_>
   <rows>5</rows>
   <cols>1</cols>
   <dt>d</dt>
   <data>-0.1840928673709393 -0.0328189923757994 0.0000000000000000 0.0000000000000000 0.2205440258401062 </data>
  </distortion>
</opencv_storage>

This is the whole content of this article.