I understand that this is an old question, but I think that the current answers are somehow misleading.
A call to both at<T>(...) and ptr<T>(...) will check the bounds in debug mode. If the _DEBUG macro _DEBUG not defined, they will basically compute y * width + x and give you either a pointer to the data or the data itself. Thus, 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-representation of another matrix. In debug mode, you get security checks.
I think the best way is to process the image line by line, get the line pointer using ptr<T>(y) , and then using p[x] . This has the advantage that you do not need to calculate with various data layouts and there is still a simple pointer to the inner loop.
You can use simple pointers completely, which would be most efficient because you avoid multiplying by a string, but then you need to use step1(i) to move the pointer. I think using ptr<T>(y) is a good compromise.
source share