2013-05-26 194 views
1

这里是我的代码看起来像:如何打印CvMat矩阵的结果?

// 10 rows and 2 cols matrix. 
CvMat* results = cvCreateMat(10, 2, CV_32FC1); 

// Some operations ... 

ann->predict(samples, results); 

// How to print out the **results** ? 

有没有C++的API呢?

回答

1
std::cout << results <<"\n"; 

额外的好处是能打印的格式,可以直接复制到MATLAB

2

你好这里是我的用于观看里面的CvMat中的数据。

void printMat(CvMat* mat, char* filename) 
{ 
    FILE *pf =fopen(filename,"w"); 
    fprintf(pf,"(%dx%d)\n",mat->cols,mat->rows); 
    for(int i=0; i<mat->rows; i++) 
    { 
     if(i==0) 
     { 
      for(int j=0; j<mat->cols; j++) fprintf(pf,"%10d",j+1); 
     } 

     fprintf(pf,"\n%4d: ",i+1); 
     for(int j=0; j<mat->cols; j++) 
     { 

      fprintf(pf,"%10.2f",cvGet2D(mat,i,j).val[0]); 
     } 
    } 
    fflush(pf); 
    fclose(pf); 
} 
2

什么工作是std::cout << cv::Mat(results) << '\n';

这是因为cv::Mat可以constructedCvMatcv::Mat有运营商<<overloaded

0

在OpenCV 3中,cv::Mat(results)不再工作(至少从OpenCV 3.2开始,我使用的版本)。

相反,你应该使用cvarrToMat

std::cout << cv::cvarrToMat(results) << '\n';