• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 989
  • Last Modified:

how to print to file

How do I write matrix values to a file to check it??

Thanks in advance WLE


//uchar lut[256];
//lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
//cvSetData( lut_mat, lut, 0 );   //set the matrix data to 0
//CvMat* lut_mat = cvCreateMat(1,256,CV_8UC1);
CvMat* lut_mat    = cvCreateMat(1,256,CV_8UC1);
int n             = lut_mat->cols;
int *data         = lut_mat->data.i;

for(int i = 0; i < 256; i++ )
{
if ((i>105)&&(i<117))   //fill ranges
{
    data[i] = i;
}
else
{
    //data[i] = 0;
}

}

    return true;
}

0
Wanderinglazyeye
Asked:
Wanderinglazyeye
1 Solution
 
Infinity08Commented:
>> How do I write matrix values to a file to check it??

What do you mean by "to check it" ? To check what ?

If you just want to see whether the values are filled in correctly, then you can use code like this :

    void printMatrixToFile(CvMat *lut_mat, char *fileName) {
        int rows = 0, cols = 0;
        int *data = 0;

        if (!lut_mat || !fileName) return;

        rows = lut_mat->rows;
        cols = lut_mat->cols;
        data = lut_mat->data.i;

        ofstream theFile;
        theFile.open(fileName);
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                theFile << data[(i * cols) + j] << "\t";
            }
            theFile << "\n";
        }
        theFile.close();

        return;
    }

and call it like this :

    printMatrixToFile(lut_mat, "theFile.txt");

More info on file input/output can be found in this tutorial :

        http://www.cplusplus.com/doc/tutorial/files.html
0
 
WanderinglazyeyeAuthor Commented:
Thanks Infinity08-

I condensed the code just to check matrix values, then move on. It works well. Posting just for posterirty.

CvMat* lut_mat    = cvCreateMat(1,256,CV_8UC1);
int m_cols          = lut_mat->cols;
int m_rows          = lut_mat->rows;
int *data           = lut_mat->data.i;



            for (int i = 0; i < m_rows; ++i)
        {
            for (int j = 0; j < m_cols; ++j)
                {
                    if ((j>105)&&(j<117))   //fill ranges
                        {
                            cvSetReal2D(lut_mat, i, j, j); // Set M(i,j)
                        }
                    else
                        {
                            cvSetReal2D(lut_mat, i, j, 0); // Set M(i,j)
                        }
                }
        }

        int n_rows = lut_mat->rows;

        int n_cols = lut_mat->cols;

        int *data2 = lut_mat->data.i;
         
ofstream theFile;


        theFile.open("matrixfile.txt");
        theFile << "Writing this to a file.\n";

        for (int i = 0; i < n_rows; ++i)
        {
            for (int j = 0; j < n_cols; ++j)
            {
               int L = cvGetReal2D(lut_mat, i, j); // Set M(i,j)

                theFile << L << "\t";
            }
            theFile << "\n";

        }
        theFile.close();
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now