matlab导出相机标定数据

由于matlab标定数据比opencv的标定准一些,有时候会将matlab的标定参数导入自己程序中应用。

导出matlab标定参数

function writeXML(cameraParams,file)
%writeXML(cameraParams,file)
%功能:将相机校正的参数保存为xml文件
%输入:
%cameraParams:相机校正数据结构
%file:xml文件名
    xml = com.mathworks.xml.XMLUtils.createDocument('opencv_storage'); %创建xml文件对象
    root = xml.getDocumentElement; %获取根节点

    node_Intrinsic = createNode(xml, cameraParams.IntrinsicMatrix, 'camera-matrix');
    root.appendChild(node_Intrinsic);
    
    node_Translation = createNode(xml, cameraParams.TranslationVectors, 'translation');
    root.appendChild(node_Translation);
    
    node_Rotation = createNode(xml, cameraParams.RotationMatrices, 'rotation');
    root.appendChild(node_Rotation);
    
    xmlFileName = file;
    xmlwrite(xmlFileName,xml);
end


function [node] = createNode(xml, M, name)
    [H, W, C] = size(M);
    node = xml.createElement(name); %创建mat节点
    node.setAttribute('type_id','opencv-matrix'); %设置mat节点属性
    rows = xml.createElement('rows'); %创建行节点
    rows.appendChild(xml.createTextNode(sprintf('%d',H))); %创建文本节点,并作为行的子节点
    node.appendChild(rows); %将行节点作为mat子节点

    cols = xml.createElement('cols');
    cols.appendChild(xml.createTextNode(sprintf('%d',W)));
    node.appendChild(cols);

    dt = xml.createElement('dt');
    dt.appendChild(xml.createTextNode(sprintf('"%dd"',C)));
    node.appendChild(dt);

    data = xml.createElement('data');
    if C == 1 % 二维矩阵
        for i=1:H
            for j=1:W
                data.appendChild(xml.createTextNode(sprintf('%.16f ',M(i,j))));
            end
            data.appendChild(xml.createTextNode(sprintf('\n')));
        end
    else % 三维矩阵
        for i=1:H
            for j=1:W
                for k = 1:C
                    data.appendChild(xml.createTextNode(sprintf('%.16f ',M(i,j,k))));
                end
                data.appendChild(xml.createTextNode(sprintf('\n')));
            end
            data.appendChild(xml.createTextNode(sprintf('\n\n')));
        end
    end
    node.appendChild(data);
end

python中导入标定参数

file = cv.FileStorage(file_path, cv.FileStorage_READ)
mtx = file.getNode('camera-matrix').mat().T
Tmat = file.getNode('translation').mat()
Rmat = file.getNode('rotation').mat()
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值