1

I am using C++. I want to print the matrix of a Mat object on different lines like this:

http://i.imgur.com/1FsNmah.png

What I have right now is

cout << m <<endl <<endl; 

Can anyone help with this please?

wimh
  • 15,072
  • 6
  • 47
  • 98
Steph
  • 609
  • 3
  • 13
  • 32
  • sorry i dont know why the last line is not appearing. It's cout < – Steph Feb 01 '14 at 11:28
  • I fixed the formatting, but can you explain what m or matrix_f is (declaration)? – wimh Feb 01 '14 at 11:30
  • sorry I forgot to mention. "matrix_f" is the name of the Mat object(Mat matrix_f;) – Steph Feb 01 '14 at 11:40
  • Do you have just one Mat object and want to print it out, or do you have multiple, and they need to be printed out like in the checker-board pattern you showed in the question? – Anton Poznyakovskiy Feb 01 '14 at 11:43
  • i will have 3 Mat matrices which will need to be printed as in the picture in command line – Steph Feb 01 '14 at 11:44

2 Answers2

3

In this case (referring to the comments) you need to print your matrices row-wise. Make use of m.row() and m.at(). Supposed that you have 3x3 matrices as in the image:

for (int i = 0; i < 3; ++i)
{
    Mat row1 = m1.row (i);
    Mat row2 = m2.row (i);
    Mat row3 = m3.row (i);

    // this can be replaced by a loop, I spell it out for the sake of clearness
    cout << row1.at(0, 0) << " " << row1.at (0, 1) << " " << row1.at (0, 2) << "\t"
         << row2.at(0, 0) << " " << row2.at (0, 1) << " " << row2.at (0, 2) << "\t"
         << row3.at(0, 0) << " " << row3.at (0, 1) << " " << row3.at (0, 2) << endl;

}
Anton Poznyakovskiy
  • 2,109
  • 1
  • 20
  • 38
2

If you are looking to print it to command line, i suggest looking at OpenCV: Matrix Iteration

If you are doing this quite frequently, although not very recommended, you can derive your own Matrix class and override the << operator to print it the way you want it to (as in the link above)

Community
  • 1
  • 1
avihayas
  • 281
  • 2
  • 10