OpenCV和Matlab 通过XML传递数据

我是研究僧i 提交于 2020-02-05 17:59:44

因为现在下到的数据集大部分都使用了Matlab的Calibration toolbox 进行标定,其结构大部分是.mat结构的,所以它和opencv中数据传递需要一个中间过程,网上也有直接调用matlab的dll的方法,但是中间件方法必然会受到版本限制,所以我最后还是选择了使用xml来作为中间文件传递数据。


--------------1.calibration toolbox原始标定的.m数据--------------
 
http://ishare.iask.sina.com.cn/f/67004587.html
 
--------------2.通过matlab根据opencv的存储格式写成的xml文件--------------
http://ishare.iask.sina.com.cn/f/67004482.html
 
 
--------------3.matlab端转换代码--------------
 
function  writeXML( name,fc,kc,alpha_c,cc,om,t )
%WRITEXML write mat to xml file
%   this function is used to convert matrix information from matlab to
%   opencv
kk=[fc(1) alpha_c*fc(1) cc(1); 0 fc(2) cc(2);0 0 1];
 
%write FC
xdoc=com.mathworks.xml.XMLUtils.createDocument('opencv_storage');
xroot=xdoc.getDocumentElement;
[m,n]=size(fc);
 
type=xdoc.createElement_x_x_x_x_x('fc');
type.setAttribute('type_id','opencv-matrix')
xroot.a(type);
 
rows=xdoc.createElement_x_x_x_x_x('rows');
rows.a(xdoc.createTextNode(sprintf('%d',m)));
type.a(rows);
 
cols=xdoc.createElement_x_x_x_x_x('cols');
cols.a(xdoc.createTextNode(sprintf('%d',n)));
type.a(cols);
 
dt=xdoc.createElement_x_x_x_x_x('dt');
dt.a(xdoc.createTextNode(sprintf('%s','d')));
 
type.a(dt);
data=xdoc.createElement_x_x_x_x_x('data');
for i=1:m
    for j=1:n
        data.a(xdoc.createTextNode(sprintf('%d',fc(i,j))));
         data.a(xdoc.createTextNode(sprintf('%s',' ')));
    end
end
type.a(data);
 
%write KC
[m,n]=size(kc);
type=xdoc.createElement_x_x_x_x_x('kc');
type.setAttribute('type_id','opencv-matrix')
xroot.a(type);
 
rows=xdoc.createElement_x_x_x_x_x('rows');
rows.a(xdoc.createTextNode(sprintf('%d',m)));
type.a(rows);
 
cols=xdoc.createElement_x_x_x_x_x('cols');
cols.a(xdoc.createTextNode(sprintf('%d',n)));
type.a(cols);
 
dt=xdoc.createElement_x_x_x_x_x('dt');
dt.a(xdoc.createTextNode(sprintf('%s','d')));
 
type.a(dt);
data=xdoc.createElement_x_x_x_x_x('data');
for i=1:m
    for j=1:n
        data.a(xdoc.createTextNode(sprintf('%d',kc(i,j))));
         data.a(xdoc.createTextNode(sprintf('%s',' ')));
    end
end
type.a(data);
 
 
%write ALPHA_C
[m,n]=size(alpha_c);
type=xdoc.createElement_x_x_x_x_x('alpha_c');
type.setAttribute('type_id','opencv-matrix')
xroot.a(type);
 
rows=xdoc.createElement_x_x_x_x_x('rows');
rows.a(xdoc.createTextNode(sprintf('%d',m)));
type.a(rows);
 
cols=xdoc.createElement_x_x_x_x_x('cols');
cols.a(xdoc.createTextNode(sprintf('%d',n)));
type.a(cols);
 
dt=xdoc.createElement_x_x_x_x_x('dt');
dt.a(xdoc.createTextNode(sprintf('%s','d')));
 
type.a(dt);
data=xdoc.createElement_x_x_x_x_x('data');
for i=1:m
    for j=1:n
        data.a(xdoc.createTextNode(sprintf('%d',alpha_c(i,j))));
         data.a(xdoc.createTextNode(sprintf('%s',' ')));
    end
end
type.a(data);
 
 
%write CC
[m,n]=size(cc);
type=xdoc.createElement_x_x_x_x_x('cc');
type.setAttribute('type_id','opencv-matrix')
xroot.a(type);
 
rows=xdoc.createElement_x_x_x_x_x('rows');
rows.a(xdoc.createTextNode(sprintf('%d',m)));
type.a(rows);
 
cols=xdoc.createElement_x_x_x_x_x('cols');
cols.a(xdoc.createTextNode(sprintf('%d',n)));
type.a(cols);
 
dt=xdoc.createElement_x_x_x_x_x('dt');
dt.a(xdoc.createTextNode(sprintf('%s','d')));
 
type.a(dt);
data=xdoc.createElement_x_x_x_x_x('data');
for i=1:m
    for j=1:n
        data.a(xdoc.createTextNode(sprintf('%d',cc(i,j))));
         data.a(xdoc.createTextNode(sprintf('%s',' ')));
    end
end
type.a(data);
 
%write KK
[m,n]=size(kk);
type=xdoc.createElement_x_x_x_x_x('kk');
type.setAttribute('type_id','opencv-matrix')
xroot.a(type);
 
rows=xdoc.createElement_x_x_x_x_x('rows');
rows.a(xdoc.createTextNode(sprintf('%d',m)));
type.a(rows);
 
cols=xdoc.createElement_x_x_x_x_x('cols');
cols.a(xdoc.createTextNode(sprintf('%d',n)));
type.a(cols);
 
