// create a 3x3 matrix cv::Mat mat(3, 3, CV_32FC1); // fill the matrix for(int i = 0; i < mat.rows; i++) { for(int j = 0; j < mat.cols; j++) { mat.at(i, j) = i * 3 + j; } } // get a pointer to the second column float* col_ptr = mat.ptr (0) + 1; // print the elements in the second column for(int i = 0; i < mat.rows; i++) { std::cout << col_ptr[i * mat.step1()] << std::endl; }
// create a 3x3 matrix cv::Mat mat(3, 3, CV_32SC1); // fill the matrix for(int i = 0; i < mat.rows; i++) { for(int j = 0; j < mat.cols; j++) { mat.atIn this example, we create a 3x3 matrix and fill it with row-major order. We then use the colptr function inside a loop to get pointers to each column of the matrix. Finally, we print out the elements in each column. The colptr function is part of the OpenCV C++ library.(i, j) = i * 3 + j; } } // get pointers to each column int* col_ptrs[3]; for(int j = 0; j < mat.cols; j++) { col_ptrs[j] = mat.ptr (0) + j; } // print the elements in each column for(int i = 0; i < mat.rows; i++) { for(int j = 0; j < mat.cols; j++) { std::cout << col_ptrs[j][i] << " "; } std::cout << std::endl; }