While your answer is correct for your situation, it's good to know what step
and AUTO_STEP
mean
Usually, images are stored in continuous blocks of memory. Each row follows after the previous one, so you can access the data by pointer with a simple
data = dataPtr[currentCol + width * currentRow];
Where width is the row width in bytes (not in pixels!)
But this is not always the case - sometimes you access a submatrix, and the data is continuous on each row, but to go the next row you have to jump a sequence of bytes.
This is where the step (also known as stride) comes in. It represents the distance, in bytes, between two consecutive rows. In continuous matrices, its value is sizeof(pixel)*rowWidth
, but it may have custom values in special situations. When you pass AUTO_STEP to the Mat constructor, it knows that the data is continuous and calculates the step with the above formula. So now, the more correct approach to reading pixel values is
data = dataPtr[currentCol + step * currentRow];
Which is working for all kinds of images.
Last, but not least, do not forget that step is measured in bytes, not pixels. So if you have a 3-channel uchar RGB image, step will be 3*number of pixels
, and if you have a 3 channel int image, step = 3(channels)*(4=sizeof(int))*(number rows)