As for Martin's post, you can check if memory is constantly allocated using the isContinuous () method in the OpenCV Mat object. The following is a general idiom to ensure that the outer loop includes only one cycle, if possible:
#include <opencv2/core/core.hpp> using namespace cv; int main(void) { Mat img = imread("test.jpg"); int rows = img.rows; int cols = img.cols; if (img.isContinuous()) { cols = rows * cols; // Loop over all pixels as 1D array. rows = 1; } for (int i = 0; i < rows; i++) { Vec3b *ptr = img.ptr<Vec3b>(i); for (int j = 0; j < cols; j++) { Vec3b pixel = ptr[j]; } } return 0; }
source share