I realize this is an old question, but I think the current answers are somehow misleading.
Calling both at<T>(...)
and ptr<T>(...)
will check the boundaries in the debug mode. If the _DEBUG
macro is not defined, they will basically calculate y * width + x
and give you either the pointer to the data or the data itself. So using at<T>(...)
in release mode is equivalent to calculating the pointer yourself, but safer because calculating the pointer is not just y * width + x
if the matrix is just a sub-view of another matrix. In debug mode, you get the safety checks.
I think the best way is to process the image row-by-row, getting the row pointer using ptr<T>(y)
and then using p[x]
. This has the benefit that you don't have to count with various data layouts and still plain pointer for the inner loop.
You can use plain pointers all the way, which would be most efficient because you avoid one the multiplication per row, but then you need to use step1(i)
to advance the pointer. I think that using ptr<T>(y)
is a nice trade-off.