WoooHaaaa
WoooHaaaa

Reputation: 20450

How print the result of CvMat matrix?

Here's how my code looks like:

// 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** ?

Is there any C++ API for this ?

Upvotes: 1

Views: 3817

Answers (4)

Catree
Catree

Reputation: 2517

In OpenCV 3, cv::Mat(results) does not work anymore (at least since OpenCV 3.2, the version I used).

Instead, you should use cvarrToMat:

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

Upvotes: 0

kiranpradeep
kiranpradeep

Reputation: 11201

What works is std::cout << cv::Mat(results) << '\n';

This is since cv::Mat can be constructed from CvMat and cv::Mat has operator << overloaded.

Upvotes: 2

sapeyes
sapeyes

Reputation: 21

Hi here is mine to be used for viewing inside data of 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);
}

Upvotes: 2

Martin Beckett
Martin Beckett

Reputation: 96109

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

As an extra bonus this prints in a format that can be copied straight into matlab

Upvotes: 1

Related Questions