cv::Mat区块访间

 

通过阅读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 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值