Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to iterate through a column in reverse?

Or, equivalently, why does the following code crash (on my Ubuntu 12.04 LTS box running OpenCV 2.4.5)

#include <iostream>
#include <opencv2/core/core.hpp>

using namespace cv;
using namespace std;

int
main(int argc, char *argv[])
{
  Mat m = (Mat_<int>(3,3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
  cout << m << endl;

  MatConstIterator_<int> Mptr = m.col(0).end<int>() - 1;
  for (int i = 0; i < 3; i++) {
    cout << (int)*Mptr-- << endl;
  }
  return(0);
}

I feel like this should work, but it crashes on the second iteration through the loop. I'm not completely comfortable with C++ iterators, and don't understand the nuances of begin()/end() vs rbegin()/rend(), but, as there is no rbegin()/rend() defined for OpenCV matrices, I guess that doesn't matter. Can somebody tell me what I'm missing here?

What is the correct way to iterate through a column of data in reverse order?

Thanks.

--wpd