OPENCV and MATLAB pass data through XML

Source: Internet
Author: User

Because most of the datasets that are now down are calibrated using MATLAB's calibration toolbox, Most of the structure of the. MAT structure, so it and OPENCV data transfer needs an intermediate process, the network also has a direct call to Matlab DLL method, but the middleware approach is bound to be limited by the version, so I finally chose to use XML as an intermediate file to pass the data.


--------------1.calibration Toolbox original calibrated. M Data --------------  Http://ishare.iask.sina.com.cn/f/67004587.html  -------------- 2. xml file written in MATLAB according to OPENCV storage format --------------Http://ishare.iask.sina.com.cn/f/67004482.html   -------------- 3.matlab End Conversion Code --------------  function WriteXML (name,fc,kc,alpha_c,cc,om,t)%writexml Write mat to XMLFile% This function isused to convert the matrix information from Matlab to% OpenCVKK=[FC (1) ALPHA_C*FC (1) cc (1); 0 FC (2) CC (2); 0 0 1]; %write FCxdoc=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:mFor j=1:ndata.a (Xdoc.createtextnode (sprintf ('%d ', FC (I,J) ));data.a (Xdoc.createtextnode (sprintf ('%s ', ') ));EndEndtype.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:mFor j=1:ndata.a (Xdoc.createtextnode (sprintf ('%d ', KC (i,j) ));data.a (Xdoc.createtextnode (sprintf ('%s ', ') ));EndEndtype.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:mFor j=1:ndata.a (Xdoc.createtextnode (sprintf ('%d ', Alpha_c (i,j) ));data.a (Xdoc.createtextnode (sprintf ('%s ', ') ));EndEndtype.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:mFor j=1:ndata.a (Xdoc.createtextnode (sprintf ('%d ', CC (I,J) ));data.a (Xdoc.createtextnode (sprintf ('%s ', ') ));EndEndtype.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:mFor j=1:ndata.a (Xdoc.createtextnode (sprintf ('%d ', KK (I,J) ));data.a (Xdoc.createtextnode (sprintf ('%s ', ') ));EndEndtype.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:mFor j=1:ndata.a (Xdoc.createtextnode (sprintf ('%d ', OM (i,j )));data.a (Xdoc.createtextnode (sprintf ('%s ', ') ));EndEndtype.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:mFor j=1:ndata.a (Xdoc.createtextnode (sprintf ('%d ', T (i,j) ));data.a (Xdoc.createtextnode (sprintf ('%s ', ') ));EndEndtype.a (data);   str=strcat (Name, '. xml ');Xmlwrite (str,xdoc);End  --------------4.c++ Reading code --------------  #include "highgui.h"#include "cv.h"#include#include#includeusing namespace std;using namespace CV;int main (void){//*********read xmlexample*********//1.cvfilestorage Write Xmlexampleint 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, "MyData", 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 ToXMLCvmat*writemat=cvcreatemat (3,3,CV_64FC1);cvsetidentity (Writemat);cvsave ("Test.xml", Writemat);  //*********read xmlexample*********//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 datasetexamplefilestorage FSS;Fss.open ("Cam1.xml", filestorage::read);matfc,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; }through the above process, you can transfer data in the middle of MATLAB and OpenCV, it seems very troublesome, but the actual use of the module is very convenient, especially when there are a large number of matrices need to be passed, in the transmission using a mat read method, of course, When the matrix to be passed is not many, the method of merging matrix, using Cvload to read and then partition the required data, should be more efficient, but it needs the matrix structure known to pass.from:http://blog.sina.com.cn/s/blog_5e3213f30101hdt6.html

OPENCV and MATLAB pass data through XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.