法一 指针法(效率最高)
void main()
{
//灰度图
Mat src = imread("src.jpg", 0);
//图像行、列
int row = src.rows, col = src.cols;
for (int i = 0; i < row; i++)
{
//读取整行数据
uchar* ptr = src.ptr<uchar>(i);
for (int j = 0; j < col; j++)
{
int pixel = (int)ptr[j];
cout << pixel << " ";
}
cout << endl;
}
}
法二
void main()
{
//灰度图
Mat src = imread("src.jpg", 0);
//图像行、列
int row = src.rows, col = src.cols;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
int pixel = (int)src.ptr<uchar>(i, j);
cout << pixel << " ";
}
cout << endl;
}
}
法三
void main()
{
//灰度图
Mat src = imread("src.jpg", 0);
//图像行、列
int row = src.rows, col = src.cols;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
int pixel = (int)src.at<uchar>(i,j);
cout << pixel << " ";
}
cout << endl;
}
}
来源:CSDN
作者:忘·月
链接:https://blog.csdn.net/ETNthrough/article/details/104180533