C + + version of OPENCV and MATLAB matrix data passed through TXT file

Source: Internet
Author: User

Recently need to work with MATLAB and C + +, in order to circumvent the code from MATLAB to C + +, can only convert data. (I also translated the code, found that MATLAB for the matrix calculation is more convenient and fast)

The storage of data in Opencv seems like only files in specific formats such as XML, YML, and Matlab is less likely to read such files. So, I refer to some online methods, wrote a generic version.

including two parts, OpenCV write, matlab read with matlab write, OpenCV read. This is pediatrics, simple record, later directly to use can.

Note: For convenience, TXT is a column, the first two rows are the number of rows and columns.

1.OPENCV write, Matlab read

//@brief save Mat to txt files//The Mat type must be doubleintMat2txt (Mat D,stringtxt_name)    {D.convertto (D, CV_64FC1);    Ofstream Fout; Fout.open ("Txt_name"); Fout<< d.rows <<Endl; Fout<< D.cols <<Endl;  for(inti =0; i < d.rows; i++)    {         for(intj =0; J < D.cols; J + +) {Fout<< d.ptr<double> (i) [j] <<Endl; }} fout<<Flush;    Fout.close (); return 0;}
%the txt must is one col%and the first and second num must be the rows and Colsfunction[data]=txt2mat (txt_name) Txt_data=im Portdata (Txt_name); R=txt_data (1); C=txt_data (2);d ata=txt_data (3:end);d Ata=reshape (data,[c,r]) '; end

2.matlab Write, OpenCV read

%the txt must be one colfunction mat2txt (txt_name) fid=fopen (txt_name, ' wt '); [R,c]=size (A); fprintf (FID, '%f\n ', r); fprintf (FID, '%f\n ', c); for i=1:1:r for    j=1:1:c        fprintf (FID, '%f\n ', A (i , j));    Endendfclose (FID); end

  

//@brief string to num (int,double ...)//#include <sstream>Template <classType>Type Stringtonum (Const string&str)    {istringstream iss (str);    Type num; ISS>>num; returnnum;}//@brief Get the txt file from Matlab//The data saved in TXT must is one colMat Txt2mat (stringtxt_name)    {ifstream fin;    Fin.open (Txt_name); if(!Fin.is_open ()) {cout<<"The file can not be opened!"<<Endl; }    stringtemp;    Mat A; intline =0; intR =0; intc =0;  while(getline (FIN, temp)) { line++; if(line = =1) {R= stringtonum<int>(temp); }        Else        {            if(line = =2) C= stringtonum<int>(temp); Else{a.push_back (stringtonum<Double>(temp));    }}} fin.close (); Opencv_assert (A.rows= = R*c,"Testcverror","The size doesnt match!"); return(A.reshape (0, R));}

C + + version of OPENCV and MATLAB matrix data passed through TXT file

Related Article

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.