通过阅读Opencv3中文版 得出Mat的区块矩阵访问函数如下:
实践代码如下:
void test01()
{
//构建Mat二维数组 二维数组包括3行3列
Mat Test = (Mat_<double>(3, 3) << 0, 1, 2, 3, 4, 5, 6, 7, 8);
cout << "Total matrix:" << endl;
cout << Test << endl << endl;
cv::Range r;
//取出Test矩阵中的第一行和第二行数据
Mat testrow = Test.rowRange(0, 2).clone();
cout << "Row range:" << endl;
cout << testrow << endl <<endl;
//单独取出第一行数据
cout << "Test 1 row:" << endl;
cout << Test.row(0) << endl;
//单独取出第二行数据
cout << "Test 2 row:" << endl;
cout << Test.row(1) << endl;
cout << "Test 2 row:" << endl<<endl;
//取出0列和1列 共两列数据
Mat testcol = Test.colRange(0, 2).clone();
cout << "Col range:" << endl;
cout << testcol << endl << endl;
//取出第一个元素所在的“\”对角线内容
Mat testdiag0 = Test.diag(0);
cout << "testdiag0:" << endl;
cout << testdiag0 << endl;
//取出第二个元素所在的“\”对角线内容
Mat testdiag1 = Test.diag(1);
cout << "testdiag1:" << endl;
cout << testdiag1 << endl<<endl;
//没有Test.diag(3);项
//Mat testdiag3 = Test.diag(3);
//cout << "testdiag3:" << endl;
//cout << testdiag3 << endl;
//取出开始行结束行,开始列结束列的矩阵内容 下面表示3行两列的内容
int row_start = 0;
int row_end = 3;
int col_start = 0;
int col_end = 2;
cv::Range row_range(row_start, row_end);
cv::Range col_range(col_start, col_end);
Mat m_range = Test(row_range, col_range).clone();
cout << "m_range:" << endl;
cout << m_range << endl<<endl;
//取出Rect矩形范围内的矩阵内容
cv::Rect rect(0, 1, 2, 2); //表示左上角为(0,1)的坐标原点,之后圈出宽为2,高度为3的矩阵内容
Mat m_rect = Test(rect).clone();
cout << "m_rect:" << endl;
cout << m_rect << endl;
//取出ranges索引区域构成的数组
//r_array数组必须和Mat的维度数量相同
cv::Range r_01(0, 2);
cv::Range r_02(0, 2);
cv::Range r_array[2] = { r_01,r_02 };
Mat m_ranges = Test(r_array).clone();
cout << "m_ranges:" << endl;
cout << m_ranges << endl;
}
运行结果如下:
参考网址:https://blog.csdn.net/sinat_29089097/article/details/75533538