dt=xdoc.createElement_x_x_x_x_x('dt');
dt.a(xdoc.createTextNode(sprintf('%s','d')));
 
type.a(dt);
data=xdoc.createElement_x_x_x_x_x('data');
for i=1:m
    for j=1:n
        data.a(xdoc.createTextNode(sprintf('%d',kk(i,j))));
         data.a(xdoc.createTextNode(sprintf('%s',' ')));
    end
end
type.a(data);
 
 
%write OM
[m,n]=size(om);
type=xdoc.createElement_x_x_x_x_x('om');
type.setAttribute('type_id','opencv-matrix')
xroot.a(type);
 
rows=xdoc.createElement_x_x_x_x_x('rows');
rows.a(xdoc.createTextNode(sprintf('%d',m)));
type.a(rows);
 
cols=xdoc.createElement_x_x_x_x_x('cols');
cols.a(xdoc.createTextNode(sprintf('%d',n)));
type.a(cols);
 
dt=xdoc.createElement_x_x_x_x_x('dt');
dt.a(xdoc.createTextNode(sprintf('%s','d')));
 
type.a(dt);
data=xdoc.createElement_x_x_x_x_x('data');
for i=1:m
    for j=1:n
        data.a(xdoc.createTextNode(sprintf('%d',om(i,j))));
         data.a(xdoc.createTextNode(sprintf('%s',' ')));
    end
end
type.a(data);
 
%write T
[m,n]=size(t);
type=xdoc.createElement_x_x_x_x_x('t');
type.setAttribute('type_id','opencv-matrix')
xroot.a(type);
 
rows=xdoc.createElement_x_x_x_x_x('rows');
rows.a(xdoc.createTextNode(sprintf('%d',m)));
type.a(rows);
 
cols=xdoc.createElement_x_x_x_x_x('cols');
cols.a(xdoc.createTextNode(sprintf('%d',n)));
type.a(cols);
 
dt=xdoc.createElement_x_x_x_x_x('dt');
dt.a(xdoc.createTextNode(sprintf('%s','d')));
 
type.a(dt);
data=xdoc.createElement_x_x_x_x_x('data');
for i=1:m
    for j=1:n
        data.a(xdoc.createTextNode(sprintf('%d',t(i,j))));
         data.a(xdoc.createTextNode(sprintf('%s',' ')));
    end
end
type.a(data);
 
 
 
str=strcat(name,'.xml');
xmlwrite(str,xdoc);
end
 
--------------4.C++读取代码--------------
 
 
#include"highgui.h"
#include"cv.h"
#include
#include
#include
using namespace  std;
using namespace cv;
int main(void)
{
//*********Read XML Example*********
//1.cvFileStorage write XML Example
int a=1;
float b=2;
double c[]={3,5,6,6};
CvMat *mat=cvCreateMat(3,3,CV_32SC1);
cvSetIdentity(mat);
CvFileStorage *fs=cvOpenFileStorage("test1.xml",0,CV_STORAGE_WRITE);
cvWriteComment(fs,"my data",1);
cvStartWriteStruct(fs,"DATA",CV_NODE_MAP,0,cvAttrList(0,0));
 
cvStartWriteStruct(fs,"c",CV_NODE_SEQ,0,cvAttrList(0,0));
cvWriteRawData(fs,c,4,"d");
cvEndWriteStruct(fs);
cvSave("mat.xml",mat);
cvWriteInt(fs,"a",a);
cvWriteReal(fs,"b",b);
cvStartWriteStruct(fs,"c",CV_NODE_SEQ,0,cvAttrList(0,0));
cvWriteRawData(fs,c,4,"d");
cvEndWriteStruct(fs);
cvReleaseFileStorage(&fs);
cvReleaseMat(&mat);
 
 
//2.Mat to XML 
CvMat *writemat=cvCreateMat(3,3,CV_64FC1);
cvSetIdentity(writemat);
cvSave("test.xml",writemat);
 
 
//*********Read XML Example*********
    //1. Direct read sigle CvMat Example
 
CvMat *testmat=(CvMat *)cvLoad("test.xml");
for(int i=0;irows;i++)
{
for(int j=0;jcols;j++)
cout<<CV_MAT_ELEM(*testmat,double,i,j)<<" ";
cout<<endl;
 
}
system("PAUSE");
//2.File Storage multi dataset Example
FileStorage fss;
fss.open("cam1.xml",FileStorage::READ);
Mat fc,cc,kc,alpha_c,kk,om,t;
fss["fc"]>>fc;
fss["cc"]>>cc;
fss["kc"]>>kc;
fss["alpha_c"]>>alpha_c;
fss["kk"]>>kk;
fss["om"]>>om;
fss["t"]>>t;
cout<<"fc:"<<endl<<fc<<endl;
cout<<"cc:"<<endl<<cc<<endl;
cout<<"kc:"<<endl<<kc<<endl;
cout<<"alpha_c:"<<endl<<alpha_c<<endl;
cout<<"kk:"<<endl<<kk<<endl;
cout<<"om:"<<endl<<om<<endl;
cout<<"t:"<<endl<<t<<endl;
fss.release();
system("PAUSE");
return 0;
 
}
通过上面的过程,就可以在matlab和opencv中间传递数据了,看似很麻烦,但是实际使用 上经过模块化是非常方便的,尤其是有大量的矩阵需要传递的时候,在传递中采用了逐个mat读取的方法,当然,要传递的矩阵不多的时候,可以采用合并矩阵, 用cvload读取后再分割出所需数据的方法,这种方法的效率应该更高,但是需要已知传递的矩阵结构
 
from: http://blog.sina.com.cn/s/blog_5e3213f30101hdt6.html
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!