#include <opencv2/opencv.hpp> #include

using namespace cv; using namespace std;

int main() { Mat img_gray = imread('lena_gray.jpg', IMREAD_GRAYSCALE); // 读取灰度图像

int rows = img_gray.rows; // 获取行数
int cols = img_gray.cols; // 获取列数

for(int i=0; i<rows; i++)
{
    for(int j=0; j<cols; j++)
    {
        uchar pixel = img_gray.at<uchar>(i,j); // 获取像素值
        cout << (int)pixel << ' '; // 输出像素值
    }
    cout << endl;
}

return 0;

}

C++ OpenCV 获取灰度图像矩阵数据示例