1 | initial version |
jayrambhia's answer is approximately right, but it doesn't account for the step (also called the stride or pitch) of the Mat. You need to multiply by img.step, rather than img.cols. Also, if you're interested in speed, you want the per-colum loop nested inside the per-row loop. Something like the following. Even this assumes RGB color model in BGR component order, so it doesn't work for gray scale.
unsigned char *input = (unsigned char*)(img.data);
for(int j = 0;j < img.rows;j++){
for(int i = 0;i < img.cols;i++){
unsigned char b = input[img.step * j + i ] ;
unsigned char g = input[img.step * j + i + 1];
unsigned char r = input[img.step * j + i + 2];
}
}
}