#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat m = (Mat_<int>(3, 2) << 11, 12, 33, 43, 51, 16);
/*************************方法一*********************************/
for (int r = 0; r < m.rows; r++)
{
for (int c = 0; c < m.cols; c++)
{
//cout << m.at<int>(r, c) << ",";
cout << m.at<int>(Point(c, r)) << ",";//效果一样
}
cout << endl;
}
/*************************方法二*********************************/
for (int r = 0; r < m.rows; r++)
{
const int * ptr = m.ptr<int>(r);
for (int c = 0; c < m.cols; c++)
{
//cout << ptr[c] << ",";
cout << *(ptr + c) << ",";
}
cout << endl;
}
/**************************方法三*********************************/
if (m.isContinuous())
{
int*ptr = m.ptr<int>(0);
for (int n = 0; n < m.rows*m.cols; n++)
cout << ptr[n] << ",";
}
/*************************方法四*********************************/
for (int r = 0; r < m.rows; r++)
{
for (int c = 0; c < m.cols; c++)
{
cout << *((int*)(m.data + m.step[0] * r + c * m.step[1])) << ",";
//step[0]代表每一行所占字节数,若有间隔,间隔也会被当作字节数的一部分算在其中;
//step[1]代表每一个数值所站的字节数;
//data是指向第一个数值的指针。
}
cout << endl;
}
waitKey(0);
}
从取值效率上说,直接使用指针的形式取值最快,使用at是最慢的,但是可读性很高。